通过公钥转换为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;
}
}