RSAツール類は、直接ご利用いただける点がございますのでご了承ください
/**
* RSA
*
* Create by Mazhanzhu on 2019/8/10
*/
public class RSAUtils {
private static final String TAG = "RSAUtils";
public static final String KEY_ALGORITHM = "RSA";
public static final String split = " ";//
public static final int max = 117;// // 117
private static RSAUtils me;
private RSAUtils() {
}//
public static RSAUtils create() {
if (me == null) {
me = new RSAUtils();
}
try {
// 、 , , ,
KeyPairGenerator kpg = KeyPairGenerator.getInstance(KEY_ALGORITHM);
kpg.initialize(1024);
KeyPair kp = kpg.generateKeyPair();
me.publicKey = (RSAPublicKey) kp.getPublic();
me.privateKey = (RSAPrivateCrtKey) kp.getPrivate();
} catch (Exception e) {
e.printStackTrace();
}
return me;
}
private RSAPublicKey publicKey;
private RSAPrivateCrtKey privateKey;
/**
*
*/
public String getPublicKey() {
return parseByte2HexStr(publicKey.getEncoded());
}
/**
*
*/
public String getPrivateKey() {
return parseByte2HexStr(privateKey.getEncoded());
}
/**
* -
*/
public String encodeByPublicKey(String res, String key) {
byte[] resBytes = res.getBytes();
byte[] keyBytes = parseHexStr2Byte(key);// 2
StringBuffer result = new StringBuffer();//
// 100
if (keyBytes.length <= max) {//
return encodePub(resBytes, keyBytes);
} else {
int size = resBytes.length / max
+ (resBytes.length % max > 0 ? 1 : 0);
for (int i = 0; i < size; i++) {
int len = i == size - 1 ? resBytes.length % max : max;
byte[] bs = new byte[len];//
System.arraycopy(resBytes, i * max, bs, 0, len);
result.append(encodePub(bs, keyBytes));
if (i != size - 1)
result.append(split);
}
return result.toString();
}
}
/**
* -
*/
public String encodeByPrivateKey(String res, String key) {
byte[] resBytes = res.getBytes();
byte[] keyBytes = parseHexStr2Byte(key);
StringBuffer result = new StringBuffer();
// 100
if (keyBytes.length <= max) {//
return encodePri(resBytes, keyBytes);
} else {
int size = resBytes.length / max
+ (resBytes.length % max > 0 ? 1 : 0);
for (int i = 0; i < size; i++) {
int len = i == size - 1 ? resBytes.length % max : max;
byte[] bs = new byte[len];//
System.arraycopy(resBytes, i * max, bs, 0, len);
result.append(encodePri(bs, keyBytes));
if (i != size - 1)
result.append(split);
}
return result.toString();
}
}
/**
* -
*/
public String decodeByPublicKey(String res, String key) {
byte[] keyBytes = parseHexStr2Byte(key);
//
String[] rs = res.split("\\" + split);
//
if (rs != null) {
int len = 0;
// byte[]
byte[] result = new byte[rs.length * max];
for (int i = 0; i < rs.length; i++) {
byte[] bs = decodePub(parseHexStr2Byte(rs[i]), keyBytes);
System.arraycopy(bs, 0, result, i * max, bs.length);
len += bs.length;
}
byte[] newResult = new byte[len];
System.arraycopy(result, 0, newResult, 0, len);
//
return new String(newResult);
}
return null;
}
/**
* -
*/
public String decodeByPrivateKey(String res, String key) {
byte[] keyBytes = parseHexStr2Byte(key);
//
String[] rs = res.split("\\" + split);
//
if (rs != null) {
int len = 0;
// byte[]
byte[] result = new byte[rs.length * max];
for (int i = 0; i < rs.length; i++) {
byte[] bs = decodePri(parseHexStr2Byte(rs[i]), keyBytes);
System.arraycopy(bs, 0, result, i * max, bs.length);
len += bs.length;
}
byte[] newResult = new byte[len];
System.arraycopy(result, 0, newResult, 0, len);
//
return new String(newResult);
}
return null;
}
/**
* 16
*/
public static String parseByte2HexStr(byte buf[]) {
StringBuffer sb = new StringBuffer();
for (int i = 0; i < buf.length; i++) {
String hex = Integer.toHexString(buf[i] & 0xFF);
if (hex.length() == 1) {
hex = '0' + hex;
}
sb.append(hex.toUpperCase());
}
return sb.toString();
}
/**
* 16
*/
public static byte[] parseHexStr2Byte(String hexStr) {
if (hexStr.length() < 1)
return null;
byte[] result = new byte[hexStr.length() / 2];
for (int i = 0; i < hexStr.length() / 2; i++) {
int high = Integer.parseInt(hexStr.substring(i * 2, i * 2 + 1), 16);
int low = Integer.parseInt(hexStr.substring(i * 2 + 1, i * 2 + 2),
16);
result[i] = (byte) (high * 16 + low);
}
return result;
}
/**
* - -
*/
private String encodePub(byte[] res, byte[] keyBytes) {
X509EncodedKeySpec x5 = new X509EncodedKeySpec(keyBytes);// 2 x509
try {
KeyFactory kf = KeyFactory.getInstance(KEY_ALGORITHM);
Key pubKey = kf.generatePublic(x5);// KeyFactory x509 pubKey
Cipher cp = Cipher.getInstance(kf.getAlgorithm());// Cipher
cp.init(Cipher.ENCRYPT_MODE, pubKey);// cipher , pubKey
return parseByte2HexStr(cp.doFinal(res));// 16
} catch (Exception e) {
System.out.println(" ");
e.printStackTrace();
}
return null;
}
/**
* - -
*/
private String encodePri(byte[] res, byte[] keyBytes) {
PKCS8EncodedKeySpec pk8 = new PKCS8EncodedKeySpec(keyBytes);
try {
KeyFactory kf = KeyFactory.getInstance(KEY_ALGORITHM);
Key priKey = kf.generatePrivate(pk8);
Cipher cp = Cipher.getInstance(kf.getAlgorithm());
cp.init(Cipher.ENCRYPT_MODE, priKey);
return parseByte2HexStr(cp.doFinal(res));
} catch (Exception e) {
System.out.println(" ");
e.printStackTrace();
}
return null;
}
/**
* - -
*/
private byte[] decodePub(byte[] res, byte[] keyBytes) {
X509EncodedKeySpec x5 = new X509EncodedKeySpec(keyBytes);
try {
KeyFactory kf = KeyFactory.getInstance(KEY_ALGORITHM);
Key pubKey = kf.generatePublic(x5);
Cipher cp = Cipher.getInstance(kf.getAlgorithm());
cp.init(Cipher.DECRYPT_MODE, pubKey);
return cp.doFinal(res);
} catch (Exception e) {
System.out.println(" ");
e.printStackTrace();
}
return null;
}
/**
* - -
*/
private byte[] decodePri(byte[] res, byte[] keyBytes) {
PKCS8EncodedKeySpec pk8 = new PKCS8EncodedKeySpec(keyBytes);
try {
KeyFactory kf = KeyFactory.getInstance(KEY_ALGORITHM);
Key priKey = kf.generatePrivate(pk8);
Cipher cp = Cipher.getInstance(kf.getAlgorithm());
cp.init(Cipher.DECRYPT_MODE, priKey);
return cp.doFinal(res);
} catch (Exception e) {
System.out.println(" ");
e.printStackTrace();
}
return null;
}
}