Python 3 AES-CFB Encryption (pycryptodomex):

from Cryptodome.Cipher import AES
from Cryptodome import Random
from base64 import b64encode, b64decode

BS = 16
base64pad = lambda s: s + '=' * (4 - len(s) % 4)
#base64unpad = lambda s: s.rstrip("=")

def encrypt(key, msg): #https://pycryptodome.readthedocs.io/en/latest/src/cipher/classic.html#cfb-mode - No padding needed for CFB mode
iv = Random.new().read(BS)
cipher = AES.new(key.encode("utf8"), AES.MODE_CFB, iv)
encrypted_msg = cipher.encrypt(str(msg).encode("utf8"))
return base64unpad(b64encode(iv + encrypted_msg).decode("utf8"))

encryptedText = encrypt("abcdefghijklmnop", "WebberSky") #Key needs to be 16 chars
print(EncryptedText)

PHP 7 AES-CFB Decryption (openssl):

function decrypt($encryptedText){
$key = 'abcdefghijklmnop';
$decrypted = openssl_decrypt(substr(base64_decode($encryptedText), 16), "AES-128-CFB8", $key, OPENSSL_RAW_DATA, substr(base64_decode($encryptedText), 0, 16));
return $decrypted;
}

echo decrypt($encryptedText);
Categories: Code

0 Comments

Leave a Reply

Avatar placeholder

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Verified by MonsterInsights