I am working on PostgreSQL and getting below error during update statement execution from java code.
ERROR: invalid byte sequence for encoding "UTF8": 0x00
The code snippet for password encrption is :
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();
Encrypt function is as below:
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;
}
I have checked client_encoding
by show client_encoding
command and it is showing UTF-8.
Can someone give me pointer on resolving this issue? I have gone through the suggestions provided in other threads but none helped.