java.security.InvalidKeyException:错误的算法:需要DESede或TripleDES

时间:2019-03-11 10:26:26

标签: java encryption

我编写了这段代码,我的密钥是:“ ooWqEPcw7KR / h / JIbrFCRHiEVaybvnB2”。

(?<!^|[A-Z\p{P}])[A-Z]|(?<=\p{P})\p{P}|(?<=\p{L})\d|(((?<!^)(?<!\p{P})[A-Z](?=[a-z]))|((?<=[a-z])[A-Z]))|(?<!^)(?=[[({&])|(?<=[)\]}!&}])

但是我得到这个错误!!!

    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();
    }

1 个答案:

答案 0 :(得分:1)

DESedeCrypt类仅根据源代码支持DESedeTripleDES算法:

void init(boolean decrypting, String algorithm, byte[] keys)
        throws InvalidKeyException {
    if (!algorithm.equalsIgnoreCase("DESede")
                && !algorithm.equalsIgnoreCase("TripleDES")) {
        throw new InvalidKeyException
            ("Wrong algorithm: DESede or TripleDES required");
    }

您的代码必须使用SecretKey中的两个之一。 Cipher可以保留为DESede/ECB/PKCS5Padding

String workingKey = "ooWqEPcw7KR/h/JIbrFCRHiEVaybvnB2";
SecretKey secretKey = new SecretKeySpec(
        Base64.getDecoder().decode(workingKey), 
        "DESede");
Cipher cipher = Cipher.getInstance("DESede/ECB/PKCS5Padding");
cipher.init(1, secretKey);