欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

[转]java生成3DES对称秘钥和RSA非对称秘钥

程序员文章站 2022-06-15 16:42:18
...
/*1、对称加密3DES秘钥生成*/
KeyGenerator kg = KeyGenerator.getInstance("DESede");
kg.init(112);//must be equal to 112 or 168
System.out.println("SecretKey:");
System.out.println(new BASE64Encoder().encode(kg.generateKey().getEncoded()));
 
/*2、非对称加密RSA公钥和私钥生成*/
KeyPairGenerator keyGen = KeyPairGenerator.getInstance("RSA");  
keyGen.initialize(512);//RSA keys must be at least 512 bits long
KeyPair key = keyGen.generateKeyPair();
System.out.println("PublicKey:");
System.out.println(new BASE64Encoder().encode(key.getPublic().getEncoded()));
System.out.println("PrivateKey:");
System.out.println(new BASE64Encoder().encode(key.getPrivate().getEncoded()));


引用