Search code examples
javaencryptioncryptography3des

java.security.InvalidKeyException: Wrong algorithm: DESede or TripleDES required


I wrote this code, my key is: "ooWqEPcw7KR/h/JIbrFCRHiEVaybvnB2".

    try
    {
        Base64Decoder base64Decoder=new Base64Decoder();

        String encryptType="DESede/ECB/PKCS5Padding";
        String workingKey="ooWqEPcw7KR/h/JIbrFCRHiEVaybvnB2";
        SecretKey secretKey=new SecretKeySpec(base64Decoder.decode(workingKey), encryptType);

        Cipher cipher=Cipher.getInstance(encryptType);
        cipher.init(1, secretKey);
    }
    catch(NoSuchAlgorithmException | NoSuchPaddingException | InvalidKeyException e)
    {
        e.printStackTrace();
    }

But I get this error!!!

java.security.InvalidKeyException: Wrong algorithm: DESede or TripleDES required
    at com.sun.crypto.provider.DESedeCrypt.init(DESedeCrypt.java:65)
    at com.sun.crypto.provider.ElectronicCodeBook.init(ElectronicCodeBook.java:93)
    at com.sun.crypto.provider.CipherCore.init(CipherCore.java:582)
    at com.sun.crypto.provider.CipherCore.init(CipherCore.java:458)
    at  com.sun.crypto.provider.DESedeCipher.engineInit(DESedeCipher.java:166)
    at javax.crypto.Cipher.implInit(Cipher.java:802)
    at javax.crypto.Cipher.chooseProvider(Cipher.java:864)
    at javax.crypto.Cipher.init(Cipher.java:1249)
    at javax.crypto.Cipher.init(Cipher.java:1186)
    at EncryptText.main(EncryptText.java:24)

Solution

  • You should write SecretKeySpec as

    SecretKey secretKey=new SecretKeySpec(base64Decoder.decode(workingKey),"DESede");
    

    Also use Cipher.ENCRYPT_MODE and Cipher.DECRYPT_MODE instead of constants.