com.luues.util.encryption.RSAUtil_back Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of commons-util Show documentation
Show all versions of commons-util Show documentation
A Simple Tool Operations Class
//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//
package com.luues.util.encryption;
import com.luues.util.logs.LogUtil;
import java.io.UnsupportedEncodingException;
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.SecureRandom;
import java.security.spec.PKCS8EncodedKeySpec;
import java.security.spec.X509EncodedKeySpec;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import javax.crypto.Cipher;
import org.apache.commons.codec.binary.Base64;
/** 后台加密 后台解密 */
public class RSAUtil_back {
private static String KEY_RSA_TYPE = "RSA";
private static int KEY_SIZE = 1024;
private static int ENCODE_PART_SIZE;
private static final String PUBLIC_KEY = "PUBLIC_KEY";
private static final String PRIVATE_KEY = "PRIVATE_KEY";
public RSAUtil_back() {
}
public static void main(String[] args){
String k = "MIICdwIBADANBgkqhkiG9w0BAQEFAASCAmEwggJdAgEAAoGBAIKo3OibEfllxB9t3AbkE9/3K9vwM+uhScBhXb9ILp7XpMcH1pf+i/OImFDPIeahXF/yO078mrSZE7Zl+BH/9H0AgQuqMYsW/F2Js8MFlqVEt4eerpS3rgRZiE2GwrN3jweombVn+tCFUbJbVf33w6R/OminSh8BmOM7KFonua2vAgMBAAECgYBuS9Pnu3fJ22Fa0qdpXcF1T7bDfBqoL0gN0WOM5IcOaaVcqj45opRLqaRCmwfYI6DSEa6FN/H0wEAGuj9WGoYsflOaihCxGQSw3R4FEfjy7MkLtSvpc92oaaWz+e9jm6afX1VilaFsel1vkm/ybsgIkl45I9C33PWjRtSxCsodYQJBAOaYBn5HcCpo7hz1hJd3yi2sRRHFiXs9szcM+X2FQGrlz6ctVXCdS8Km0WIht+0QDFXT+VNNAofmSE3boGwgK/ECQQCRDij7+n1sdXprmJlumzeEsbyQXTYMrwtVgwjaLZDV7Wq2ZXK1ARAwTd5zvmpC8MCw5SX/es5UcL4jmh+ds5OfAkEAmlgDcJ6saxaU2nlTaHbbsrpt+Lk5jm37+MsMa4G3XlW4KfPkDl6aiQ1TdNWQ4HIvb3tUlPckzIXMu6BXvkfCYQJAQOfyJNYv+zESB0UwGpLvo7uYIYzj24cT7j6E3oOXFHJ41obMbfu8z7B4QphImg16W20dtJSx8IzGN84GKZ2qBwJBANKOiojqmBEkIGJ0/f8oiAhIFw+KoctNHNPn4hwhvwD0h7GkCiJI+Z2ksG/vbg5V020KDkHrdfmv/NmeX3JAtGM=";
System.err.println(decode("f+fyVTk8eNPeJPNLvJIuVBFs3vssiaEjBJp6tGcQ7vF5WT1UikjH8XvN8dPi2UOLY1HwrXOriF2eomguWUG6FOaPaTLIDGoJR9h9PyFrLvTdYToVcvLnBg2DGdPh2oxdMOTCkH1LJUZ89rW8VczKyT6EnDPCHkbRh3ry3nBWxxo=", k));
}
public static Map createRSAKeys() {
HashMap keyPairMap = new HashMap();
try {
KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance(KEY_RSA_TYPE);
keyPairGenerator.initialize(KEY_SIZE, new SecureRandom());
KeyPair keyPair = keyPairGenerator.generateKeyPair();
String publicKeyValue = new String(Base64.encodeBase64(keyPair.getPublic().getEncoded()), "utf-8");
String privateKeyValue = new String(Base64.encodeBase64(keyPair.getPrivate().getEncoded()), "utf-8");
keyPairMap.put(PUBLIC_KEY, publicKeyValue);
keyPairMap.put(PRIVATE_KEY, privateKeyValue);
} catch (NoSuchAlgorithmException var5) {
LogUtil.error("当前JDK版本没找到RSA加密算法!");
var5.printStackTrace();
} catch (UnsupportedEncodingException var6) {
var6.printStackTrace();
}
return keyPairMap;
}
public static String encode(String sourceStr, String publicKeyBase64Str) {
byte[] publicBytes = Base64.decodeBase64(publicKeyBase64Str.getBytes());
X509EncodedKeySpec x509EncodedKeySpec = new X509EncodedKeySpec(publicBytes);
List alreadyEncodeListData = new LinkedList();
int maxEncodeSize = ENCODE_PART_SIZE - 11;
String encodeBase64Result = null;
try {
KeyFactory keyFactory = KeyFactory.getInstance(KEY_RSA_TYPE);
PublicKey publicKey = keyFactory.generatePublic(x509EncodedKeySpec);
Cipher cipher = Cipher.getInstance(KEY_RSA_TYPE);
cipher.init(1, publicKey);
byte[] sourceBytes = sourceStr.getBytes("utf-8");
int sourceLen = sourceBytes.length;
int partLen;
int curPosition;
byte[] tempByteList;
for(partLen = 0; partLen < sourceLen; partLen += maxEncodeSize) {
curPosition = sourceLen - partLen;
int tempLen = curPosition;
if (curPosition > maxEncodeSize) {
tempLen = maxEncodeSize;
}
byte[] tempBytes = new byte[tempLen];
System.arraycopy(sourceBytes, partLen, tempBytes, 0, tempLen);
tempByteList = cipher.doFinal(tempBytes);
alreadyEncodeListData.add(tempByteList);
}
partLen = alreadyEncodeListData.size();
curPosition = partLen * ENCODE_PART_SIZE;
byte[] encodeData = new byte[curPosition];
for(int i = 0; i < partLen; ++i) {
tempByteList = (byte[])alreadyEncodeListData.get(i);
System.arraycopy(tempByteList, 0, encodeData, i * ENCODE_PART_SIZE, ENCODE_PART_SIZE);
}
encodeBase64Result = new String(Base64.encodeBase64(encodeData), "utf-8");
} catch (Exception var17) {
var17.printStackTrace();
}
return encodeBase64Result;
}
public static String decode(String sourceBase64RSA, String privateKeyBase64Str) {
byte[] privateBytes = Base64.decodeBase64(privateKeyBase64Str.getBytes());
byte[] encodeSource = Base64.decodeBase64(sourceBase64RSA.getBytes());
int encodePartLen = encodeSource.length / ENCODE_PART_SIZE;
List decodeListData = new LinkedList();
String decodeStrResult = null;
PKCS8EncodedKeySpec pkcs8EncodedKeySpec = new PKCS8EncodedKeySpec(privateBytes);
try {
KeyFactory keyFactory = KeyFactory.getInstance(KEY_RSA_TYPE);
PrivateKey privateKey = keyFactory.generatePrivate(pkcs8EncodedKeySpec);
Cipher cipher = Cipher.getInstance(KEY_RSA_TYPE);
cipher.init(2, privateKey);
int allDecodeByteLen = 0;
for(int i = 0; i < encodePartLen; ++i) {
byte[] tempEncodedData = new byte[ENCODE_PART_SIZE];
System.arraycopy(encodeSource, i * ENCODE_PART_SIZE, tempEncodedData, 0, ENCODE_PART_SIZE);
byte[] decodePartData = cipher.doFinal(tempEncodedData);
decodeListData.add(decodePartData);
allDecodeByteLen += decodePartData.length;
}
byte[] decodeResultBytes = new byte[allDecodeByteLen];
int i = 0;
for(int curPosition = 0; i < encodePartLen; ++i) {
byte[] tempSorceBytes = (byte[])decodeListData.get(i);
int tempSourceBytesLen = tempSorceBytes.length;
System.arraycopy(tempSorceBytes, 0, decodeResultBytes, curPosition, tempSourceBytesLen);
curPosition += tempSourceBytesLen;
}
decodeStrResult = new String(decodeResultBytes, "UTF-8");
} catch (Exception var17) {
var17.printStackTrace();
}
return decodeStrResult;
}
public static String getPublicKey(Map map){
return map.get(PUBLIC_KEY);
}
public static String getPrivateKey(Map map){
return map.get(PRIVATE_KEY);
}
static {
ENCODE_PART_SIZE = KEY_SIZE / 8;
}
}