java decryption compatible with sjcl throws error - mac check in CCM failed

912 Views Asked by At

I am trying to decrypt on java an encrypted message that I received from client that uses sjcl for encriptyon I use sjcl with default parameters

here is what I have so far

    public static void decrypt(){

    try {
        String salt="yIv/YH1+pBs=";
        String iv="xmzopU9UqwM=";
        String password="password";
        String cipherText="5xy76uy4oOIHA8PKeD3Uyjp9ex6zh449";
        //Security.setProperty("crypto.policy", "unlimited");

        SecretKeyFactory factory = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA256");
        KeySpec spec = new PBEKeySpec(password.toCharArray(), salt.getBytes(), 10000, 128);
        SecretKey tmp = factory.generateSecret(spec);
        SecretKey secret = new SecretKeySpec(tmp.getEncoded(), "AES");

        Cipher cipher = Cipher.getInstance("AES/CCM/NoPadding",new BouncyCastleProvider());
        cipher.init(Cipher.DECRYPT_MODE, secret, new IvParameterSpec(iv.getBytes()));
        cipher.doFinal(cipherText.getBytes());

        System.out.println(cipher.doFinal(cipherText.getBytes()));

    }catch(Exception e) {
        e.printStackTrace();
    }

}

from calling this method I get the next exception

javax.crypto.AEADBadTagException: mac check in CCM failed
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
at java.lang.reflect.Constructor.newInstance(Unknown Source)
at org.bouncycastle.jcajce.provider.symmetric.util.BaseBlockCipher$AEADGenericBlockCipher.doFinal(Unknown Source)
at org.bouncycastle.jcajce.provider.symmetric.util.BaseBlockCipher.engineDoFinal(Unknown Source)
at javax.crypto.Cipher.doFinal(Cipher.java:2165)
at probaDeMicrofon.AdvancedEncryptionStandard.decrypt(AdvancedEncryptionStandard.java:84)
at probaDeMicrofon.AdvancedEncryptionStandard.main(AdvancedEncryptionStandard.java:115)

Can anyone help me please?

0

There are 0 best solutions below