jdk 1.8
package test.mima;
import java.security.KeyFactory;
import java.security.KeyPair;
import java.security.KeyPairGenerator;
import java.security.PrivateKey;
import java.security.PublicKey;
import java.security.SecureRandom;
import java.security.spec.PKCS8EncodedKeySpec;
import java.security.spec.X509EncodedKeySpec;
import java.util.Base64;
import javax.crypto.Cipher;
public class RSATest {
public static String publicKey = "MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCBaTlrnFzuBvsDNR1B+ckEslQ6/0y73wf483MB+gZZ1NRPwvasZyuk14AhDMVFRhH+B/iICa4H6MW/tTRRKMT9oMd8CFeYLfl6ik3fJCqRemBcOI4k4a3bfWvwufaxUr2KvGcUAM3zDK2J8saxQhlD9H61P9duU8wIij/RDoAwDwIDAQAB";
public static String privateKey = "MIICdwIBADANBgkqhkiG9w0BAQEFAASCAmEwggJdAgEAAoGBAIFpOWucXO4G+wM1HUH5yQSyVDr/TLvfB/jzcwH6BlnU1E/C9qxnK6TXgCEMxUVGEf4H+IgJrgfoxb+1NFEoxP2gx3wIV5gt+XqKTd8kKpF6YFw4jiThrdt9a/C59rFSvYq8ZxQAzfMMrYnyxrFCGUP0frU/125TzAiKP9EOgDAPAgMBAAECgYA8EW+Jt6abBbp1nq3Z9euMtonKaL5hZgD5WWdEv507DUMPUtvU4n6roSvkK11V7lRjqad+3Z9eJHnFpoKGOXpV5lwBsqqrzj/pw1Gi0wO7pEVXS3nH3nwHhaMXfBxb44KCAOn2vVxNVtyS2e0kw6Oecn97PJkaJKd/rXlrn3rq4QJBAMQ6Xsap8JPk7zcxGinZb821eXeQOdsWE6rz5AuugCLM5OUuO8VVWGbmYmpwqdARZiCxNBuIvScYxfjkhMw0Nh0CQQCo1Itxk4u40OrDWMVXtScgsUBlFdt4vecdJKAgmYUKYvwwU2gtTrSs5fDYJhr2fNu49TYKhPdHJRGU0Iibg3cbAkEAvjyYhaFbSwY3pBpGcywJ6DR3fiKk9HGCv8mS3IBgWVyqzET2TWlgOuI/p1FyT/WOPUOOcOMv/idWRuXEmPRFQQJBAJbl4fyRs8Bsemg/X6MU9vPxeWzgLFmu4nLZ2YVJse+mqWFIK0TTpAklPC71YAQPdRPLl5GvZpTgPOaVvIPC7SECQC3Pp+pYjYED/XEauzoNRCmONgp6Sz7sR3y/fxEOHU1mMYzim6w3oGdjVoWugs2dakJ5Xw2/RQaF6qmQv/vhLsw=";
public static String text = "hello nihaoo1234 hello nihao 世界,";
public static void main(String[] args) throws Exception {
// TODO Auto-generated method stub
KeyPair keyPair = genKeyPair(1024);
publicKey = Base64.getEncoder().encodeToString(keyPair.getPublic().getEncoded());
privateKey = Base64.getEncoder().encodeToString(keyPair.getPrivate().getEncoded());
System.out.println("公钥:" + publicKey);
System.out.println("私钥:" + privateKey);
PublicKey publick = getPublicKey(publicKey);
PrivateKey privatek = getPrivateKey(privateKey);
String mw = encrypt(text, publick);
System.out.println(mw);
String mmw = decrypt(mw, privatek);
System.out.println(mmw);
}
// 生成密钥对
public static KeyPair genKeyPair(int keyLength) throws Exception {
// KeyPairGenerator类用于生成公钥和私钥对,基于RSA算法生成对象
KeyPairGenerator keyPairGen = KeyPairGenerator.getInstance("RSA");
// 初始化密钥对生成器,密钥大小为96-1024位
keyPairGen.initialize(keyLength, new SecureRandom());
// 生成一个密钥对,保存在keyPair中
KeyPair keyPair = keyPairGen.generateKeyPair();
return keyPair;
// KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance("RSA");
// keyPairGenerator.initialize(1024);
// return keyPairGenerator.generateKeyPair();
}
// 公钥加密
public static String encrypt(String content, PublicKey publicKey) throws Exception {
Cipher cipher = Cipher.getInstance("RSA");// java默认"RSA"="RSA/ECB/PKCS1Padding"
cipher.init(Cipher.ENCRYPT_MODE, publicKey);
byte[] data = cipher.doFinal(content.getBytes());
return Base64.getEncoder().encodeToString(data);
}
// 私钥解密
public static String decrypt(String content, PrivateKey privateKey) throws Exception {
Cipher cipher = Cipher.getInstance("RSA");
cipher.init(Cipher.DECRYPT_MODE, privateKey);
byte[] data = Base64.getDecoder().decode(content.getBytes());
return new String(cipher.doFinal(data));
}
/**
* String转公钥PublicKey
*
* @param key
* @return
* @throws Exception
*/
public static PublicKey getPublicKey(String key) throws Exception {
byte[] keyBytes;
keyBytes = Base64.getDecoder().decode(key);
X509EncodedKeySpec keySpec = new X509EncodedKeySpec(keyBytes);
KeyFactory keyFactory = KeyFactory.getInstance("RSA");
PublicKey publicKey = keyFactory.generatePublic(keySpec);
return publicKey;
}
/**
* String转私钥PrivateKey
*
* @param key
* @return
* @throws Exception
*/
public static PrivateKey getPrivateKey(String key) throws Exception {
byte[] keyBytes;
keyBytes = Base64.getDecoder().decode(key);
PKCS8EncodedKeySpec keySpec = new PKCS8EncodedKeySpec(keyBytes);
KeyFactory keyFactory = KeyFactory.getInstance("RSA");
PrivateKey privateKey = keyFactory.generatePrivate(keySpec);
return privateKey;
}
}