Skip to Main Content

Java Programming

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Java AES Encrypt/Decrypt and MySQL

807588Apr 29 2008 — edited Jul 14 2009
hello,

I am doing AES-128 encryption on some data and storing the encrypted data into MySQL db. But the problem is when I am trying to get the data by MySQL query like -
select AES_DECRYPT(columnName,'0123456789ABCDEF') from tableName;
I am not getting the same data as query output of the encrypted data. I am confused about the problem. My encryption code look like -
public static void main(String[] args) throws Exception {
        String message = "Strong Versus Unlimited Strength Cryptography";
	SecretKeySpec skeySpec = new SecretKeySpec("0123456789ABCDEF".getBytes(), "AES"); //AES-128
				
	Cipher cipher = Cipher.getInstance("AES");
	cipher.init(Cipher.ENCRYPT_MODE, skeySpec);
				
	byte[] encrypted = cipher.doFinal(message.getBytes());
	System.out.println("encrypted string: " + encrypted); //storing into MySQL DB
				
	cipher.init(Cipher.DECRYPT_MODE, skeySpec);
	byte[] original = cipher.doFinal(encrypted);
	String originalString = new String(original);				
	System.out.println("Original string: " + originalString);
}
Can some one tell me what I am doing wrong here?
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Aug 11 2009
Added on Apr 29 2008
5 comments
4,057 views