【发布时间】:2025-12-16 19:40:01
【问题描述】:
我正在研究 PostgreSQL,在从 java 代码执行更新语句期间遇到错误。
ERROR: invalid byte sequence for encoding "UTF8": 0x00
密码加密的代码sn-p是:
StringBuilder encryptionKey = new StringBuilder().append(newPassword).append(userEmail).append(userEmail).append(userEmail);
AdvancedEncryptionStandard aes = new AdvancedEncryptionStandard(encryptionKey.toString().getBytes(StandardCharsets.UTF_8));
newEncryptedPwd = new String(aes.encrypt(newPassword.getBytes(StandardCharsets.UTF_8)));
EntityManager em = EntityManagerUtil.getEntityManager(schemaName);
EntityManagerUtil.beginTransaction(schemaName);
Query query = em.createQuery("UPDATE User um SET um.password=:newPassword WHERE um.loginId=:userID");
query.setParameter("userID", userName);
query.setParameter("newPassword", newEncryptedPwd);
query.executeUpdate();
加密函数如下:
public byte[] encrypt(byte[] plainText) throws Exception
{
SecretKeySpec secretKey = new SecretKeySpec(key,0,16,'AES');
Cipher cipher = Cipher.getInstance(ALGORITHM);
cipher.init(Cipher.ENCRYPT_MODE, secretKey);
return cipher.doFinal(plainText);
}
public AdvancedEncryptionStandard(byte[] key)
{
this.key = key;
}
我通过show client_encoding 命令检查了client_encoding,它显示的是UTF-8。
有人可以指点我解决这个问题吗?我已经阅读了其他线程中提供的建议,但没有任何帮助。
【问题讨论】:
标签: java encryption password-encryption