[英]Decrypt MySQL fields that were encrypted using Java
我有一些使用以下Java方法加密/加密的String字段:
public class Encryptor {
private static final String ALGORITHM = "AES";
private static final byte[] keyValue =
new byte[] { 'M', 'y', 'S', 'u', 'p', 'e', 'r', 'S',
'e', 'c', 'r', 'e', 't', 'K', 'e', 'y' };
public static String encrypt(String valueToEnc) throws Exception {
Key key = generateKey();
Cipher cipher = Cipher.getInstance(ALGORITHM);
cipher.init(Cipher.ENCRYPT_MODE, key);
byte[] encValue = cipher.doFinal(valueToEnc.getBytes());
return new BASE64Encoder().encode(encValue);
}
public static String decrypt(String encryptedValue) throws Exception {
Key key = generateKey();
Cipher cipher = Cipher.getInstance(ALGORITHM);
cipher.init(Cipher.DECRYPT_MODE, key);
byte[] decodedValue = new BASE64Decoder().decodeBuffer(encryptedValue);
byte[] decValue = cipher.doFinal(decodedValue);
return new String(decValue);
}
private static Key generateKey() throws Exception {
return new SecretKeySpec(keyValue, ALGORITHM);
}
}
通過調用相應的方法,這些方法可以按要求工作,以進行加密和解密。 我將加密的字符串保留到MySQL數據庫中。 我想偶爾在MySQL命令行中解密字符串。 我一直在嘗試使用
SELECT AES_DECRPYT(encrypted_field, MySuperSecretKey), FROM table;
但是,這將為該列中的所有字段返回null
結果。 我可以解密MySQL中未加密的AES加密字段嗎?
看來我遇到的問題是我忘記了我用來在Java端加密字符串的BASE64Encoder
。 我可以使用以下MySQL命令獲取正確的數據:
SELECT AES_DECRYPT(FROM_BASE64(encrypted_field), 'MySuperSecretKey') FROM table;
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.