{{ message }}

Instantly share code, notes, and snippets.

# marekyggdrasil/aes.py

Last active Apr 24, 2021
Examples for tutorial on symmetric ciphers available under https://mareknarozniak.com/2021/04/22/symmetric/
 from Crypto.Cipher import AES from getpass import getpass ciphertext = None with open('ciphertext3.txt', 'r') as file: ciphertext = bytes.fromhex(file.read()) password = getpass('Password: ') key = bytes(password, 'ascii') key = key + b'\x00'*(16-len(key)) vect = getpass('IV: ') IV = bytes.fromhex(vect) cipher = AES.new(key, AES.MODE_CBC, IV=IV) plaintext = cipher.decrypt(ciphertext) print(plaintext)
 def rotate(alphabet, rot): return alphabet[rot:] + alphabet[:rot] def applyCipher(alphabet, cipher): unique = str(''.join(list(dict.fromkeys(list(cipher))))) applied = str(alphabet) for c in unique: applied = applied.replace(c, '') applied = unique + applied return applied def encrypt(plaintext, alphabet, cipher, rot): lookup = rotate(applyCipher(alphabet, cipher), rot) ciphertext = '' for c in plaintext: index = alphabet.index(c) cp = lookup[index] ciphertext += cp return ciphertext def decrypt(ciphertext, alphabet, cipher, rot): lookup = rotate(applyCipher(alphabet, cipher), rot) plaintext = '' for c in ciphertext: index = lookup.index(c) cp = alphabet[index] plaintext += cp return plaintext poke = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz\'?!.,0123456789 ' for i in [1, 2]: filename = 'plaintext{0}.txt'.format(str(i)) data = None with open(filename, 'r') as file: data = file.read().split('\n') plaintext = data[0] cipher = data[1] rot = int(data[2]) ciphertext = encrypt(plaintext, poke, cipher, rot) decrypted = decrypt(ciphertext, poke, cipher, rot) assert plaintext == decrypted outfile = 'ciphertext{0}.txt'.format(str(i)) with open(outfile, 'w') as file: file.write(ciphertext)
 Iurt0rh0hz6rmugmzwvzhg5rIurgmzk?mzhzruvrvuhrouv.z5rHmghzr'lhrjbfh6rqmhzr'lhrzuboh5rIur'lhrz?urmurt0rgmzkybfh5rBvmqmukrlhb'6rz?tthyrz'hufl5r2Nhb'lr'lhrcqbfor'lhrzo0rqvvozrghbg5rCbqqrt0rubthr'lyv?klr'lhrfyhbt5rAugrI2qqrlhbyr0v?rzfyhbtrbkbmu5rBqbforlvqhrz?u6r.vu2'r0v?rfvthrbugr.bzlrb.b0r'lhrybmu3rBqbforlvqhrz?u6r.vu2'r0v?rfvth3rWvu2'r0v?rfvth3rWvu2'r0v?rfvth3
 Et'X,tgczt'j??juftjut?gctzXju8tOXjubzvw'teXppjuftvutgcz8tPgctbjbu5?t'ccqt?vtZXzc8tPgct'X?t?gczctXubt'qjpcbtX?tqcmubtEtnuc,t'gctZv!pbtqXnctqctgXww28tApv,cz'tjutgcztgXjz tepv,cz'tc.cz2,gczc7tEtpv.ct?gctepv,cztfjzp8tKg tEtbvu5?tnuv,tk!'?t,g2t'gct'jqwp2tZX!fg?tq2tc2c8tEtpv.ct?gctepv,cztfjzpt'gct'ccqcbt'vt',cc?tXubtnjub8tPgctZzcw?tju?vtq2tqjub8