Last active
April 24, 2021 10:36
-
-
Save marekyggdrasil/c92fbd293a350da4412d9865e83f7a89 to your computer and use it in GitHub Desktop.
Examples for tutorial on symmetric ciphers available under https://mareknarozniak.com/2021/04/22/symmetric/
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
3e75752a1995ef69a39e665cf77d11b6ad582351183a1bd9d44cb25867e61b05982807b75f4c68a1512ff77e65f96635f929cfcaba1e1c165c43285beb643f8a16d4185572c2b00f68a168f8b452515dedbad45bf8eee5bd10f061b496274fe484a1e3cc0e8aebd581b5ecf7c36d0661eb26d9b92acc68970dad3f276a2fb08dde9395af2c42e53add85e0187c3470ff2d9a09c5e42ec8c791579f3a7929fda1544bc5262d03fc72eb4ade4b38b5e86f7d46ab236861b4b95036cf513a4aee37d34e602214c1e7f752f11107cca42dac789b813cfad11f30fe0d8ad11f25b0afb85a577e503225151df6810472b72074c5174afc2b7e44657358a6efa43e346404da9c431a5e477daab481bfe6b8cbc8156f8b723e6cb4ed338a28c963a2d1fa78e05166ad83e159529d2a22ff845c4e8f53e3276e6cd8fb0b0d864d8c4b2546c77e3d9a878803b196d5de4ec3af438bf1d473edfa5916acc6a9c89676ac2d78725926e52d240c513a9376596148a0c3aa01d8aa0efdcf9752fa8bd61549aaf28c6fa80dbf178edb6483743de19fef8cbdf2a0c6bbeccca5f0443116f878ed860dd4f4281c17be51dfba08f361f0ae6ebeacfd0dd198aa00f92119c20af54e53c7ba4a7970aaaa5de8bf8178baa954ae31cf0690b8beeaa88e343c5ec737d6afc13f5849c81a536611abd6bb89cafb88bdd954ca79a78b63534a64c62800d81994801e0fd75d31e8ab1e8e9475aa0376b13855b0f1ae5763b4326f1c1bbeee0c2febeaeba2b2ef1ccbb6e24971976e0c4d0cfe798ff7c9de1bbadd94479a8f37d5fcf422dc66b0706698ffb7caba31d5b08e753eb863b3b7862f2f8c77f2cd82c7edefec9dcab13620133fbba52d7595248d0b8779ec6d657124055d04c245e449a96ed260e541d207d8ad68175c3c4ea55f76cfff436b6d4cf292c6d131c12474a8d488384077102794b261029a3c9ad175005e7d9fd0086730468f26fd19c6f4ec504bc796fbc5a71d7d5002160e3872d869c2967c954a397a065018e41a1d049695788241e577662bf80884776d22eb53ad734cd9a1167e6a9b92fd71b2e62854f30dfae239f84b1fb5e67f9f45c51fd953a5c66a28678e3910a0a1a1aafd86c1d22bc6a915443c7147fafbc12d240e9b3248d7e7e91edf69fdfcecc0be18f3f86b3532f22b817ca82899f4cf8d02612c71ff065fa240824149520ed34ccb1e145081910e5b7c2f1f7c51605dbf00796a827ea13b7e3411f85d89b25393a0a06a07b4b907fc85080906785b26dd3d5ce5a3dd1407ef628eb2611a8687b6d009f3f62d47911d8f6e73bcad408e333e8b4929220084c930e0b8e028fcc75943 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
In my eyes, indisposed. In disguises no one knows. Hides the face, lies the snake. In the sun in my disgrace. Boiling heat, summer stench. 'Neath the black the sky looks dead. Call my name through the cream. And I'll hear you scream again. Black hole sun, won't you come and wash away the rain? Black hole sun, won't you come? Won't you come? Won't you come? | |
disappear | |
7 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
here put text | |
<here password> | |
0 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
pycryptodome |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment