Rsa -- java

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值