package gaofeng.netconf;
import java.security.KeyFactory;
import java.security.KeyPair;
import java.security.KeyPairGenerator;
import java.security.NoSuchAlgorithmException;
import java.security.PrivateKey;
import java.security.PublicKey;
import java.security.interfaces.RSAPrivateKey;
import java.security.interfaces.RSAPublicKey;
import java.security.spec.InvalidKeySpecException;
import java.security.spec.PKCS8EncodedKeySpec;
import java.security.spec.X509EncodedKeySpec;
import java.util.Base64;
import javax.crypto.Cipher;
public class Rsa {
public static void generateKeyPair() throws NoSuchAlgorithmException {
KeyPairGenerator keyPairGen = KeyPairGenerator.getInstance("RSA");
keyPairGen.initialize(1024);//密钥位数
//By default, the private key is generated in PKCS#8 format and the public key is generated in X.509 format.
KeyPair keyPair = keyPairGen.generateKeyPair();//密钥对
PublicKey publicKey = (RSAPublicKey) keyPair.getPublic(); // 公钥
PrivateKey privateKey = (RSAPrivateKey) keyPair.getPrivate();// 私钥
System.out.println(Base64.getEncoder().encodeToString(publicKey.getEncoded()));
System.out.println(Base64.getEncoder().encodeToString(privateKey.getEncoded()));
}
public static PublicKey getPublicKey(String base64PublicKey) throws NoSuchAlgorithmException, InvalidKeySpecException{
X509EncodedKeySpec keySpec = new X509EncodedKeySpec(Base64.getDecoder().decode(base64PublicKey.getBytes()));
KeyFactory keyFactory = KeyFactory.getInstance("RSA");
PublicKey publicKey = keyFactory.generatePublic(keySpec);
return publicKey;
}
public static PrivateKey getPrivateKey(String base64PrivateKey) throws NoSuchAlgorithmException, InvalidKeySpecException{
PKCS8EncodedKeySpec keySpec = new PKCS8EncodedKeySpec(Base64.getDecoder().decode(base64PrivateKey.getBytes()));
KeyFactory keyFactory = KeyFactory.getInstance("RSA");
PrivateKey privateKey = keyFactory.generatePrivate(keySpec);
return privateKey;
}
public static void main(String[] args) throws Exception {
// generateKeyPair();
String base64PrivateKey="MIICdQIBADANBgkqhkiG9w0BAQEFAASCAl8wggJbAgEAAoGBALecV9WyOFE/Q50z9up9VHYH0sMsW5/R7aOsfz1n4asiTpWREWSSAy1rT8Il9NI6REs7xed26+1H8iZynk/rJ5inJU3CY0tFzMoa8dktRy5MkZv1Lixw1GGyY6q7EeYy5xV1bH7kEJbjkgi1Vf7kKsS5f0Oxn7oZ00nItkSY+pNdAgMBAAECgYBY3XNpewtkz1DDOg5MaYJCagDo84QY8vqoF+mxZ6DCOiV8oCyh10Vwaxq8JbC7OvYoz3I2V8lSG1sonprepTqRBd0GAJiwgK8ct0y+midyS2ookZ8QKiliUdeyvHJzDgOL9GmBdVIyTJG8GmRcBDGIpuzzp48yDYAh2iXNPfXt4QJBAOwNOJieNEct/jVVgr8nD3HdlCGpG8uztHKvpflHlf2OWBniMnDJlaChmP0HXyYYY8J37OPZqxq220PCzNQNuRUCQQDHIJtJuZaJHIumQjokgKkiOUGwJ/xBlcwgTJ4zAcnXPEQRn5tWHr4OpdLm4WGmFrrvcfuffxpEEz6FBLZP9fMpAkAuny3eduC8bkkXH5yDRKO4SZtISq5CvWuWezq80Xa+ba7Y7eQogqga4c/lqd8RFyAc+LLBDezNifAkogTynQNBAkAZNN9zbbLImHx1B10k0cSXOHhAPE+KXdQBIrHD8y4BuPdE49deB3Y3lHYmE9GwnJ/9P4iCoCxLAcSgQgByDshBAkAigRl10dRrPzLSMFAa1qmYQPnFjL3Saa/rmPjOVrXPhMdu7Zh0u9B+rFKxs4xIlQhxdvr2X+3bMG8L5VdQUkf4";
String base64PublicKey="MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQC3nFfVsjhRP0OdM/bqfVR2B9LDLFuf0e2jrH89Z+GrIk6VkRFkkgMta0/CJfTSOkRLO8XnduvtR/Imcp5P6yeYpyVNwmNLRczKGvHZLUcuTJGb9S4scNRhsmOquxHmMucVdWx+5BCW45IItVX+5CrEuX9DsZ+6GdNJyLZEmPqTXQIDAQAB";
Cipher cipher = Cipher.getInstance("RSA/ECB/PKCS1Padding");
String data = "Dhiraj is the author567777777777565444444444444444444444444444448888888888888888888889999999999999999999999999999999";
cipher.init(Cipher.ENCRYPT_MODE, getPrivateKey( base64PrivateKey) );
byte[] encodeString = cipher.doFinal(data.getBytes());
System.out.println(Base64.getEncoder().encodeToString(encodeString));
cipher.init(Cipher.DECRYPT_MODE, getPublicKey( base64PublicKey));
byte[] plan = cipher.doFinal(encodeString);
System.out.println(new String(plan));
}
}
UuKyphNJreXqKfRNVhwZHdusekkPBpm8GlxRxZRMTeuZN2o/Ptvx4xcgA/di18KOAl7sNh9f8rx2EMaOTfyNO6CPu3QzPlSKer06K263WeAEqpL2/CRmGDMmfTTXw292y+feF6yMDdja3fM0eS2oBymTTieYPr13irYht3/5Mi0=
Dhiraj is the author567777777777565444444444444444444444444444448888888888888888888889999999999999999999999999999999
Rsa -- java
最新推荐文章于 2024-09-01 07:36:40 发布