简体   繁体   中英

Decrypt MySQL fields created using Java AESBouncyCastle

I have method to encrypt and decrypt in Java. How can I reproduce using MySQL command?

public static String encrypt(String plainString) throws Exception{
    byte[] encr  = AESBouncyCastle.encrypt(plainString.getBytes("UTF-8"), "secretKey");
    String encryptedString= Hex.encodeHexString(encr);
    return encryptedString;
}

public static String decrypt(String encryptedString) throws Exception{      
    byte[] retr = AESBouncyCastle.decrypt(Hex.decodeHex(encryptedString.toCharArray()), "secretKey");
    String decryptedString = new String(retr, "UTF-8");
    return decryptedString .trim();
}

Any idea where I'm doing wrong? Ultimately I want to see whether I can use AES_DECRYPT function in MySQL to decrypt the string.

UPDATE

First I need to figure out Mysql equivalent for Java's Hex.decodeHex. We are using Apache's common codec library.

I tried UNHEX in MySQL and did the same in Java, but the results are different.

When I did UNHEX('956cafe431f5bfc0e66ca7d89359d2e9b63060a3ed5793e60c67aacaca43117c') it returns me something like this •l¯ä1õ¿Àæl§Ø“YÒé¶0`£íW“ægªÊÊC| . Whereas Java returns something like this [B@32267cbc for Hex.decodeHex("956cafe431f5bfc0e66ca7d89359d2e9b63060a3ed5793e60c67aacaca43117c".toCharArray())

Ofcourse, you can use AES_DECRYPT function in MySQL to decrypt the string.

linke here : http://dev.mysql.com/doc/refman/5.7/en/encryption-functions.html

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM