crypto modules
|
@ -1,10 +1,5 @@
|
||||||
#!/usr/bin/env python
|
#!/usr/bin/env python
|
||||||
|
|
||||||
def example_sha():
|
|
||||||
from Crypto.Hash import SHA256
|
|
||||||
hash = SHA256.new()
|
|
||||||
hash.update('message')
|
|
||||||
print hash.digest()
|
|
||||||
|
|
||||||
def example_aes():
|
def example_aes():
|
||||||
from Crypto.Cipher import AES
|
from Crypto.Cipher import AES
|
||||||
|
@ -19,5 +14,4 @@ def example_aes():
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
example_sha()
|
example_aes()
|
||||||
#example_aes()
|
|
17
Cryptography/Block_Ciphers/DES/DES_CFB_example.py
Normal file
|
@ -0,0 +1,17 @@
|
||||||
|
#!/usr/bin/env python
|
||||||
|
|
||||||
|
__author__ = "bt3"
|
||||||
|
|
||||||
|
|
||||||
|
from Crypto.Cipher import DES
|
||||||
|
from Crypto import Random
|
||||||
|
|
||||||
|
iv = Random.get_random_bytes(8)
|
||||||
|
|
||||||
|
des1 = DES.new('01234567', DES.MODE_CFB, iv)
|
||||||
|
des2 = DES.new('01234567', DES.MODE_CFB, iv)
|
||||||
|
text = 'abcdefghijklmnop'
|
||||||
|
|
||||||
|
cipher_text = des1.encrypt(text)
|
||||||
|
print cipher_text
|
||||||
|
print des2.decrypt(cipher_text)
|
22
Cryptography/Block_Ciphers/DES/DES_ECB_example.py
Normal file
|
@ -0,0 +1,22 @@
|
||||||
|
#!/usr/bin/env python
|
||||||
|
|
||||||
|
__author__ = "bt3"
|
||||||
|
|
||||||
|
|
||||||
|
from Crypto.Cipher import DES
|
||||||
|
|
||||||
|
def decrypt(key, text):
|
||||||
|
des = DES.new(key, DES.MODE_ECB)
|
||||||
|
return des.decrypt(text)
|
||||||
|
|
||||||
|
def encrypt(key, text):
|
||||||
|
des = DES.new(key, DES.MODE_ECB)
|
||||||
|
return des.encrypt(text)
|
||||||
|
|
||||||
|
if __name__ == '__main__':
|
||||||
|
text = "01234567"
|
||||||
|
key = 'abcdefgh'
|
||||||
|
print encrypt(key, text)
|
||||||
|
print
|
||||||
|
print decrypt(key, text)
|
||||||
|
|
Before Width: | Height: | Size: 1.5 KiB After Width: | Height: | Size: 1.5 KiB |
Before Width: | Height: | Size: 7.4 KiB After Width: | Height: | Size: 7.4 KiB |
Before Width: | Height: | Size: 1.3 KiB After Width: | Height: | Size: 1.3 KiB |
Before Width: | Height: | Size: 1.1 KiB After Width: | Height: | Size: 1.1 KiB |
10
Cryptography/Hash_Functions/SHA/sha_simple_example.py
Normal file
|
@ -0,0 +1,10 @@
|
||||||
|
#!/usr/bin/env python
|
||||||
|
|
||||||
|
def example_sha():
|
||||||
|
from Crypto.Hash import SHA256
|
||||||
|
hash = SHA256.new()
|
||||||
|
hash.update('message')
|
||||||
|
print hash.digest()
|
||||||
|
|
||||||
|
if __name__ == '__main__':
|
||||||
|
example_sha()
|
44
Cryptography/Public_Key/RSA.py
Normal file
|
@ -0,0 +1,44 @@
|
||||||
|
#!/usr/bin/env python
|
||||||
|
|
||||||
|
|
||||||
|
from Crypto.PublicKey import RSA
|
||||||
|
from Crypto import Random
|
||||||
|
|
||||||
|
|
||||||
|
def gen_key(nbits=1024):
|
||||||
|
random_generator = Random.new().read
|
||||||
|
key = RSA.generate(nbits, random_generator)
|
||||||
|
return key
|
||||||
|
|
||||||
|
def check_key(key):
|
||||||
|
print key.can_encrypt()
|
||||||
|
print key.can_sign()
|
||||||
|
print key.has_private()
|
||||||
|
|
||||||
|
|
||||||
|
def get_pubk(key):
|
||||||
|
return key.publickey()
|
||||||
|
|
||||||
|
|
||||||
|
def encrypt(public_key, text, random):
|
||||||
|
return public_key.encrypt(text, random)
|
||||||
|
|
||||||
|
|
||||||
|
def decrypt(data):
|
||||||
|
return key.decrypt(enc_data)
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == '__main__':
|
||||||
|
key = gen_key()
|
||||||
|
|
||||||
|
check_key(key)
|
||||||
|
|
||||||
|
public_key = get_pubk(key)
|
||||||
|
|
||||||
|
text = 'abcdefgh'
|
||||||
|
enc_data = encrypt(public_key, text, random=32)
|
||||||
|
print enc_data
|
||||||
|
|
||||||
|
dec_data = decrypt(enc_data)
|
||||||
|
print dec_data
|
||||||
|
|
16
Cryptography/Public_Key/RSA_sign.py
Normal file
|
@ -0,0 +1,16 @@
|
||||||
|
#!/usr/bin/env python
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
from Crypto.Hash import SHA256
|
||||||
|
from Crypto.PublicKey import RSA
|
||||||
|
from Crypto import Random
|
||||||
|
|
||||||
|
key = RSA.generate(1024)
|
||||||
|
text = 'abcdefgh'
|
||||||
|
|
||||||
|
hash = SHA256.new(text).digest()
|
||||||
|
print hash
|
||||||
|
|
||||||
|
signature = key.sign(hash, '')
|
||||||
|
print signature
|
12
Cryptography/Stream_Ciphers/arc4.py
Normal file
|
@ -0,0 +1,12 @@
|
||||||
|
#!/usr/bin/env python
|
||||||
|
|
||||||
|
|
||||||
|
from Crypto.Cipher import ARC4
|
||||||
|
|
||||||
|
obj1 = ARC4.new('01234567')
|
||||||
|
obj2 = ARC4.new('01234567')
|
||||||
|
|
||||||
|
text = 'abcdefghijklmnop'
|
||||||
|
cipher_text = obj1.encrypt(text)
|
||||||
|
print cipher_text
|
||||||
|
print obj2.decrypt(cipher_text)
|