from Crypto.Cipher import AES
import os
key = 'mysecretpassword'
iv = os.urandom(16)
plaintext1 = 'Secret Message A'
encobj = AES.new(key, AES.MODE_CBC, iv)
ciphertext1 = encobj.encrypt(plaintext1)
encryptedText = ciphertext1.encode('base64')
print encryptedText
decobj = AES.new(key, AES.MODE_CBC, iv)
print decobj.decrypt(ciphertext1)
I copied the printed value of encryptedText and the key from my code and pasted to the websites below.
http://www.everpassword.com/aes-encryptor
http://www.nakov.com/blog/2011/12/26/online-aes-encryptor-decryptor-javascript/
I would expected it to be able to decrypt my cipher, but it doesn’t. Thus I must be using pycrypto wrong. How do I fix this? The two sites can both encrypt and decrypt between each other, but mines can’t. Both the websites do indeed use CBC mode.
If you look at the page source for the website in question, you will see that it uses gibberish-aes javascript library. To see whet you have to do to make it work, you have to study what it does.
Looking through its source code, it seems to use a random salt for encryption. That, prepended by the string
Salted__forms the beginning of the cyphertext before it is base64 encoded.and
For decryption, it uses picks the random portion of the salt out of the beginning of the cyphertext after base64 decoding (the first
sliceoperator):The missing piece now is the
openSSLkeyfunction:So basically you have to translate the
openSSLKeyfunction to Python and feed it your password and salt. That creates a (key, iv) tuple. Use those to encrypt your data. Prepend the stringSalted__and the salt to the ciphertext before encoding it with base64. Then it should work, I think.