热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

微信小程序java解密报异常Keylengthnot128/192/256bits

异常12345678org.bouncycastle.jcajce.provider.symmetric.util.BaseBlockCipher$InvalidKeyOrParametersExce

异常



1
2
3
4
5
6
7
8
org.bouncycastle.jcajce.provider.symmetric.util.BaseBlockCipher$InvalidKeyOrParametersException: Key length not 128/192/256 bits.

    at org.bouncycastle.jcajce.provider.symmetric.util.BaseBlockCipher.engineInit(Unknown Source)

    at javax.crypto.Cipher.implInit(Cipher.java:806)

    at javax.crypto.Cipher.chooseProvider(Cipher.java:864)

    at javax.crypto.Cipher.init(Cipher.java:1396)

    at javax.crypto.Cipher.init(Cipher.java:1327)

    at com.cust.wechat.utils.AesCbcUtil.decrypt(AesCbcUtil.java:62)

    at com.cust.wechat.utils.AesCbcUtil.main(AesCbcUtil.java:117)

代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
public static void main(String[] args) throws Exception

    {

        //这个会报异常

        String sessiOnKey= "o-kJW4_3AVXz5s96JgUkcBE4M3LA";

        String encryptedData = "JDIIqASgtHXbNkolc7j0cV4f3bakhZMj/Z2gbxJtCCFmzJmbZeUXKujEb4DEAhfrHH90qhVMpKTB+SAAjdicjT7waWtgexWfipIgZS2FREJvMmdNsp/ahhtn68joAhZze458X0YFQ4i01/Y5UFr1XktllN7C+LSb5zVqOzPGG20zER5TtB1XaZig1PmOF4CqO3A7ZV7nUnr0itIDhSXPZhQqJCNwd93EKyi7lVnCBSOF6Fqb3Wp6na3KrKP1lON1UV5g8DDZRJFOpsvVMMP6KNdy8Sf5DTn9PE7+XcZ2yVwiIPBihsA0iPMlv3jhOnzhuEtz7M4+02f0V1MNS31kZuvQb0NnwGGwRb8JrlTF1c70InLMUvgRG66gFE7otx7v/aHvrv02MitbjgL5Lg76O8Og6Tm1/gFkQaPoz4F1AQ+SNTnjgRYSFHwT2AQXusG2hJ678r8+kcUTR1tPRSrc+zBxNoEA3SPNgDZEFE557FI=";

        String iv = "dqNtltSAZGV9Q+O9Hh8mLg==";

        //这个不会报异常

        String sessionKey1 = "tiihtNczf5v6AKRyjwEUhQ==";

        String encryptedData1 =     "CiyLU1Aw2KjvrjMdj8YKliAjtP4gsMZMQmRzooG2xrDcvSnxIMXFufNstNGTyaGS9uT5geRa0W4oTOb1WT7fJlAC+oNPdbB+3hVbJSRgv+4lGOETKUQz6OYStslQ142dNCuabNPGBzlooOmB231qMM85d2/fV6ChevvXvQP8Hkue1poOFtnEtpyxVLW1zAo6/1Xx1COxFvrc2d7UL/lmHInNlxuacJXwu0fjpXfz/YqYzBIBzD6WUfTIF9GRHpOn/Hz7saL8xz+W//FRAUid1OksQaQx4CMs8LOddcQhULW4ucetDf96JcR3g0gfRK4PC7E/r7Z6xNrXd2UIeorGj5Ef7b1pJAYB6Y5anaHqZ9J6nKEBvB4DnNLIVWSgARns/8wR2SiRS7MNACwTyrGvt9ts8p12PKFdlqYTopNHR1Vf7XjfhQlVsAJdNiKdYmYVoKlaRv85IfVunYzO0IKXsyl7JCUjCpoG20f0a04COwfneQAGGwd5oa+T8yO5hzuyDb/XcxxmK01EpqOyuxINew==";

        String iv1 = "r7BXXKkLb8qrSNn05n0qiA==";

        System.out.println(AesCbcUtil.decrypt(encryptedData1, sessionKey1, iv1, "UTF-8"));

    }

public static String decrypt(String data, String key, String iv, String encodingFormat) throws Exception

    {

        //initialize();

        //被加密的数据

        byte[] dataByte = Base64.decodeBase64(data);

        //加密秘钥

        byte[] keyByte = Base64.decodeBase64(key);

        //偏移量

        byte[] ivByte = Base64.decodeBase64(iv);

        try

        {

            Cipher cipher = Cipher.getInstance("AES/CBC/PKCS7Padding");

            SecretKeySpec spec = new SecretKeySpec(keyByte, "AES");

            //生成iv

            AlgorithmParameters parameters = AlgorithmParameters.getInstance("AES");

            parameters.init(new IvParameterSpec(ivByte));

            // 初始化

            cipher.init(Cipher.DECRYPT_MODE, spec, parameters);

            byte[] resultByte = cipher.doFinal(dataByte);

            if (null != resultByte && resultByte.length > 0)

            {

                String result = new String(resultByte, encodingFormat);

                return result;

            }

            return null;

        }

        catch (NoSuchAlgorithmException e)

        {

            e.printStackTrace();

        }

        catch (NoSuchPaddingException e)

        {

            e.printStackTrace();

        }

        catch (InvalidParameterSpecException e)

        {

            e.printStackTrace();

        }

        catch (InvalidKeyException e)

        {

            e.printStackTrace();

        }

        catch (InvalidAlgorithmParameterException e)

        {

            e.printStackTrace();

        }

        catch (IllegalBlockSizeException e)

        {

            e.printStackTrace();

        }

        catch (BadPaddingException e)

        {

            e.printStackTrace();

        }

        catch (UnsupportedEncodingException e)

        {

            e.printStackTrace();

        }

        return null;

    }



   



推荐阅读
author-avatar
楼_市早班车_954
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有