2

I have the following python code:

def AES_build_cipher(key, iv, op): return EVP.Cipher(alg='aes_128_cbc', key=key, iv=iv, op=op, padding=True) # PKCS#5 paddig def AES_encrypt(key, msg, iv): # key, iv -> bytes, msg -> text if iv is None: raise ValueError("IV must be defined!") # Return the encryption function def encrypt(data): cipher = AES_build_cipher(key, iv, ENCRYPTION) v = cipher.update(data) v = v + cipher.final() del cipher return v return encrypt(msg) 

It works ok (encryption/decryption via M2Crypto).

Java code for decryption:

public static String AESDecrypt(String b64data, byte[] key, byte[] iv) throws CipherException { try { aesCipher_ = Cipher.getInstance("AES/CBC/PKCS5Padding"); aesCipher_.init(Cipher.DECRYPT_MODE, new SecretKeySpec(key, "AES"), new IvParameterSpec(iv)); final byte[] byteData = Base64.decode(b64data, Base64.DEFAULT); final byte[] decryptedData = aesCipher_.doFinal(byteData); return new String(decryptedData); } catch (Exception e) { throw new CipherException(e); } } 

data:

  • iv = 8b9123ba6712612fb98452aac3854838 (hex representation)
  • text = 12345678901234567890 (simple text)
  • ciphertext = af87d97bf9779efcff0386d4eaee18619dc8f1fe7c5adea2a91657f53491bc2 (hex representation)
  • password = 791a06ee369dc2f842c655f6bec8ce2 (hex representation)

Result:

  • Exp:'12345678901234567890'
  • Got:'1���$V��c�J�}7890'

Looks like something wrong with IV (first 16 bytes of result). But I have no idea what I missed.

1 Answer 1

1

The problem was be with IV in python. It's actually will be unicode string, not ascii string. The following code will help convert unicode str to ascii str:

''.join([chr(ord(x)) for x in request.session['iv']]) 
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.