mirror of
https://github.com/billbuchanan/appliedcrypto.git
synced 2026-02-21 11:18:02 +00:00
Update
This commit is contained in:
BIN
unit02_symmetric/.DS_Store
vendored
BIN
unit02_symmetric/.DS_Store
vendored
Binary file not shown.
Binary file not shown.
Binary file not shown.
BIN
unit02_symmetric/lecture/~$chapter02_secret.pptx
Normal file
BIN
unit02_symmetric/lecture/~$chapter02_secret.pptx
Normal file
Binary file not shown.
@@ -1,35 +1,56 @@
|
||||
from Crypto.Cipher import AES
|
||||
from cryptography.hazmat.primitives.ciphers import Cipher, algorithms, modes
|
||||
from cryptography.hazmat.primitives import padding
|
||||
from cryptography.hazmat.backends import default_backend
|
||||
|
||||
import hashlib
|
||||
import sys
|
||||
import binascii
|
||||
import Padding
|
||||
|
||||
val='hello'
|
||||
password='hello'
|
||||
password='hello123'
|
||||
|
||||
plaintext=val
|
||||
|
||||
def encrypt(plaintext,key, mode):
|
||||
encobj = AES.new(key,mode)
|
||||
return(encobj.encrypt(plaintext))
|
||||
method=algorithms.AES(key)
|
||||
cipher = Cipher(method,mode, default_backend())
|
||||
encryptor = cipher.encryptor()
|
||||
ct = encryptor.update(plaintext) + encryptor.finalize()
|
||||
return(ct)
|
||||
|
||||
def decrypt(ciphertext,key, mode):
|
||||
encobj = AES.new(key,mode)
|
||||
return(encobj.decrypt(ciphertext))
|
||||
method=algorithms.AES(key)
|
||||
cipher = Cipher(method, mode, default_backend())
|
||||
decryptor = cipher.decryptor()
|
||||
pl = decryptor.update(ciphertext) + decryptor.finalize()
|
||||
return(pl)
|
||||
|
||||
def pad(data,size=128):
|
||||
padder = padding.PKCS7(size).padder()
|
||||
padded_data = padder.update(data)
|
||||
padded_data += padder.finalize()
|
||||
return(padded_data)
|
||||
|
||||
def unpad(data,size=128):
|
||||
padder = padding.PKCS7(size).unpadder()
|
||||
unpadded_data = padder.update(data)
|
||||
unpadded_data += padder.finalize()
|
||||
return(unpadded_data)
|
||||
|
||||
key = hashlib.sha256(password.encode()).digest()
|
||||
|
||||
print("Before padding: ",plaintext)
|
||||
|
||||
plaintext = Padding.appendPadding(plaintext,blocksize=Padding.AES_blocksize,mode='CMS')
|
||||
plaintext=pad(plaintext.encode())
|
||||
|
||||
print("After padding (CMS): ",binascii.hexlify(bytearray(plaintext.encode())))
|
||||
print("After padding (CMS): ",binascii.hexlify(bytearray(plaintext)))
|
||||
|
||||
ciphertext = encrypt(plaintext.encode(),key,AES.MODE_ECB)
|
||||
ciphertext = encrypt(plaintext,key,modes.ECB())
|
||||
print("Cipher (ECB): ",binascii.hexlify(bytearray(ciphertext)))
|
||||
|
||||
plaintext = decrypt(ciphertext,key,AES.MODE_ECB)
|
||||
plaintext = decrypt(ciphertext,key,modes.ECB())
|
||||
|
||||
plaintext = unpad(plaintext)
|
||||
print(" decrypt: ",plaintext.decode())
|
||||
|
||||
plaintext = Padding.removePadding(plaintext.decode(),mode='CMS')
|
||||
print(" decrypt: ",plaintext)
|
||||
|
||||
plaintext=val
|
||||
|
||||
33
unit02_symmetric/src/g_01.py
Normal file
33
unit02_symmetric/src/g_01.py
Normal file
@@ -0,0 +1,33 @@
|
||||
from cryptography.hazmat.primitives import hashes
|
||||
from cryptography.hazmat.primitives.ciphers.aead import ChaCha20Poly1305
|
||||
import sys
|
||||
import binascii
|
||||
from cryptography.hazmat.backends import default_backend
|
||||
|
||||
|
||||
msg = "edinburgh"
|
||||
key = "qwerty"
|
||||
|
||||
if (len(sys.argv)>1):
|
||||
msg=str(sys.argv[1])
|
||||
|
||||
if (len(sys.argv)>2):
|
||||
key=str(sys.argv[2])
|
||||
|
||||
print ("Data:\t",msg)
|
||||
print ("Key:\t",key)
|
||||
|
||||
digest = hashes.Hash(hashes.SHA256(),default_backend())
|
||||
digest.update(key.encode())
|
||||
k=digest.finalize()
|
||||
|
||||
chacha = ChaCha20Poly1305(k)
|
||||
nonce = '\0\0\0\0\0\0\0\0\0\0\0\0'
|
||||
add=''
|
||||
cipher = chacha.encrypt(nonce.encode(), msg.encode(), add.encode())
|
||||
rtn=chacha.decrypt(nonce.encode(), cipher, add.encode())
|
||||
|
||||
print ("\nKey:\t",binascii.b2a_hex(key.encode()).decode())
|
||||
print ("Nonce:\t",binascii.b2a_hex(nonce.encode()).decode())
|
||||
print ("\nCipher:\t",binascii.b2a_hex(cipher).decode())
|
||||
print ("Decrypted:\t",rtn.decode())
|
||||
38
unit02_symmetric/src/g_02.py
Normal file
38
unit02_symmetric/src/g_02.py
Normal file
@@ -0,0 +1,38 @@
|
||||
from cryptography.hazmat.primitives import hashes
|
||||
from cryptography.hazmat.primitives.ciphers import Cipher, algorithms
|
||||
import sys
|
||||
import binascii
|
||||
from cryptography.hazmat.backends import default_backend
|
||||
|
||||
|
||||
|
||||
|
||||
msg = "edinburgh"
|
||||
key = "qwerty"
|
||||
|
||||
if (len(sys.argv)>1):
|
||||
msg=str(sys.argv[1])
|
||||
|
||||
if (len(sys.argv)>2):
|
||||
key=str(sys.argv[2])
|
||||
|
||||
print ("Data:\t",msg)
|
||||
print ("Key:\t",key)
|
||||
|
||||
digest = hashes.Hash(hashes.SHA256(),default_backend())
|
||||
digest.update(key.encode())
|
||||
k=digest.finalize()
|
||||
|
||||
algorithm = algorithms.ARC4(k)
|
||||
cipher = Cipher(algorithm, mode=None, backend=default_backend())
|
||||
encryptor = cipher.encryptor()
|
||||
ct = encryptor.update(msg.encode())
|
||||
pt = cipher.decryptor()
|
||||
pt=pt.update(ct)
|
||||
|
||||
|
||||
|
||||
print ("\nKey:\t",binascii.b2a_hex(key.encode()).decode())
|
||||
|
||||
print ("\nCipher:\t",binascii.b2a_hex(ct).decode())
|
||||
print ("Decrypted:\t",pt.decode())
|
||||
35
unit02_symmetric/src/old/d_01.py
Normal file
35
unit02_symmetric/src/old/d_01.py
Normal file
@@ -0,0 +1,35 @@
|
||||
from Crypto.Cipher import AES
|
||||
import hashlib
|
||||
import sys
|
||||
import binascii
|
||||
import Padding
|
||||
|
||||
val='hello'
|
||||
password='hello'
|
||||
|
||||
plaintext=val
|
||||
|
||||
def encrypt(plaintext,key, mode):
|
||||
encobj = AES.new(key,mode)
|
||||
return(encobj.encrypt(plaintext))
|
||||
|
||||
def decrypt(ciphertext,key, mode):
|
||||
encobj = AES.new(key,mode)
|
||||
return(encobj.decrypt(ciphertext))
|
||||
|
||||
key = hashlib.sha256(password.encode()).digest()
|
||||
|
||||
|
||||
plaintext = Padding.appendPadding(plaintext,blocksize=Padding.AES_blocksize,mode='CMS')
|
||||
|
||||
print("After padding (CMS): ",binascii.hexlify(bytearray(plaintext.encode())))
|
||||
|
||||
ciphertext = encrypt(plaintext.encode(),key,AES.MODE_ECB)
|
||||
print("Cipher (ECB): ",binascii.hexlify(bytearray(ciphertext)))
|
||||
|
||||
plaintext = decrypt(ciphertext,key,AES.MODE_ECB)
|
||||
|
||||
plaintext = Padding.removePadding(plaintext.decode(),mode='CMS')
|
||||
print(" decrypt: ",plaintext)
|
||||
|
||||
plaintext=val
|
||||
Reference in New Issue
Block a user