AES解包密钥报错---在线等
public static void main(String[] args){KeyGenerator kg;
SecretKey sk ;
Cipher cipher = null;
String str = "程度上发";
Key publicKey = null;
Security.addProvider(new com.sun.crypto.provider.SunJCE());
try {
kg = KeyGenerator.getInstance("AES");
Key sqk = kg.generateKey();
cipher = Cipher.getInstance("AES");
cipher.init(Cipher.WRAP_MODE, sqk);
byte[] dd = cipher.wrap(sqk);
cipher.init(Cipher.ENCRYPT_MODE, sqk);
byte[] strbyte = cipher.doFinal(str.getBytes());
System.out.println("dd:"+dd.length);
Cipher cipher1 = Cipher.getInstance("AES");
KeyGenerator kg1 = KeyGenerator.getInstance("AES");
Key umkey = cipher1.unwrap(dd, "AES", Cipher.SECRET_KEY);
System.out.println(new String(cipher1.doFinal(strbyte)));
} catch (NoSuchAlgorithmException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (NoSuchPaddingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InvalidKeyException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalBlockSizeException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
运行后报:java.lang.IllegalStateException: Cipher not initialized
at javax.crypto.Cipher.unwrap(DashoA13*..)
at AESTest.main(AESTest.java:52) 接收端怎么初始化Cipher ? 没有Key对象Cipher也能初始化的吗? 大大能帮我解决这个问题吧。。。。。。啊,急啊。 --------------------编程问答-------------------- 看看这个
http://blog.163.com/mr_zyf/blog/static/60242161201172803323816/ --------------------编程问答-------------------- 按照你的代码的话,有一句应该这么写:
Cipher cipher1 = Cipher.getInstance("AES/CBC/PKCS5Padding");
补充:Java , Java EE