mirror of
https://github.com/github/codeql.git
synced 2026-04-29 10:45:15 +02:00
Python: Add working tests of AES and RC4
This commit is contained in:
@@ -0,0 +1,36 @@
|
||||
# https://pycryptodome.readthedocs.io/en/latest/src/cipher/aes.html
|
||||
from Cryptodome.Cipher import AES
|
||||
|
||||
import os
|
||||
|
||||
key = os.urandom(256//8)
|
||||
iv = os.urandom(16)
|
||||
|
||||
# ------------------------------------------------------------------------------
|
||||
# encrypt/decrypt
|
||||
# ------------------------------------------------------------------------------
|
||||
|
||||
|
||||
print("encrypt/decrypt")
|
||||
|
||||
secret_message = b"secret message"
|
||||
|
||||
padding_len = 16 - (len(secret_message) % 16)
|
||||
padding = b"\0"*padding_len
|
||||
|
||||
cipher = AES.new(key, AES.MODE_CBC, iv=iv)
|
||||
# using separate .encrypt calls on individual lines does not work
|
||||
whole_plantext = secret_message + padding
|
||||
encrypted = cipher.encrypt(whole_plantext)
|
||||
|
||||
print("encrypted={}".format(encrypted))
|
||||
|
||||
print()
|
||||
|
||||
cipher = AES.new(key, AES.MODE_CBC, iv=iv)
|
||||
decrypted = cipher.decrypt(encrypted)
|
||||
|
||||
decrypted = decrypted[:-padding_len]
|
||||
|
||||
print("decrypted={}".format(decrypted))
|
||||
assert decrypted == secret_message
|
||||
@@ -0,0 +1,30 @@
|
||||
# https://pycryptodome.readthedocs.io/en/latest/src/cipher/arc4.html
|
||||
from Cryptodome.Cipher import ARC4
|
||||
|
||||
import os
|
||||
|
||||
key = os.urandom(256//8)
|
||||
|
||||
|
||||
# ------------------------------------------------------------------------------
|
||||
# encrypt/decrypt
|
||||
# ------------------------------------------------------------------------------
|
||||
|
||||
|
||||
|
||||
print("encrypt/decrypt")
|
||||
|
||||
secret_message = b"secret message"
|
||||
|
||||
cipher = ARC4.new(key)
|
||||
encrypted = cipher.encrypt(secret_message)
|
||||
|
||||
print("encrypted={}".format(encrypted))
|
||||
|
||||
print()
|
||||
|
||||
cipher = ARC4.new(key)
|
||||
decrypted = cipher.decrypt(encrypted)
|
||||
|
||||
print("decrypted={}".format(decrypted))
|
||||
assert decrypted == secret_message
|
||||
@@ -0,0 +1,40 @@
|
||||
from cryptography.hazmat.primitives.ciphers import algorithms, Cipher, modes
|
||||
import os
|
||||
|
||||
key = os.urandom(256//8)
|
||||
iv = os.urandom(16)
|
||||
|
||||
algorithm = algorithms.AES(key)
|
||||
cipher = Cipher(algorithm, mode=modes.CBC(iv))
|
||||
|
||||
# ------------------------------------------------------------------------------
|
||||
# encrypt/decrypt
|
||||
# ------------------------------------------------------------------------------
|
||||
|
||||
# following https://cryptography.io/en/latest/hazmat/primitives/symmetric-encryption/#cryptography.hazmat.primitives.ciphers.Cipher
|
||||
|
||||
print("encrypt/decrypt")
|
||||
|
||||
secret_message = b"secret message"
|
||||
|
||||
padding_len = 16 - (len(secret_message) % 16)
|
||||
padding = b"\0"*padding_len
|
||||
|
||||
encryptor = cipher.encryptor()
|
||||
print(padding_len)
|
||||
encrypted = encryptor.update(secret_message)
|
||||
encrypted += encryptor.update(padding)
|
||||
encrypted += encryptor.finalize()
|
||||
|
||||
print("encrypted={}".format(encrypted))
|
||||
|
||||
print()
|
||||
|
||||
decryptor = cipher.decryptor()
|
||||
decrypted = decryptor.update(encrypted)
|
||||
decrypted += decryptor.finalize()
|
||||
|
||||
decrypted = decrypted[:-padding_len]
|
||||
|
||||
print("decrypted={}".format(decrypted))
|
||||
assert decrypted == secret_message
|
||||
@@ -0,0 +1,32 @@
|
||||
from cryptography.hazmat.primitives.ciphers import algorithms, Cipher
|
||||
import os
|
||||
|
||||
key = os.urandom(256//8)
|
||||
|
||||
algorithm = algorithms.ARC4(key)
|
||||
cipher = Cipher(algorithm, mode=None)
|
||||
|
||||
# ------------------------------------------------------------------------------
|
||||
# encrypt/decrypt
|
||||
# ------------------------------------------------------------------------------
|
||||
|
||||
# following https://cryptography.io/en/latest/hazmat/primitives/symmetric-encryption.html#cryptography.hazmat.primitives.ciphers.algorithms.ARC4
|
||||
|
||||
print("encrypt/decrypt")
|
||||
|
||||
secret_message = b"secret message"
|
||||
|
||||
encryptor = cipher.encryptor()
|
||||
encrypted = encryptor.update(secret_message)
|
||||
encrypted += encryptor.finalize()
|
||||
|
||||
print("encrypted={}".format(encrypted))
|
||||
|
||||
print()
|
||||
|
||||
decryptor = cipher.decryptor()
|
||||
decrypted = decryptor.update(encrypted)
|
||||
decrypted += decryptor.finalize()
|
||||
|
||||
print("decrypted={}".format(decrypted))
|
||||
assert decrypted == secret_message
|
||||
Reference in New Issue
Block a user