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


/*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()));


[quote]引自:[url]http://my.oschina.net/h2do/blog/370684[/url][/quote]

你可能感兴趣的:(加密/解密,java)