1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 |
#!/usr/bin/python3 # -*- coding:utf-8 -*- # # pip install PyCrypto # pip3 install PyCrypto # # See: https://stackoverflow.com/questions/46346371/convert-openssl-aes-in-php-to-python-aes import base64 from Crypto.Cipher import AES from Crypto import Random import re, random # echo test | openssl aes-256-cbc -e -base64 encryption_encoded_key = 'U2FsdGVkX19nm8SCCdOk1RytPaIUvb2+Z7w+JfarEYo=' end_of_data_mark = b'<EOD>' def aes_encrypt(data): try: global encryption_encoded_key, end_of_data_mark data += end_of_data_mark.decode() # Align data length data = data + padding(data) encryption_key = base64.b64decode(encryption_encoded_key) iv = Random.new().read(AES.block_size) cipher = AES.new(encryption_key, AES.MODE_CFB, iv, segment_size=128) # Encrypt the data using AES 256 encryption in CBC mode using our encryption key and initialization vector. encrypted = cipher.encrypt(data) return base64.b64encode(encrypted).decode(), base64.b64encode(iv).decode() except Exception as e: raise Exception(str(e)) def aes_decrypt(data, iv): try: global encryption_encoded_key, end_of_data_mark encryption_key = base64.b64decode(encryption_encoded_key) iv = base64.b64decode(iv) cipher = AES.new(encryption_key, AES.MODE_CFB, iv, segment_size=128) encrypted_data = base64.b64decode(data) # Align data length encrypted_data = encrypted_data + padding(encrypted_data).encode() return cipher.decrypt(encrypted_data).split(end_of_data_mark)[0].decode() except Exception as e: raise Exception(str(e)) def padding(data): try: size = len(data) i = int(size / AES.block_size) j = AES.block_size * i if j < size: j += AES.block_size return "".ljust(j - size, "\x00") except Exception as e: raise Exception(str(e)) if __name__ == '__main__': # 12345678123456781234567812345678 data = 'Hello world!' data, iv = aes_encrypt(data) print("data = %s" % data) print("iv = %s" % iv) data = aes_decrypt(data, iv) print("data = %s" % data) |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 |
/* See: https://code.google.com/archive/p/crypto-js/ https://code.google.com/archive/p/crypto-js/downloads https://stackoverflow.com/questions/14958103/how-to-decrypt-message-with-cryptojs-aes-i-have-a-working-ruby-example https://gist.github.com/marcoslin/8026990 http://sct9.blog85.fc2.com/blog-entry-62.html https://fisproject.jp/2015/01/crypto-js-pycrypto-compatible/ */ var encryption_encoded_key = 'U2FsdGVkX19nm8SCCdOk1RytPaIUvb2+Z7w+JfarEYo='; var end_of_data_mark = '<EOD>'; function aes_encrypt(data) { var key = CryptoJS.enc.Base64.parse(encryption_encoded_key).toString(); var key_hex = CryptoJS.enc.Hex.parse(key); var iv_base64 = iv(); var iv_string = CryptoJS.enc.Base64.parse(iv_base64).toString(); var iv_hex = CryptoJS.enc.Hex.parse(iv_string); data += end_of_data_mark; var result = CryptoJS.AES.encrypt(data, key_hex, { iv: iv_hex, mode: CryptoJS.mode.CFB, padding: CryptoJS.pad.NoPadding, }); return Array(result.toString(), iv_base64); } function aes_decrypt(data, iv_base64) { var key = CryptoJS.enc.Base64.parse(encryption_encoded_key).toString(); var key_hex = CryptoJS.enc.Hex.parse(key); var iv_string = CryptoJS.enc.Base64.parse(iv_base64).toString(); var iv_hex = CryptoJS.enc.Hex.parse(iv_string); var cipher = CryptoJS.lib.CipherParams.create({ ciphertext: CryptoJS.enc.Base64.parse(data), }); var result = CryptoJS.AES.decrypt(cipher, key_hex, { iv: iv_hex, mode: CryptoJS.mode.CFB, padding: CryptoJS.pad.NoPadding, }); result = result.toString(CryptoJS.enc.Utf8).split(end_of_data_mark, 1)[0]; return result; } function iv() { var rawStr = CryptoJS.lib.WordArray.random(128 / 16); var wordArray = CryptoJS.enc.Utf8.parse(rawStr); var base64 = CryptoJS.enc.Base64.stringify(wordArray); return base64; } (function () { var data = 'Hello world!'; var e = aes_encrypt(data); var data = e[0]; var iv = e[1]; console.log('data = ' + data); console.log('iv = ' + iv); var d = aes_decrypt(data, iv); console.log('data = ' + d); })(); |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
<html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <title>CryptoJS AES 256 mode-cfb</title> <script src="./components/core.js"></script> <script src="./components/md5.js"></script> <script src="./components/enc-base64.js"></script> <script src="./components/evpkdf.js"></script> <script src="./components/cipher-core.js"></script> <script src="./components/aes.js"></script> <script src="./components/mode-cfb.js"></script> <script src="./components/pad-nopadding.js"></script> <script src="test.js"></script> </head> <body> </body> </html> |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 |
<?php /* * https://stackoverflow.com/questions/46346371/convert-openssl-aes-in-php-to-python-aes */ $encryption_encoded_key = 'U2FsdGVkX19nm8SCCdOk1RytPaIUvb2+Z7w+JfarEYo='; $end_of_data_mark = '<EOD>'; function aes_encrypt($data) { global $encryption_encoded_key, $end_of_data_mark; try { $data .= $end_of_data_mark; $encryption_key = base64_decode($encryption_encoded_key); $iv = openssl_random_pseudo_bytes(openssl_cipher_iv_length('aes-256-cfb')); $encrypted = openssl_encrypt($data, 'aes-256-cfb', $encryption_key, 1, $iv); return Array(base64_encode($encrypted), base64_encode($iv)); } catch (Exception $e) { throw new Exception($e->getMessage()); } } function aes_decrypt($data, $iv) { global $encryption_encoded_key, $end_of_data_mark; try { $encryption_key = base64_decode($encryption_encoded_key); $encrypted_data = base64_decode($data); $iv = base64_decode($iv); return explode($end_of_data_mark, openssl_decrypt($encrypted_data, 'aes-256-cfb', $encryption_key, 1, $iv), 2)[0]; } catch (Exception $e) { throw new Exception($e->getMessage()); } } $data = 'Hello world!'; list($data, $iv) = aes_encrypt($data); echo "data = $data\n"; echo "iv = $iv\n"; $data = aes_decrypt($data, $iv); echo "data = $data\n"; |