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也能初始化的吗? 大大能帮我解决这个问题吧啊,急啊。
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也能初始化的吗? 大大能帮我解决这个问题吧啊,急啊。
解决方案 »
- Spring 3 使用-处理整个框架全局的异常
- 小弟刚刚对jsp servlet 有初步的了解,想学hibernate,structs前辈们给些建议。。
- 在struts1.X的property中能不能这样,对象属性.对象属性.对象属性
- List 转化
- 从java开发服务器角度,咨询服务器集群,负载平衡,故障排除方面的知识
- Hibernate用户自定义值类型怎么用
- 看了很多帖,一直很困惑的问题
- KPI统计问题
- bean标签的问题
- 关与static 方法的问题!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
- JDBC的驱动问题
- JEECG使用分享
http://blog.163.com/mr_zyf/blog/static/60242161201172803323816/
Cipher cipher1 = Cipher.getInstance("AES/CBC/PKCS5Padding");