RSA根据modules和exponent公私钥转换

通过公钥转换为modules和exponent。

代码如下:

import org.apache.commons.codec.binary.Base64;
import org.apache.tomcat.util.json.JSONParser;

import java.math.BigInteger;
import java.security.KeyFactory;
import java.security.NoSuchAlgorithmException;
import java.security.PrivateKey;
import java.security.PublicKey;
import java.security.interfaces.RSAPublicKey;
import java.security.spec.InvalidKeySpecException;
import java.security.spec.RSAPrivateKeySpec;
import java.security.spec.RSAPublicKeySpec;
import java.security.spec.X509EncodedKeySpec;

public class TEST {
    public static void main(String[] args) throws Exception {
        String modulusStr = "设置modulus";
        String exponentStr = "设置exponent";
        String publicKey = modulus_exponent_to_publicKey(modulusStr,exponentStr);
        System.out.println(publicKey);
        String privateKey = modulus_exponent_to_privateKey(modulusStr,exponentStr);
        System.out.println(privateKey);
        JSONParser jsonParser = publicKey_setModulus(publicKey);
        System.out.println(jsonParser.object());
         RSAPublicKey rsaPublicKey=getPublicKey(publicKey);
        System.out.println(rsaPublicKey.getModulus());
    }
    // 通过Base64私钥生成私钥的modules 和 exponent
    public static RSAPrivateKey getPrivateKey(String base64EncodePrivateKey) throws NoSuchAlgorithmException, InvalidKeySpecException {
        byte[] bytes = Base64.decodeBase64(base64EncodePrivateKey);
        // Only RSAPrivate(Crt)KeySpec and PKCS8EncodedKeySpec supported for RSA private keys
        // 私钥仅支持 RSAPrivate(Crt)KeySpec 和 PKCS8EncodedKeySpec
        PKCS8EncodedKeySpec keySpec = new PKCS8EncodedKeySpec(bytes);
        KeyFactory keyFactory = KeyFactory.getInstance("RSA");
        return (RSAPrivateKey) keyFactory.generatePrivate(keySpec);
    }
    // 通过Base64公钥生成公钥的modules 和 exponent
    public static RSAPublicKey getPublicKey(String base64EncodePublicKey) throws NoSuchAlgorithmException, InvalidKeySpecException {
        byte[] bytes = Base64.decodeBase64(base64EncodePublicKey);
        // Only RSAPublicKeySpec and X509EncodedKeySpec supported for RSA public keys
        // 公钥仅支持 RSAPublicKeySpec 和 X509EncodedKeySpec
        X509EncodedKeySpec keySpec = new X509EncodedKeySpec(bytes);
        KeyFactory keyFactory = KeyFactory.getInstance("RSA");
        return (RSAPublicKey) keyFactory.generatePublic(keySpec);
    }
//    根据公钥的modules 和 publicExponent 还原公钥
    public static String modulus_exponent_to_publicKey(String modulusStr,String exponentStr) throws NoSuchAlgorithmException, InvalidKeySpecException {
        BigInteger modulus = new BigInteger(modulusStr);
        BigInteger exponent = new BigInteger(exponentStr);
        RSAPublicKeySpec keySpec = new RSAPublicKeySpec(modulus, exponent);
        KeyFactory keyFactory = KeyFactory.getInstance("RSA");
        PublicKey publicKey = keyFactory.generatePublic(keySpec);
        return new String(Base64.encodeBase64(publicKey.getEncoded()));
    }
//    根据私钥的modules 和 privateExponent 还原私钥
    public static String modulus_exponent_to_privateKey(String modulusStr,String exponentStr) throws NoSuchAlgorithmException, InvalidKeySpecException {
        BigInteger modulus = new BigInteger(modulusStr);
        BigInteger exponent = new BigInteger(exponentStr);
        RSAPrivateKeySpec keySpec = new RSAPrivateKeySpec(modulus, exponent);
        KeyFactory keyFactory = KeyFactory.getInstance("RSA");
        PrivateKey privateKey = keyFactory.generatePrivate(keySpec);
        return new String(Base64.encodeBase64(privateKey.getEncoded()));
    }
//    根据公钥生成的modules 和 exponent
    public static JSONParser publicKey_setModulus(String publicKey) throws Exception {

        byte[] publicKeyBytes = Base64.decodeBase64(publicKey);
        X509EncodedKeySpec keySpec = new X509EncodedKeySpec(publicKeyBytes);
        KeyFactory keyFactory = KeyFactory.getInstance("RSA");
        RSAPublicKey rsaPublicKey = (RSAPublicKey)keyFactory.generatePublic(keySpec);
        BigInteger modulus = rsaPublicKey.getModulus();
        BigInteger publicExponent = rsaPublicKey.getPublicExponent();
        String jsonstr = "{\"modulus\":"+modulus+",\"exponent\":"+publicExponent+"}";
        System.out.println(modulus);
        System.out.println(publicExponent);
        JSONParser jsonParser = new JSONParser(jsonstr);
        return jsonParser;

    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

K2epUp

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值