MySQl数据库使⽤AES加密解密配Java代码使⽤MySQl数据库加密解密
待加密字段varchar
INSERT INTO  base_info(id,school_db_host) VALUES('1111254',HEX(AES_ENCRYPT(('要加密的'),'coco') ));
SELECT AES_DECRYPT(UNHEX(school_db_host), 'coco') FROM base_info;
待加密字段varbinary
INSERT INTO  base_info(id,school_db_host) VALUES('111254',AES_ENCRYPT(('要加密的'),'coco'));
SELECT AES_DECRYPT(school_db_host, 'coco') FROM base_info;
Java代码加密解密配合使⽤
import java.io.UnsupportedEncodingException;
pto.Cipher;
pto.spec.SecretKeySpec;
import dec.binary.Hex;
/**通常mysql中AES加密,和java中不⼀致,
* 也就是说mysql中AES加密的东西,copy出来,
* ⽤java代码不能解密
* 下⾯⼤佬就来秀⼀波操作
* */
public class AESUtil {
/**加密的Key*/
private static String  AESKEY = "zhfw_700";
/**同意是⽤的编码*/
private static String AESCODE  = "UTF-8";
public static SecretKeySpec generateMySQLAESKey(final String key, final String encoding) {
try {
final byte[] finalKey = new byte[16];
int i = 0;
for(byte b : Bytes(encoding))
finalKey[i++%16] ^= b;
return new SecretKeySpec(finalKey, "AES");
} catch(UnsupportedEncodingException e) {
throw new RuntimeException(e);
}
}
/
**AES 解密
* data : 待解密的数据
* */
java源代码加密public static String decrpt(String data)  throws Exception {
// Decrypt
final Cipher decryptCipher = Instance("AES");
decryptCipher.init(Cipher.DECRYPT_MODE, generateMySQLAESKey(AESKEY, AESCODE));
return new String(decryptCipher.doFinal(Hex.CharArray())));
}
/**AES加密
* data : 待加密的数据
* */
public static String encrpt(String data)  throws Exception {
// Encrypt
final Cipher encryptCipher = Instance("AES");
final Cipher encryptCipher = Instance("AES");
encryptCipher.init(Cipher.ENCRYPT_MODE, generateMySQLAESKey(AESKEY, AESCODE));
char[] code=  deHex(encryptCipher.Bytes(AESCODE)));
StringBuilder builder = new StringBuilder();
for(char d:code) {
builder.append(d);
}
String strning = String();
return strning;
}
public static void args) throws Exception {
// Decrypt
//        final Cipher decryptCipher = Instance("AES");
//        decryptCipher.init(Cipher.DECRYPT_MODE, generateMySQLAESKey("coco", "UTF-8"));
//        System.out.println(new String(decryptCipher.doFinal(Hex.decodeHex("d1314e21a741b85e00fb975529a217ac98fab03b6fca017c0a6bf1cd621ef7ae" .toCharArray()))));
// Encrypt
//        final Cipher encryptCipher = Instance("AES");
//        encryptCipher.init(Cipher.ENCRYPT_MODE, generateMySQLAESKey("coco", "UTF-8"));
//        System.out.deHex(encryptCipher.doFinal("要加密的".getBytes("UTF-8"))));
System.out.println(encrpt("要加密的")); //加密
System.out.println(decrpt(encrpt("要加密的"))); //解密加密的数据
}
}

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。