BS = 16
pad = lambda s: s + (BS - len(s) % BS) * chr(BS - len(s) % BS)
unpad = lambda s: s[:-ord(s[len(s) - 1:])]
base64pad = lambda s: s + '=' * (4 - len(s) % 4)
base64unpad = lambda s: s.rstrip("=")
encrypt_key = 'BKHlhb899Y09olUa' #Needs to be 16 chars
def encrypt(key, msg):
iv = Random.new().read(BS)
cipher = AES.new(key.encode("utf8"), AES.MODE_CFB, iv)
encrypted_msg = cipher.encrypt(pad(str(msg)).encode("utf8"))
return base64unpad(urlsafe_b64encode(iv + encrypted_msg).decode("utf8"))
def decrypt(key, msg):
decoded_msg = urlsafe_b64decode(base64pad(msg))
iv = decoded_msg[:BS]
encrypted_msg = decoded_msg[BS:]
cipher = AES.new(key.encode("utf8"), AES.MODE_CFB, iv)
return unpad(cipher.decrypt(encrypted_msg))
Encrypted = encrypt(encrypt_key, "1234567890123456")
print(Encrypted)
Decrypted = decrypt(encrypt_key, Encrypted)
print(Decrypted)
Выше приведен код, который я использую в Python 3, который отлично работает.Но я не могу расшифровать зашифрованный текст в PHP.Мой текущий код:
$encryptedText = "wIR0ElYIkuKckQQ-0KikhLuXjkL2vTAaOtaO7WyVp2A2JE5FTWZf__ZgCmUQ2Lre";
$key = "BKHlhb899Y09olUa";
echo openssl_decrypt(substr(base64_decode($encryptedText), 16), "AES-128-CFB", $key, OPENSSL_RAW_DATA, substr(base64_decode($encryptedText), 0, 16))."<br>";