问题描述
//encodedata BASE64编码后加密密文 //key BASE64编码后对称密钥对 //algorithmName 加密算法 public static String decodeforp1(String encodedata,String key,String algorithmName){ try {SecretKeySpec k = new SecretKeySpec(Base64.decode(key.getBytes()),algorithmName); //解密 Cipher cp = Cipher.getInstance(algorithmName); cp.init(Cipher.DECRYPT_MODE, k); byte[] ptext = cp.doFinal(Base64.decode(encodedata.getBytes())); String s = new String(Base64.encode(ptext));return s;} catch (InvalidKeyException e) {e.printStackTrace();} catch (NoSuchAlgorithmException e) {e.printStackTrace();} catch (NoSuchPaddingException e) {e.printStackTrace();} catch (IllegalBlockSizeException e) {e.printStackTrace();} catch (BadPaddingException e) {e.printStackTrace();}return null; }调用过程String key="HK6Jy6OrfB7ZkZg6WD/rQIZfNeFn+nrrXQCTfN7Cv3cAADoAAAAAAOPCv3cAAAAAAM35CFzU/wsIAAAAVNT/C8LWWgwUwXwDkMB8A+GrWgzEwXwDlFzAd3AgvncAAAAAZAYAAK8nVQwUwXwDZAAAAEAAAABn0/8LXNT/C9PAWgwUwXwDQAAAAATBfAMJTRN4SLuBCnzBfAMAAAAAEAAAAAAAAAAAAAAAAAAAAAAAAABEWnGJorzX8xAuTW1+kKO3zOL5ESpEX3uYttX1BhgrP1RqgZmyzOcDID5dfY6gs8fc8gkhOlRvi6jG5QUWKDtPO7f8CyC1YQxQvmEMAAAAAASCwBckn0F6gAAAAGjT/ws=";String encodedata="MWY4OWQ5M2ZmMWFiNzk5N2VlOGViYzgwZGFlMDZhNTExYjBiNzk4Yd4j2eRQnsXNu5BWFsvJGXw=";String algorithmName="DESede";String date=encodeforp1(data, key, algorithmName);System.out.println(date);但会报错java.security.InvalidKeyException: Wrong key sizeat com.sun.crypto.provider.SunJCE_y.a(DashoA13*..)at com.sun.crypto.provider.SunJCE_g.a(DashoA13*..)at com.sun.crypto.provider.SunJCE_f.a(DashoA13*..)at com.sun.crypto.provider.SunJCE_f.a(DashoA13*..)at com.sun.crypto.provider.DESedeCipher.engineInit(DashoA13*..)at javax.crypto.Cipher.a(DashoA13*..)at javax.crypto.Cipher.a(DashoA13*..)at javax.crypto.Cipher.init(DashoA13*..)at javax.crypto.Cipher.init(DashoA13*..)at com.szca.authenti.util.CAUtil.encodeforp1(CAUtil.java:67)at com.szca.authenti.util.Test.main(Test.java:61)求解问题补充没人自己顶下问题补充已经找出原因了 JDK对DESede算法密钥对长度是112位和168位 而我的这个密钥对是192位的问题补充谢谢 fastbo问题补充而且KEY也有很大的问题,KEY是C产生的密钥对,它把整个对象都包装在一起再返回的,所以用它来封装密钥是有问题的问题补充进行两天的努力已经把问题得到解决,学到的东西还真不少
解决方案
引用java.security.InvalidKeyException: Wrong key size这里不是很明显写着原因吗?错误的key大小。我以前做过一个AES-128-CBC加密模式的加解密的东西,key需要为16位,程序里还做了判断。DES算法里的key位数估计也是有规定的。代码供你参考。// 解密public static String decrypt(String sSrc, String sKey) throws Exception {try {// 判断Key是否正确if (sKey == null) {System.out.print("Key为空null");return null;}// 判断Key是否为16位if (sKey.length() != 16) {System.out.print("Key长度不是16位");return null;}byte[] raw = sKey.getBytes("ASCII");SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES");Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");IvParameterSpec iv = new IvParameterSpec("0102030405060708".getBytes());cipher.init(Cipher.DECRYPT_MODE, skeySpec, iv);byte[] encrypted1 = new BASE64Decoder().decodeBuffer(sSrc);// 先用base64解密try {byte[] original = cipher.doFinal(encrypted1);String originalString = new String(original);return originalString;} catch (Exception e) {System.out.println(e.toString());return null;}} catch (Exception ex) {System.out.println(ex.toString());return null;}}