Re: aes_encrypted string doesn't match original after aes_decrypt
Posted by: Chris Russell
Date: October 10, 2013 11:10AM

Hi Alex,
I fixed my issue. The problem was that I was assigning the result of the aes_encrypt to a string and then passing that string to aes_decrypt and that didn't work.

The result of aes_encrypt is a byte[]. Once I made those changes I encrypt and decrypt correctly.

In a nutshell here is the working code:
Encrypt:
String stmt = "{? = CALL AES_ENCRYPT(?, ?)}";
CallableStatement cstmt = conn.prepareCall(stmt);
cstmt.setString(2, clearString);
cstmt.setString(3, AES_KEY);
cstmt.registerOutParameter(1, Types.VARBINARY);
cstmt.execute();
byte[] varbinary = cstmt.getBytes(1);
return varbinary;

Decrypt (encString is a byte[])
String stmt = "{? = CALL AES_DECRYPT(?, ?)}";
CallableStatement cstmt = conn.prepareCall(stmt);
cstmt.setBytes(2, encString);
cstmt.setString(3, AES_KEY);
cstmt.registerOutParameter(1, Types.VARCHAR);
cstmt.execute();
return cstmt.getString(1);

Thanks,
Chris

Options: ReplyQuote


Subject
Written By
Posted
Re: aes_encrypted string doesn't match original after aes_decrypt
October 10, 2013 11:10AM


Sorry, you can't reply to this topic. It has been closed.

Content reproduced on this site is the property of the respective copyright holders. It is not reviewed in advance by Oracle and does not necessarily represent the opinion of Oracle or any other party.