//
//Source code recreated from a .class file by IntelliJ IDEA//(powered by Fernflower decompiler)//
packagecom.union.pufa;importjava.math.BigInteger;importjava.security.Key;importjava.security.KeyFactory;importjava.security.KeyPair;importjava.security.KeyPairGenerator;importjava.security.NoSuchAlgorithmException;importjava.security.PrivateKey;importjava.security.Provider;importjava.security.PublicKey;importjava.security.SecureRandom;importjava.security.Signature;importjava.security.interfaces.RSAPrivateKey;importjava.security.interfaces.RSAPublicKey;importjava.security.spec.InvalidKeySpecException;importjava.security.spec.PKCS8EncodedKeySpec;importjava.security.spec.RSAKeyGenParameterSpec;importjava.security.spec.RSAPrivateKeySpec;importjava.security.spec.RSAPublicKeySpec;importjava.security.spec.X509EncodedKeySpec;importjavax.crypto.Cipher;importorg.apache.commons.codec.binary.Hex;importorg.apache.commons.lang.StringUtils;importorg.bouncycastle.jce.provider.BouncyCastleProvider;importorg.slf4j.Logger;importorg.slf4j.LoggerFactory;public abstract classRSAUtils {private static final Logger LOGGER = LoggerFactory.getLogger(RSAUtils.class);private static final String ALGORITHOM = "RSA";private static final Provider DEFAULT_PROVIDER = newBouncyCastleProvider();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());
}
}privateRSAUtils() {
}public static synchronized KeyPair generateRSAKeyPair(intkeysize, BigInteger publicExponent) {try{
keyPairGen.initialize(new RSAKeyGenParameterSpec(keysize, publicExponent), newSecureRandom());returnkeyPairGen.generateKeyPair();
}catch(Exception var3) {
LOGGER.error("生成模长 =" + keysize + ",指数=" + publicExponent + "的RSA密钥对失败", var3);return null;
}
}public staticPrivateKey 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= newRSAPrivateKeySpec(mbig, ebig);try{returnkeyFactory.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 staticPublicKey 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= newRSAPublicKeySpec(mbig, ebig);try{returnkeyFactory.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 staticString getBase64CodeKey(Key key) {returnCodeUtils.base64Encode(key.getEncoded());
}public staticString getNakedPublicKey(RSAPublicKey key) {return key.getModulus().toString(16).toUpperCase();
}private static byte[] getAsn1Len(intlen) {int ret = false;byte[] buff = new byte[10];byte[] asn1Len = (byte[])null;if(len > '\uffff') {return null;
}else{byteret;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);returnasn1Len;
}
}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);returnderPK;
}else{return null;
}
}public staticString getDerPKWithAscHex(String pkModule, String exp) {return pkModule != null && exp != null?CodeUtils.byte2hex(getDerPK(CodeUtils.hex2byte(pkModule), CodeUtils.hex2byte(exp))):null;
}public staticPublicKey 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;inti;intlenOfNextPart;intbitsOfLenFlag;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;intlenOfPK;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);returngetRSAPublidKey(pk, CodeUtils.byte2hex(LPkEval));
}
}
}
}
}public staticRSAPublicKey getRSAPublidKeyBybase64(String base64s) {
X509EncodedKeySpec keySpec= newX509EncodedKeySpec(CodeUtils.base64Decode(base64s));
RSAPublicKey publicKey= null;try{
publicKey=(RSAPublicKey)keyFactory.generatePublic(keySpec);
}catch(InvalidKeySpecException var4) {
LOGGER.error("base64编码=" + base64s + "转RSA公钥失败", var4);
}returnpublicKey;
}public staticRSAPrivateKey getRSAPrivateKeyBybase64(String base64s) {
PKCS8EncodedKeySpec keySpec= newPKCS8EncodedKeySpec(CodeUtils.base64Decode(base64s));
RSAPrivateKey privateKey= null;try{
privateKey=(RSAPrivateKey)keyFactory.generatePrivate(keySpec);
}catch(InvalidKeySpecException var4) {
LOGGER.error("base64编码=" + base64s + "转RSA私钥失败", var4);
}returnprivateKey;
}public static byte[] encrypt(Key key, byte[] data) throwsException {
Cipher cipher= Cipher.getInstance("RSA/ECB/PKCS1Padding", DEFAULT_PROVIDER);
cipher.init(1, key);returncipher.doFinal(data);
}public static byte[] decrypt(Key key, byte[] data) throwsException {
Cipher cipher= Cipher.getInstance("RSA/ECB/PKCS1Padding", DEFAULT_PROVIDER);
cipher.init(2, key);returncipher.doFinal(data);
}public staticString encryptString(Key key, String plaintext) {if(key != null && plaintext != null) {byte[] data =plaintext.getBytes();try{byte[] en_data =encrypt(key, data);return newString(Hex.encodeHex(en_data));
}catch(Exception var4) {
LOGGER.error(var4.getCause().getMessage());return null;
}
}else{return null;
}
}public staticString 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 (newString(Hex.encodeHex(en_data))).toUpperCase();
}catch(Exception var6) {
LOGGER.error(var6.getCause().getMessage());return null;
}
}else{return null;
}
}public staticString 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 newString(data);
}catch(Exception var4) {
LOGGER.error(String.format("\"%s\" Decryption failed. Cause: %s", newObject[]{encrypttext, var4.getCause().getMessage()}));return null;
}
}else{return null;
}
}public static String sign(RSAPrivateKey privatekey, String dataFillMode, String hashID, String data) throwsException {
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());returnCodeUtils.byte2hex(signature.sign()).toUpperCase();
}public static boolean verifySign(PublicKey publicKey, String dataFillMode, String hashID, String data, String sign) throwsException {
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());returnsignature.verify(CodeUtils.hex2byte(sign.toUpperCase()));
}
}