crypto modules

This commit is contained in:
Mari Wahl 2015-01-03 20:47:39 -05:00
parent f211c1cbab
commit 5fcb5a5cb9
28 changed files with 122 additions and 7 deletions

View file

@ -1,10 +1,5 @@
#!/usr/bin/env python
def example_sha():
from Crypto.Hash import SHA256
hash = SHA256.new()
hash.update('message')
print hash.digest()
def example_aes():
from Crypto.Cipher import AES
@ -19,5 +14,4 @@ def example_aes():
if __name__ == '__main__':
example_sha()
#example_aes()
example_aes()

View 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)

View 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)

View 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()

View 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

View 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

View 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)