Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit d18fbb7

Browse files
committed
Python: Add working tests of AES and RC4
1 parent cf64701 commit d18fbb7

4 files changed

Lines changed: 138 additions & 0 deletions

File tree

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
# https://pycryptodome.readthedocs.io/en/latest/src/cipher/aes.html
2+
from Cryptodome.Cipher import AES
3+
4+
import os
5+
6+
key = os.urandom(256//8)
7+
iv = os.urandom(16)
8+
9+
# ------------------------------------------------------------------------------
10+
# encrypt/decrypt
11+
# ------------------------------------------------------------------------------
12+
13+
14+
print("encrypt/decrypt")
15+
16+
secret_message = b"secret message"
17+
18+
padding_len = 16 - (len(secret_message) % 16)
19+
padding = b"\0"*padding_len
20+
21+
cipher = AES.new(key, AES.MODE_CBC, iv=iv)
22+
# using separate .encrypt calls on individual lines does not work
23+
whole_plantext = secret_message + padding
24+
encrypted = cipher.encrypt(whole_plantext)
25+
26+
print("encrypted={}".format(encrypted))
27+
28+
print()
29+
30+
cipher = AES.new(key, AES.MODE_CBC, iv=iv)
31+
decrypted = cipher.decrypt(encrypted)
32+
33+
decrypted = decrypted[:-padding_len]
34+
35+
print("decrypted={}".format(decrypted))
36+
assert decrypted == secret_message
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
# https://pycryptodome.readthedocs.io/en/latest/src/cipher/arc4.html
2+
from Cryptodome.Cipher import ARC4
3+
4+
import os
5+
6+
key = os.urandom(256//8)
7+
8+
9+
# ------------------------------------------------------------------------------
10+
# encrypt/decrypt
11+
# ------------------------------------------------------------------------------
12+
13+
14+
15+
print("encrypt/decrypt")
16+
17+
secret_message = b"secret message"
18+
19+
cipher = ARC4.new(key)
20+
encrypted = cipher.encrypt(secret_message)
21+
22+
print("encrypted={}".format(encrypted))
23+
24+
print()
25+
26+
cipher = ARC4.new(key)
27+
decrypted = cipher.decrypt(encrypted)
28+
29+
print("decrypted={}".format(decrypted))
30+
assert decrypted == secret_message
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
from cryptography.hazmat.primitives.ciphers import algorithms, Cipher, modes
2+
import os
3+
4+
key = os.urandom(256//8)
5+
iv = os.urandom(16)
6+
7+
algorithm = algorithms.AES(key)
8+
cipher = Cipher(algorithm, mode=modes.CBC(iv))
9+
10+
# ------------------------------------------------------------------------------
11+
# encrypt/decrypt
12+
# ------------------------------------------------------------------------------
13+
14+
# following https://cryptography.io/en/latest/hazmat/primitives/symmetric-encryption/#cryptography.hazmat.primitives.ciphers.Cipher
15+
16+
print("encrypt/decrypt")
17+
18+
secret_message = b"secret message"
19+
20+
padding_len = 16 - (len(secret_message) % 16)
21+
padding = b"\0"*padding_len
22+
23+
encryptor = cipher.encryptor()
24+
print(padding_len)
25+
encrypted = encryptor.update(secret_message)
26+
encrypted += encryptor.update(padding)
27+
encrypted += encryptor.finalize()
28+
29+
print("encrypted={}".format(encrypted))
30+
31+
print()
32+
33+
decryptor = cipher.decryptor()
34+
decrypted = decryptor.update(encrypted)
35+
decrypted += decryptor.finalize()
36+
37+
decrypted = decrypted[:-padding_len]
38+
39+
print("decrypted={}".format(decrypted))
40+
assert decrypted == secret_message
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
from cryptography.hazmat.primitives.ciphers import algorithms, Cipher
2+
import os
3+
4+
key = os.urandom(256//8)
5+
6+
algorithm = algorithms.ARC4(key)
7+
cipher = Cipher(algorithm, mode=None)
8+
9+
# ------------------------------------------------------------------------------
10+
# encrypt/decrypt
11+
# ------------------------------------------------------------------------------
12+
13+
# following https://cryptography.io/en/latest/hazmat/primitives/symmetric-encryption.html#cryptography.hazmat.primitives.ciphers.algorithms.ARC4
14+
15+
print("encrypt/decrypt")
16+
17+
secret_message = b"secret message"
18+
19+
encryptor = cipher.encryptor()
20+
encrypted = encryptor.update(secret_message)
21+
encrypted += encryptor.finalize()
22+
23+
print("encrypted={}".format(encrypted))
24+
25+
print()
26+
27+
decryptor = cipher.decryptor()
28+
decrypted = decryptor.update(encrypted)
29+
decrypted += decryptor.finalize()
30+
31+
print("decrypted={}".format(decrypted))
32+
assert decrypted == secret_message

0 commit comments

Comments
 (0)