I am trying to send an encrypted username from my React frontend to my Spring Boot backend. My frontend looks like this:
const secretKey = "mySecretKey";
const encrypted = CryptoJS.AES.encrypt(username, secretKey).toString();
setHashedUsername(encrypted);
Which is then sent through a GET request in the backend. The backend looks like this:
public String decryptData(String encryptedUsername) throws NoSuchAlgorithmException {
try {
byte[] keyBytes = secretKey.getBytes("UTF-8");
SecretKeySpec secretKeySpec = new SecretKeySpec(keyBytes, "AES");
Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
cipher.init(Cipher.DECRYPT_MODE, secretKeySpec);
byte[] decryptedBytes = cipher.doFinal(Base64.getDecoder().decode(encryptedUsername));
return new String(decryptedBytes,"UTF-8");
} catch (Exception e) {
return "Error";
}
}
I always get the following error while debugging:
Cipher.AES/ECB/PKCS5Padding, mode: not initialized, algorithm from: (no provider)
I tried to replace the "AES/ECB/PKCS5Padding" transformation with other variations, hoping that the original one didn't match the requirements from the frontend, but all failed. Then I also tried the following solution: What are the AES parameters used and steps performed internally by crypto-js while encrypting a message with a password? , but still didn't work. What could be the problem?