javaangularencryptionaessalt-cryptography

Unable to Decrypt String in angular which is Encrypted in java


I tried encrypt some message in java but when I tried to decrypt it into angular it is not giving any proper output.

what all changes need to done in angular to decrypt the message?

Here is my Java code:

public class AesBase64Wrapper{
    
    private static char[] password = "EF737CC29DAE7C80644A5B01544CBA61".toCharArray();
    private static byte iv[];
    static {
        try {             
            iv = getBytes("79994A6EF73DA76C");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    
    public static String encryptAndEncode(String raw,String salt)
    {
        try
        {
            Cipher c = getCipher(1,salt);
            byte[] encryptedVal = c.doFinal(getBytes(raw));
            return Base64.getEncoder().encodeToString(encryptedVal);
        }
        catch (Throwable t)
        {
            throw new RuntimeException(t);
        }
    }
  
    public static String decodeAndDecrypt(String encrypted,String salt) throws Exception
    {
        byte[] decodedValue = Base64.getDecoder().decode(encrypted);
        Cipher c = getCipher(2,salt);
        byte[] decValue = c.doFinal(decodedValue);
        return new String(decValue);
    }
  
    private static byte[] getBytes(String str) throws UnsupportedEncodingException
    {
        return str.getBytes("UTF-8");
    }
    
    private static Cipher getCipher(int mode,String salt)throws Exception
    {

        Cipher c = Cipher.getInstance("AES/CBC/PKCS5Padding");
        c.init(mode, generateKey(salt), new IvParameterSpec(iv));
        return c;
    }
  
  
    private static Key generateKey(String salt) throws Exception
    {
        SecretKeyFactory factory = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA1");

        byte[] saltb = getBytes(salt);
    
        KeySpec spec = new PBEKeySpec(password, saltb, 65536, 256);
        SecretKey tmp = factory.generateSecret(spec);
        return new SecretKeySpec(tmp.getEncoded(), "AES");
    }
  
    
    
}

This one is angular:

export class PrepaidCryptoService {

  Key_IV = "79994A6EF73DA76C";
  PASSWORD = "EF737CC29DAE7C80644A5B01544CBA61";
  SALT = "12345";
  finalEncryption;
  constructor() {
  }

  encoder(str) {
    let encoder = new TextEncoder();
    let byteArray = encoder.encode(str)
    return CryptoJS.enc.Utf8.parse(str)
  }

  toWordArray(str) {
    return CryptoJS.enc.Utf8.parse(str);
  }

  encrypt(value) {

      var key = CryptoJS.PBKDF2(this.PASSWORD, this.SALT, {
        keySize : 256, 
        iterations: 65536
      })
      var iv = CryptoJS.enc.Hex.parse(this.Key_IV);
      var encrypted = CryptoJS.AES.encrypt(this.encoder(value), key, {
        iv: this.toWordArray(iv),
        padding: CryptoJS.pad.Pkcs7,
        mode: CryptoJS.mode.CBC
      })

      return encrypted.ciphertext.toString(CryptoJS.enc.Base64);
    }


  decrypt(value) {
 
    var key = CryptoJS.PBKDF2(this.PASSWORD, this.SALT, {
       keySize : 256,
      iterations: 65536
    })
     var iv =  CryptoJS.enc.Utf8.parse(this.Key_IV)
    var decrypted = CryptoJS.AES.decrypt(value, key, {
      iv: this.toWordArray(iv),
      padding: CryptoJS.pad.Pkcs7,
      mode: CryptoJS.mode.CBC
    })

    return decrypted.toString(CryptoJS.enc.Utf8);

  }

}

I have tried several different ways but still this issue is coming.


Solution

  • I Just Update below methods in my angular code and it works for me :

    encrypt(value) {
        var key = CryptoJS.PBKDF2(this.PASSWORD, this.SALT, {
          keySize: 256 / 32,
          iterations: 65536
        })
        var encrypted = CryptoJS.AES.encrypt(this.encoder(value), key, {
          iv: this.toWordArray(this.Key_IV),
          padding: CryptoJS.pad.Pkcs7,
          mode: CryptoJS.mode.CBC
        })
        return encrypted.ciphertext.toString(CryptoJS.enc.Base64);
      }
    
      decrypt(value) {
        var key = CryptoJS.PBKDF2(this.PASSWORD, this.SALT, {
          keySize: 256 / 32,
          iterations: 65536
        })
        var decrypted = CryptoJS.AES.decrypt(value, key, {
          iv: this.toWordArray(this.Key_IV),
          padding: CryptoJS.pad.Pkcs7,
          mode: CryptoJS.mode.CBC
        })
        return decrypted.toString(CryptoJS.enc.Utf8);
      }
    }