测试代码:
PrivateKey privateKey=RSAUtils.getRSAPrivateKeyBybase64(savePrivateKey);
PublicKey publicKey=RSAUtils.getRSAPublidKeyBybase64(savePublicKey);
String publicKeyXml = getRSAPublicKeyAsNetFormat(publicKey.getEncoded());
String privateKeyXml = getRSAPrivateKeyAsNetFormat(privateKey.getEncoded());
import java.security.KeyFactory;
import java.security.interfaces.RSAPrivateCrtKey;
import java.security.interfaces.RSAPublicKey;
import java.security.spec.PKCS8EncodedKeySpec;
import java.security.spec.X509EncodedKeySpec;
private static String getRSAPrivateKeyAsNetFormat(byte[] encodedPrivkey) {
try {
StringBuffer buff = new StringBuffer(1024); PKCS8EncodedKeySpec pvkKeySpec = new PKCS8EncodedKeySpec(
encodedPrivkey);
KeyFactory keyFactory = KeyFactory.getInstance("RSA");
RSAPrivateCrtKey pvkKey = (RSAPrivateCrtKey) keyFactory
.generatePrivate(pvkKeySpec); buff.append("<RSAKeyValue>");
buff.append("<Modulus>"
+ CodeUtils.base64Encode(removeMSZero(pvkKey.getModulus().toByteArray()))
+ "</Modulus>"); buff.append("<Exponent>"
+ CodeUtils.base64Encode(removeMSZero(pvkKey.getPublicExponent()
.toByteArray())) + "</Exponent>"); buff.append("<P>"
+ CodeUtils.base64Encode(removeMSZero(pvkKey.getPrimeP().toByteArray()))
+ "</P>"); buff.append("<Q>"
+ CodeUtils.base64Encode(removeMSZero(pvkKey.getPrimeQ().toByteArray()))
+ "</Q>"); buff.append("<DP>"
+ CodeUtils.base64Encode(removeMSZero(pvkKey.getPrimeExponentP()
.toByteArray())) + "</DP>"); buff.append("<DQ>"
+ CodeUtils.base64Encode(removeMSZero(pvkKey.getPrimeExponentQ()
.toByteArray())) + "</DQ>"); buff.append("<InverseQ>"
+ CodeUtils.base64Encode(removeMSZero(pvkKey.getCrtCoefficient()
.toByteArray())) + "</InverseQ>"); buff.append("<D>"
+ CodeUtils.base64Encode(removeMSZero(pvkKey.getPrivateExponent()
.toByteArray())) + "</D>");
buff.append("</RSAKeyValue>"); return buff.toString().replaceAll("[ \t\n\r]", "");
} catch (Exception e) {
System.err.println(e);
return null;
}
} private static String getRSAPublicKeyAsNetFormat(byte[] encodedPublicKey) {
try { KeyFactory keyFactory = KeyFactory.getInstance("RSA");
RSAPublicKey pukKey = (RSAPublicKey) keyFactory.generatePublic(new X509EncodedKeySpec(encodedPublicKey)); StringBuffer buff = new StringBuffer(1024);
buff.append("<RSAKeyValue>");
buff.append("<Modulus>"
+ CodeUtils.base64Encode(removeMSZero(pukKey.getModulus().toByteArray()))
+ "</Modulus>");
buff.append("<Exponent>"
+ CodeUtils.base64Encode(removeMSZero(pukKey.getPublicExponent().toByteArray())) + "</Exponent>");
buff.append("</RSAKeyValue>");
11 return buff.toString().replaceAll("[ \t\n\r]", "");
} catch (Exception e) {
System.err.println(e);
return null;
}
}
private static byte[] removeMSZero(byte[] data) {
byte[] data1;
int len = data.length;
if (data[] == ) {
data1 = new byte[data.length - ];
System.arraycopy(data, , data1, , len - );
} else
data1 = data; return data1;
}
RSAUtils.java
//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
// package com.union.pufa; import java.math.BigInteger;
import java.security.Key;
import java.security.KeyFactory;
import java.security.KeyPair;
import java.security.KeyPairGenerator;
import java.security.NoSuchAlgorithmException;
import java.security.PrivateKey;
import java.security.Provider;
import java.security.PublicKey;
import java.security.SecureRandom;
import java.security.Signature;
import java.security.interfaces.RSAPrivateKey;
import java.security.interfaces.RSAPublicKey;
import java.security.spec.InvalidKeySpecException;
import java.security.spec.PKCS8EncodedKeySpec;
import java.security.spec.RSAKeyGenParameterSpec;
import java.security.spec.RSAPrivateKeySpec;
import java.security.spec.RSAPublicKeySpec;
import java.security.spec.X509EncodedKeySpec;
import javax.crypto.Cipher;
import org.apache.commons.codec.binary.Hex;
import org.apache.commons.lang.StringUtils;
import org.bouncycastle.jce.provider.BouncyCastleProvider;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory; public abstract class RSAUtils {
private static final Logger LOGGER = LoggerFactory.getLogger(RSAUtils.class);
private static final String ALGORITHOM = "RSA";
private static final Provider DEFAULT_PROVIDER = new BouncyCastleProvider();
private static final String SIGNATURE_MD5WITHRSA = "MD5withRSA";
private static final String SIGNATURE_SHA1WITHRSA = "SHA1withRSA";
private static final String SIGNATURE_ALGORITHM_DEFAULT = "NONEwithRSA";
private static final String CIPHER_TRANSFORMATION_DEFAULT = "RSA/ECB/PKCS1Padding";
private static final String HASHID_SHA1 = "01";
private static final String HASHID_MD5 = "02";
private static KeyPairGenerator keyPairGen = null;
private static KeyFactory keyFactory = null; static {
try {
keyPairGen = KeyPairGenerator.getInstance("RSA", DEFAULT_PROVIDER);
keyFactory = KeyFactory.getInstance("RSA", DEFAULT_PROVIDER);
} catch (NoSuchAlgorithmException var1) {
LOGGER.error(var1.getMessage());
} } private RSAUtils() {
} public static synchronized KeyPair generateRSAKeyPair(int keysize, BigInteger publicExponent) {
try {
keyPairGen.initialize(new RSAKeyGenParameterSpec(keysize, publicExponent), new SecureRandom());
return keyPairGen.generateKeyPair();
} catch (Exception var3) {
LOGGER.error("生成模长 =" + keysize + ",指数=" + publicExponent + "的RSA密钥对失败", var3);
return null;
}
} public static PrivateKey getRSAPrivateKey(String hexModulus, String hexPrivateExponent) {
if(!StringUtils.isBlank(hexModulus) && !StringUtils.isBlank(hexPrivateExponent)) {
BigInteger mbig = new BigInteger(hexModulus, 16);
BigInteger ebig = new BigInteger(hexPrivateExponent, 16);
RSAPrivateKeySpec prispec = new RSAPrivateKeySpec(mbig, ebig); try {
return keyFactory.generatePrivate(prispec);
} catch (InvalidKeySpecException var6) {
LOGGER.error("hexModulus or hexPrivateExponent value is invalid. return null(RSAPrivateKey).");
return null;
}
} else {
if(LOGGER.isDebugEnabled()) {
LOGGER.debug("hexModulus and hexPrivateExponent cannot be empty. RSAPrivateKey value is null to return.");
} return null;
}
} public static PublicKey getRSAPublidKey(String hexModulus, String hexPublicExponent) {
if(!StringUtils.isBlank(hexModulus) && !StringUtils.isBlank(hexPublicExponent)) {
BigInteger mbig = new BigInteger(hexModulus, 16);
BigInteger ebig = new BigInteger(hexPublicExponent, 16);
RSAPublicKeySpec pubspec = new RSAPublicKeySpec(mbig, ebig); try {
return keyFactory.generatePublic(pubspec);
} catch (InvalidKeySpecException var6) {
LOGGER.error("hexModulus or hexPublicExponent value is invalid. return null(RSAPublicKey).");
return null;
}
} else {
if(LOGGER.isDebugEnabled()) {
LOGGER.debug("hexModulus and hexPublicExponent cannot be empty. return null(RSAPublicKey).");
} return null;
}
} public static String getBase64CodeKey(Key key) {
return CodeUtils.base64Encode(key.getEncoded());
} public static String getNakedPublicKey(RSAPublicKey key) {
return key.getModulus().toString(16).toUpperCase();
} private static byte[] getAsn1Len(int len) {
int ret = false;
byte[] buff = new byte[10];
byte[] asn1Len = (byte[])null;
if(len > '\uffff') {
return null;
} else {
byte ret;
if(len > 255) {
buff[0] = -126;
buff[1] = (byte)((len & '\uff00') >> 8);
buff[2] = (byte)(len & 255);
ret = 3;
} else if((len & 128) != 0) {
buff[0] = -127;
buff[1] = (byte)len;
ret = 2;
} else {
buff[0] = (byte)len;
ret = 1;
} asn1Len = new byte[ret];
System.arraycopy(buff, 0, asn1Len, 0, ret);
return asn1Len;
}
} public static byte[] getDerPK(byte[] pkModule, byte[] exp) {
byte[] buff = new byte[4096];
byte[] tbuff = new byte[4096];
byte[] tmp = (byte[])null;
byte[] derPK = (byte[])null;
int offset = false;
int len = false;
int lenOfPkModule = pkModule.length;
int lenOfExp = exp.length;
if(pkModule != null && exp != null) {
int offset = 0;
tbuff[offset] = 2;
int offset = offset + 1;
tmp = getAsn1Len(lenOfPkModule + 1);
System.arraycopy(tmp, 0, tbuff, offset, tmp.length);
offset += tmp.length;
tbuff[offset] = 0;
++offset;
System.arraycopy(pkModule, 0, tbuff, offset, lenOfPkModule);
offset += lenOfPkModule;
tbuff[offset] = 2;
++offset;
tmp = getAsn1Len(lenOfExp);
System.arraycopy(tmp, 0, tbuff, offset, tmp.length);
offset += tmp.length;
System.arraycopy(exp, 0, tbuff, offset, lenOfExp);
offset += lenOfExp;
int len = offset;
offset = 0;
buff[offset] = 48;
offset = offset + 1;
tmp = getAsn1Len(len);
System.arraycopy(tmp, 0, buff, offset, tmp.length);
offset += tmp.length;
System.arraycopy(tbuff, 0, buff, offset, len);
offset += len;
derPK = new byte[offset];
System.arraycopy(buff, 0, derPK, 0, offset);
return derPK;
} else {
return null;
}
} public static String getDerPKWithAscHex(String pkModule, String exp) {
return pkModule != null && exp != null?CodeUtils.byte2hex(getDerPK(CodeUtils.hex2byte(pkModule), CodeUtils.hex2byte(exp))):null;
} public static PublicKey getPKfromDerPK(String racalPK) {
byte[] racalPKStr = (byte[])null;
if(racalPK == null) {
return null;
} else {
racalPKStr = CodeUtils.hex2byte(racalPK);
int offset = 0;
if(racalPKStr[offset] != 48) {
return null;
} else {
int offset = offset + 1;
int i;
int lenOfNextPart;
int bitsOfLenFlag;
if((racalPKStr[offset] & 255) <= 128) {
lenOfNextPart = racalPKStr[offset] & 255;
++offset;
} else {
bitsOfLenFlag = (racalPKStr[offset] & 255) - 128;
++offset;
i = 0; for(lenOfNextPart = 0; i < bitsOfLenFlag; ++offset) {
lenOfNextPart += racalPKStr[offset] & 255;
++i;
}
} if((racalPKStr[offset] & 255) != 2) {
return null;
} else {
++offset;
--lenOfNextPart;
int lenOfPK = false;
int lenOfPK;
if((racalPKStr[offset] & 255) <= 128) {
lenOfPK = racalPKStr[offset] & 255;
++offset;
} else {
bitsOfLenFlag = (racalPKStr[offset] & 255) - 128;
++offset;
i = 0; for(lenOfPK = 0; i < bitsOfLenFlag; ++offset) {
lenOfPK += racalPKStr[offset] & 255;
++i;
}
} while(lenOfPK % 8 != 0) {
if((racalPKStr[offset] & 255) != 0) {
return null;
} ++offset;
--lenOfPK;
} byte[] LPk = new byte[lenOfPK];
System.arraycopy(racalPKStr, offset, LPk, 0, lenOfPK);
String pk = CodeUtils.byte2hex(LPk);
offset += lenOfPK;
int lenOfEval = false;
if(racalPKStr[offset] != 2) {
return null;
} else {
++offset;
int lenOfEval = racalPKStr[offset];
++offset;
byte[] LPkEval = new byte[lenOfEval];
System.arraycopy(racalPKStr, offset, LPkEval, 0, lenOfEval);
return getRSAPublidKey(pk, CodeUtils.byte2hex(LPkEval));
}
}
}
}
} public static RSAPublicKey getRSAPublidKeyBybase64(String base64s) {
X509EncodedKeySpec keySpec = new X509EncodedKeySpec(CodeUtils.base64Decode(base64s));
RSAPublicKey publicKey = null; try {
publicKey = (RSAPublicKey)keyFactory.generatePublic(keySpec);
} catch (InvalidKeySpecException var4) {
LOGGER.error("base64编码=" + base64s + "转RSA公钥失败", var4);
} return publicKey;
} public static RSAPrivateKey getRSAPrivateKeyBybase64(String base64s) {
PKCS8EncodedKeySpec keySpec = new PKCS8EncodedKeySpec(CodeUtils.base64Decode(base64s));
RSAPrivateKey privateKey = null; try {
privateKey = (RSAPrivateKey)keyFactory.generatePrivate(keySpec);
} catch (InvalidKeySpecException var4) {
LOGGER.error("base64编码=" + base64s + "转RSA私钥失败", var4);
} return privateKey;
} public static byte[] encrypt(Key key, byte[] data) throws Exception {
Cipher cipher = Cipher.getInstance("RSA/ECB/PKCS1Padding", DEFAULT_PROVIDER);
cipher.init(1, key);
return cipher.doFinal(data);
} public static byte[] decrypt(Key key, byte[] data) throws Exception {
Cipher cipher = Cipher.getInstance("RSA/ECB/PKCS1Padding", DEFAULT_PROVIDER);
cipher.init(2, key);
return cipher.doFinal(data);
} public static String encryptString(Key key, String plaintext) {
if(key != null && plaintext != null) {
byte[] data = plaintext.getBytes(); try {
byte[] en_data = encrypt(key, data);
return new String(Hex.encodeHex(en_data));
} catch (Exception var4) {
LOGGER.error(var4.getCause().getMessage());
return null;
}
} else {
return null;
}
} public static String encryptStr4essc(Key key, String plaintext, String accNo, String encType) {
if(key != null && plaintext != null) {
if("1".equals(encType)) {
plaintext = CodeUtils.paddingRightStr(plaintext, 'F', 16);
} else if("0".equals(encType)) {
plaintext = CodeUtils.asc2ascInt2hexasc(CodeUtils.paddingRightStr(plaintext, 'F', 16));
} byte[] data = CodeUtils.hex2byte(plaintext); try {
byte[] en_data = encrypt(key, data);
return (new String(Hex.encodeHex(en_data))).toUpperCase();
} catch (Exception var6) {
LOGGER.error(var6.getCause().getMessage());
return null;
}
} else {
return null;
}
} public static String decryptString(Key key, String encrypttext) {
if(key != null && !StringUtils.isBlank(encrypttext)) {
try {
byte[] en_data = Hex.decodeHex(encrypttext.toCharArray());
byte[] data = decrypt(key, en_data);
return new String(data);
} catch (Exception var4) {
LOGGER.error(String.format("\"%s\" Decryption failed. Cause: %s", new Object[]{encrypttext, var4.getCause().getMessage()}));
return null;
}
} else {
return null;
}
} public static String sign(RSAPrivateKey privatekey, String dataFillMode, String hashID, String data) throws Exception {
Signature signature = Signature.getInstance("NONEwithRSA");
if("01".equals(hashID)) {
data = CodeUtils.sha1String(data).toUpperCase();
} else if("02".equals(hashID)) {
data = CodeUtils.md5String(data).toUpperCase();
} signature.initSign(privatekey);
signature.update(data.getBytes());
return CodeUtils.byte2hex(signature.sign()).toUpperCase();
} public static boolean verifySign(PublicKey publicKey, String dataFillMode, String hashID, String data, String sign) throws Exception {
Signature signature = Signature.getInstance("NONEwithRSA");
if("01".equals(hashID)) {
data = CodeUtils.sha1String(data).toUpperCase();
} else if("02".equals(hashID)) {
data = CodeUtils.md5String(data).toUpperCase();
} signature.initVerify(publicKey);
signature.update(data.getBytes());
return signature.verify(CodeUtils.hex2byte(sign.toUpperCase()));
}
}