java对称解密问题

 //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;
}</pre><br /><br />调用过程<br /><pre name="code" class="java">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 size
at 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产生的密钥对,它把整个对象都包装在一起再返回的,所以用它来封装密钥是有问题的
问题补充
进行两天的努力已经把问题得到解决,学到的东西还真不少
查看全部
fang4084
fang4084
2011/02/28 10:26
  • it技术
  • 互联网问答
  • IT行业问题
  • 编程语言问答
  • 计算机技术
  • 点赞
  • 收藏
  • 回答
    私信
满意答案
查看全部

0个回复