fang4084
fang4084
2011-02-28 10:26
浏览 280
已采纳

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产生的密钥对,它把整个对象都包装在一起再返回的,所以用它来封装密钥是有问题的
问题补充
进行两天的努力已经把问题得到解决,学到的东西还真不少
  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • iteye_3974
    iteye_3974 2011-02-28 10:26
    已采纳

    [quote]java.security.InvalidKeyException: Wrong key size[/quote]
    这里不是很明显写着原因吗?错误的key大小。
    我以前做过一个AES-128-CBC加密模式的加解密的东西,key需要为16位,程序里还做了判断。DES算法里的key位数估计也是有规定的。代码供你参考。
    [code="java"] // 解密
    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;
    }
    }[/code]

    点赞 评论

相关推荐