RSA加密解密以及秘钥的生成 Posted on 2016-01-14 | In programming | 12345678910111213141516171819202122232425262728293031323334import javax.crypto.Cipher;import java.security.KeyPair;import java.security.KeyPairGenerator; public class Demo { public static void main(String... args) throws Exception { d(); } public static void d() throws Exception { //生成密钥对 KeyPairGenerator keyGen = KeyPairGenerator.getInstance("RSA"); keyGen.initialize(2048); KeyPair key = keyGen.generateKeyPair(); // 公钥 byte[] pub = key.getPublic().getEncoded(); // 私钥 byte[] pri = key.getPrivate().getEncoded(); // 原文 byte[] plainText = "你看看".getBytes(); //加密工具 Cipher c1 = Cipher.getInstance("RSA"); c1.init(Cipher.ENCRYPT_MODE, key.getPrivate()); byte[] cipherText = c1.doFinal(plainText); c1.init(Cipher.DECRYPT_MODE, key.getPublic()); byte[] output = c1.doFinal(cipherText); }}