Mysql aes_decrypt在解密值的开头返回奇怪的字符
我有两种方法,一种是加密数据,另一种是解密数据,在后端一切正常,但如果我试图直接在mysql中解密数据,结果的开头会出现奇怪的字符 方法:Mysql aes_decrypt在解密值的开头返回奇怪的字符,mysql,spring-boot,hibernate,jpa,encryption,Mysql,Spring Boot,Hibernate,Jpa,Encryption,我有两种方法,一种是加密数据,另一种是解密数据,在后端一切正常,但如果我试图直接在mysql中解密数据,结果的开头会出现奇怪的字符 方法: public CustomAttributeConverter(@Value("${database.privateKey}") String privateKey) throws Exception { encryptionKey = privateKey.getBytes(); CustomAttributeConve
public CustomAttributeConverter(@Value("${database.privateKey}") String privateKey) throws Exception {
encryptionKey = privateKey.getBytes();
CustomAttributeConverter.setPrivateKey(privateKey);
Key key = new SecretKeySpec(encryptionKey, AES);
encryptCipher = Cipher.getInstance(AES);
encryptCipher.init(Cipher.ENCRYPT_MODE, key);
decryptCipher = Cipher.getInstance(AES);
decryptCipher.init(Cipher.DECRYPT_MODE, key);
}
@Override
public String convertToDatabaseColumn(Object attribute) {
if (attribute != null) {
try {
return Base64.getEncoder().encodeToString(encryptCipher.doFinal(SerializationUtils.serialize(attribute)));
} catch (IllegalBlockSizeException | BadPaddingException e) {
throw new IllegalArgumentException(e);
}
} else return null;
}
@Override
public Object convertToEntityAttribute(String dbData) {
if (hasText(dbData)) {
try {
return SerializationUtils.deserialize(decryptCipher.doFinal(Base64.getDecoder().decode(dbData)));
} catch (IllegalBlockSizeException | BadPaddingException e) {
throw new IllegalArgumentException(e);
}
} else return null;
}
例如,如果我运行此查询:
SELECT CONVERT(CAST(AES_DECRYPT(FROM_BASE64(amount), 'keyValue') AS char) using utf8) from table1;
我得到这样的东西:
'??\0t\0!test@itsmartsystems.eu'
为什么一开始我会得到这个部分:'??\0t\0!?加密有问题吗?发布的代码没有显示密码对象是如何实例化和初始化的。可能使用CBC。损坏的开头可能是SQL语句中的错误IV(可能还必须指定模式),请参阅。你必须做一点实验。我添加了初始化密码的方法。我看到它使用ecb模式,我没有任何IVYes,代码使用ecb模式,它不应用IV,所以错误的IV不能成为原因。由于解密失败,
amount
的内容不应与使用convertToDatabaseColumn()
写入数据库的内容相同。也许这种差异可以让我们得出进一步的结论。