|
|
|
|
@ -4,6 +4,7 @@ package com.bjzc.yfdxj.utils;
|
|
|
|
|
import android.util.Base64;
|
|
|
|
|
import android.util.Log;
|
|
|
|
|
|
|
|
|
|
import java.io.ByteArrayOutputStream;
|
|
|
|
|
import java.security.Key;
|
|
|
|
|
import java.security.KeyFactory;
|
|
|
|
|
import java.security.KeyPair;
|
|
|
|
|
@ -24,11 +25,21 @@ public class RSAUtils {
|
|
|
|
|
* RSA算法
|
|
|
|
|
*/
|
|
|
|
|
public static final String RSA = "RSA";
|
|
|
|
|
|
|
|
|
|
public static final String ECB_None_PKCS1_PADDING = "RSA/None/PKCS1Padding";//java默认加密填充方式 RSA/None/PKCS1Padding
|
|
|
|
|
|
|
|
|
|
private static final String PUBLIC_KEY = "RSAPublicKey";
|
|
|
|
|
private static final String PRIVATE_KEY = "RSAPrivateKey";
|
|
|
|
|
public static final int DEFAULT_KEY_SIZE = 2048;//秘钥默认长度
|
|
|
|
|
|
|
|
|
|
//RSA最大加密明文大小 1024位是117 算法 最大加密明文大小 = 1024(bit) / 8 - 11(byte) = 117 byte 最大加密明文大小 = 2048(bit) / 8 - 11(byte) = 245 byte
|
|
|
|
|
//private static final int MAX_ENCRYPT_BLOCK = 245;
|
|
|
|
|
private static final int MAX_ENCRYPT_BLOCK = (DEFAULT_KEY_SIZE / 8) - 11;
|
|
|
|
|
//RSA最大解密密文大小 1024位是128 2048位是 256 依此类推
|
|
|
|
|
//private static final int MAX_DECRYPT_BLOCK = 256;
|
|
|
|
|
private static final int MAX_DECRYPT_BLOCK = DEFAULT_KEY_SIZE / 8;
|
|
|
|
|
//java默认加密填充方式 RSA 对应安卓 RSA/None/PKCS1Padding
|
|
|
|
|
//public static final String ECB_None_PKCS1_PADDING = "RSA";
|
|
|
|
|
//安卓手机加密填充方式用 RSA/None/PKCS1Padding
|
|
|
|
|
public static final String ECB_None_PKCS1_PADDING = "RSA/None/PKCS1Padding";
|
|
|
|
|
|
|
|
|
|
public static final String public_key = "MIIBIjANBg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H0M1pKiUzUzp6ulSC8GH2/uZzBq47agre9bW0A+Uu0IiCwfOc0J1L1UlLm/wWRGW0Sw51OHYAs99irjhxdGFOVv/mS9FzzxmBRFOwUXFD8iXyEKI8xkteyHGniKRwBYoWR32kQw7H/Mnpamo1fCrHA6Dg55CBeIEkg2oyPGKzvhkPVopJzj9zukW8w2F0iR5ll0QBl7XzGyy3EHmIsYsod3LSIh1g5A2zsscJ0ouorKGLwY1LK06a34JTkYz5p7lx6Nn6589Re8GwIDAQAB";
|
|
|
|
|
@ -50,7 +61,17 @@ public class RSAUtils {
|
|
|
|
|
public static String decryptBASE64Str(String encryptString) {
|
|
|
|
|
try {
|
|
|
|
|
byte[] decodeByte = decryptBASE64(encryptString);
|
|
|
|
|
return new String(decryptByPrivateKey(decodeByte, private_key));
|
|
|
|
|
return new String(decryptByPrivateKeyLongText(decodeByte, private_key));
|
|
|
|
|
} catch (Exception e) {
|
|
|
|
|
e.printStackTrace();
|
|
|
|
|
}
|
|
|
|
|
return "";
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static String decryptBASE64StrByPublic(String encryptString) {
|
|
|
|
|
try {
|
|
|
|
|
byte[] decodeByte = decryptBASE64(encryptString);
|
|
|
|
|
return new String(decryptByPublicKeyLongText(decodeByte, public_key));
|
|
|
|
|
} catch (Exception e) {
|
|
|
|
|
e.printStackTrace();
|
|
|
|
|
}
|
|
|
|
|
@ -60,7 +81,7 @@ public class RSAUtils {
|
|
|
|
|
public static String decryptBASE64StrLocal(String encryptString) {
|
|
|
|
|
try {
|
|
|
|
|
byte[] decodeByte = decryptBASE64(encryptString);
|
|
|
|
|
return new String(decryptByPrivateKey(decodeByte, private_key_local));
|
|
|
|
|
return new String(decryptByPrivateKeyLongText(decodeByte, private_key_local));
|
|
|
|
|
} catch (Exception e) {
|
|
|
|
|
e.printStackTrace();
|
|
|
|
|
}
|
|
|
|
|
@ -70,7 +91,7 @@ public class RSAUtils {
|
|
|
|
|
public static String decryptBASE64StrMqtt(String encryptString) {
|
|
|
|
|
try {
|
|
|
|
|
byte[] decodeByte = decryptBASE64(encryptString);
|
|
|
|
|
return new String(decryptByPrivateKey(decodeByte, private_key_mqtt));
|
|
|
|
|
return new String(decryptByPrivateKeyLongText(decodeByte, private_key_mqtt));
|
|
|
|
|
} catch (Exception e) {
|
|
|
|
|
e.printStackTrace();
|
|
|
|
|
}
|
|
|
|
|
@ -79,7 +100,7 @@ public class RSAUtils {
|
|
|
|
|
|
|
|
|
|
public static String encryptBASE64Str(String text) {
|
|
|
|
|
try {
|
|
|
|
|
return encryptBASE64(encryptByPublicKey(text.getBytes(), public_key));
|
|
|
|
|
return encryptBASE64(encryptByPublicKeyLongText(text.getBytes(), public_key));
|
|
|
|
|
} catch (Exception e) {
|
|
|
|
|
e.printStackTrace();
|
|
|
|
|
}
|
|
|
|
|
@ -88,7 +109,7 @@ public class RSAUtils {
|
|
|
|
|
|
|
|
|
|
public static String encryptBASE64StrLocal(String text) {
|
|
|
|
|
try {
|
|
|
|
|
return encryptBASE64(encryptByPublicKey(text.getBytes(), public_key_local));
|
|
|
|
|
return encryptBASE64(encryptByPublicKeyLongText(text.getBytes(), public_key_local));
|
|
|
|
|
} catch (Exception e) {
|
|
|
|
|
e.printStackTrace();
|
|
|
|
|
}
|
|
|
|
|
@ -97,7 +118,7 @@ public class RSAUtils {
|
|
|
|
|
|
|
|
|
|
public static String encryptBASE64StrMqtt(String text) {
|
|
|
|
|
try {
|
|
|
|
|
return encryptBASE64(encryptByPublicKey(text.getBytes(), public_key_mqtt));
|
|
|
|
|
return encryptBASE64(encryptByPublicKeyLongText(text.getBytes(), public_key_mqtt));
|
|
|
|
|
} catch (Exception e) {
|
|
|
|
|
e.printStackTrace();
|
|
|
|
|
}
|
|
|
|
|
@ -127,6 +148,44 @@ public class RSAUtils {
|
|
|
|
|
return cipher.doFinal(data);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 私钥分段解密
|
|
|
|
|
* @param encryptedData
|
|
|
|
|
* @param privateKey
|
|
|
|
|
* @return
|
|
|
|
|
* @throws Exception
|
|
|
|
|
*/
|
|
|
|
|
public static byte[] decryptByPrivateKeyLongText(byte[] encryptedData, String privateKey) throws Exception {
|
|
|
|
|
|
|
|
|
|
byte[] keyBytes = decryptBASE64(privateKey);
|
|
|
|
|
PKCS8EncodedKeySpec pkcs8KeySpec = new PKCS8EncodedKeySpec(keyBytes);
|
|
|
|
|
KeyFactory keyFactory = KeyFactory.getInstance(RSA);
|
|
|
|
|
Key privateK = keyFactory.generatePrivate(pkcs8KeySpec);
|
|
|
|
|
Cipher cipher = Cipher.getInstance(ECB_None_PKCS1_PADDING);
|
|
|
|
|
cipher.init(Cipher.DECRYPT_MODE, privateK);
|
|
|
|
|
|
|
|
|
|
int inputLen = encryptedData.length;
|
|
|
|
|
ByteArrayOutputStream out = new ByteArrayOutputStream();
|
|
|
|
|
int offSet = 0;
|
|
|
|
|
byte[] cache;
|
|
|
|
|
int i = 0;
|
|
|
|
|
// 对数据分段解密
|
|
|
|
|
while (inputLen - offSet > 0) {
|
|
|
|
|
if (inputLen - offSet > MAX_DECRYPT_BLOCK) {
|
|
|
|
|
|
|
|
|
|
cache = cipher.doFinal(encryptedData, offSet, MAX_DECRYPT_BLOCK);
|
|
|
|
|
} else {
|
|
|
|
|
cache = cipher.doFinal(encryptedData, offSet, inputLen - offSet);
|
|
|
|
|
}
|
|
|
|
|
out.write(cache, 0, cache.length);
|
|
|
|
|
i++;
|
|
|
|
|
offSet = i * MAX_DECRYPT_BLOCK;
|
|
|
|
|
}
|
|
|
|
|
byte[] decryptedData = out.toByteArray();
|
|
|
|
|
out.close();
|
|
|
|
|
return decryptedData;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 解密<br>
|
|
|
|
|
* 用公钥解密
|
|
|
|
|
@ -136,8 +195,7 @@ public class RSAUtils {
|
|
|
|
|
* @return
|
|
|
|
|
* @throws Exception
|
|
|
|
|
*/
|
|
|
|
|
public static byte[] decryptByPublicKey(byte[] data, String key)
|
|
|
|
|
throws Exception {
|
|
|
|
|
public static byte[] decryptByPublicKey(byte[] data, String key) throws Exception {
|
|
|
|
|
// 对密钥解密
|
|
|
|
|
byte[] keyBytes = decryptBASE64(key);
|
|
|
|
|
// 取得公钥
|
|
|
|
|
@ -150,6 +208,68 @@ public class RSAUtils {
|
|
|
|
|
return cipher.doFinal(data);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 用公钥分段解密
|
|
|
|
|
* @param encryptedData
|
|
|
|
|
* @param publicKey
|
|
|
|
|
* @return
|
|
|
|
|
* @throws Exception
|
|
|
|
|
*/
|
|
|
|
|
public static byte[] decryptByPublicKeyLongText(byte[] encryptedData, String publicKey) throws Exception {
|
|
|
|
|
|
|
|
|
|
byte[] keyBytes = decryptBASE64(publicKey);
|
|
|
|
|
|
|
|
|
|
X509EncodedKeySpec x509KeySpec = new X509EncodedKeySpec(keyBytes);
|
|
|
|
|
|
|
|
|
|
KeyFactory keyFactory = KeyFactory.getInstance(RSA);
|
|
|
|
|
|
|
|
|
|
Key publicK = keyFactory.generatePublic(x509KeySpec);
|
|
|
|
|
|
|
|
|
|
Cipher cipher = Cipher.getInstance(ECB_None_PKCS1_PADDING);
|
|
|
|
|
|
|
|
|
|
cipher.init(Cipher.DECRYPT_MODE, publicK);
|
|
|
|
|
|
|
|
|
|
int inputLen = encryptedData.length;
|
|
|
|
|
|
|
|
|
|
ByteArrayOutputStream out = new ByteArrayOutputStream();
|
|
|
|
|
|
|
|
|
|
int offSet = 0;
|
|
|
|
|
|
|
|
|
|
byte[] cache;
|
|
|
|
|
|
|
|
|
|
int i = 0;
|
|
|
|
|
|
|
|
|
|
// 对数据分段解密
|
|
|
|
|
|
|
|
|
|
while (inputLen - offSet > 0) {
|
|
|
|
|
|
|
|
|
|
if (inputLen - offSet > MAX_DECRYPT_BLOCK) {
|
|
|
|
|
|
|
|
|
|
cache = cipher.doFinal(encryptedData, offSet, MAX_DECRYPT_BLOCK);
|
|
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
|
|
|
|
|
cache = cipher.doFinal(encryptedData, offSet, inputLen - offSet);
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
out.write(cache, 0, cache.length);
|
|
|
|
|
|
|
|
|
|
i++;
|
|
|
|
|
|
|
|
|
|
offSet = i * MAX_DECRYPT_BLOCK;
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
byte[] decryptedData = out.toByteArray();
|
|
|
|
|
|
|
|
|
|
out.close();
|
|
|
|
|
|
|
|
|
|
return decryptedData;
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 加密<br>
|
|
|
|
|
* 用公钥加密
|
|
|
|
|
@ -159,8 +279,7 @@ public class RSAUtils {
|
|
|
|
|
* @return
|
|
|
|
|
* @throws Exception
|
|
|
|
|
*/
|
|
|
|
|
public static byte[] encryptByPublicKey(byte[] data, String key)
|
|
|
|
|
throws Exception {
|
|
|
|
|
public static byte[] encryptByPublicKey(byte[] data, String key) throws Exception {
|
|
|
|
|
// 对公钥解密
|
|
|
|
|
byte[] keyBytes = decryptBASE64(key);
|
|
|
|
|
// 取得公钥
|
|
|
|
|
@ -173,6 +292,69 @@ public class RSAUtils {
|
|
|
|
|
return cipher.doFinal(data);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 用公钥分段加密
|
|
|
|
|
* @param data
|
|
|
|
|
* @param publicKey
|
|
|
|
|
* @return
|
|
|
|
|
* @throws Exception
|
|
|
|
|
*/
|
|
|
|
|
public static byte[] encryptByPublicKeyLongText(byte[] data, String publicKey) throws Exception {
|
|
|
|
|
|
|
|
|
|
byte[] keyBytes = decryptBASE64(publicKey);
|
|
|
|
|
|
|
|
|
|
X509EncodedKeySpec x509KeySpec = new X509EncodedKeySpec(keyBytes);
|
|
|
|
|
|
|
|
|
|
KeyFactory keyFactory = KeyFactory.getInstance(RSA);
|
|
|
|
|
|
|
|
|
|
Key publicK = keyFactory.generatePublic(x509KeySpec);
|
|
|
|
|
|
|
|
|
|
// 对数据加密
|
|
|
|
|
|
|
|
|
|
Cipher cipher = Cipher.getInstance(ECB_None_PKCS1_PADDING);
|
|
|
|
|
|
|
|
|
|
cipher.init(Cipher.ENCRYPT_MODE, publicK);
|
|
|
|
|
|
|
|
|
|
int inputLen = data.length;
|
|
|
|
|
|
|
|
|
|
ByteArrayOutputStream out = new ByteArrayOutputStream();
|
|
|
|
|
|
|
|
|
|
int offSet = 0;
|
|
|
|
|
|
|
|
|
|
byte[] cache;
|
|
|
|
|
|
|
|
|
|
int i = 0;
|
|
|
|
|
|
|
|
|
|
// 对数据分段加密
|
|
|
|
|
|
|
|
|
|
while (inputLen - offSet > 0) {
|
|
|
|
|
|
|
|
|
|
if (inputLen - offSet > MAX_ENCRYPT_BLOCK) {
|
|
|
|
|
|
|
|
|
|
cache = cipher.doFinal(data, offSet, MAX_ENCRYPT_BLOCK);
|
|
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
|
|
|
|
|
cache = cipher.doFinal(data, offSet, inputLen - offSet);
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
out.write(cache, 0, cache.length);
|
|
|
|
|
|
|
|
|
|
i++;
|
|
|
|
|
|
|
|
|
|
offSet = i * MAX_ENCRYPT_BLOCK;
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
byte[] encryptedData = out.toByteArray();
|
|
|
|
|
|
|
|
|
|
out.close();
|
|
|
|
|
|
|
|
|
|
return encryptedData;
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 加密<br>
|
|
|
|
|
* 用私钥加密
|
|
|
|
|
@ -182,8 +364,7 @@ public class RSAUtils {
|
|
|
|
|
* @return
|
|
|
|
|
* @throws Exception
|
|
|
|
|
*/
|
|
|
|
|
public static byte[] encryptByPrivateKey(byte[] data, String key)
|
|
|
|
|
throws Exception {
|
|
|
|
|
public static byte[] encryptByPrivateKey(byte[] data, String key) throws Exception {
|
|
|
|
|
// 对密钥解密
|
|
|
|
|
byte[] keyBytes = decryptBASE64(key);
|
|
|
|
|
// 取得私钥
|
|
|
|
|
@ -196,6 +377,67 @@ public class RSAUtils {
|
|
|
|
|
return cipher.doFinal(data);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 用私钥分段加密
|
|
|
|
|
* @param data
|
|
|
|
|
* @param privateKey
|
|
|
|
|
* @return
|
|
|
|
|
* @throws Exception
|
|
|
|
|
*/
|
|
|
|
|
public static byte[] encryptByPrivateKeyLongText(byte[] data, String privateKey) throws Exception {
|
|
|
|
|
|
|
|
|
|
byte[] keyBytes = decryptBASE64(privateKey);
|
|
|
|
|
|
|
|
|
|
PKCS8EncodedKeySpec pkcs8KeySpec = new PKCS8EncodedKeySpec(keyBytes);
|
|
|
|
|
|
|
|
|
|
KeyFactory keyFactory = KeyFactory.getInstance(RSA);
|
|
|
|
|
|
|
|
|
|
Key privateK = keyFactory.generatePrivate(pkcs8KeySpec);
|
|
|
|
|
|
|
|
|
|
Cipher cipher = Cipher.getInstance(ECB_None_PKCS1_PADDING);
|
|
|
|
|
|
|
|
|
|
cipher.init(Cipher.ENCRYPT_MODE, privateK);
|
|
|
|
|
|
|
|
|
|
int inputLen = data.length;
|
|
|
|
|
|
|
|
|
|
ByteArrayOutputStream out = new ByteArrayOutputStream();
|
|
|
|
|
|
|
|
|
|
int offSet = 0;
|
|
|
|
|
|
|
|
|
|
byte[] cache;
|
|
|
|
|
|
|
|
|
|
int i = 0;
|
|
|
|
|
|
|
|
|
|
// 对数据分段加密
|
|
|
|
|
|
|
|
|
|
while (inputLen - offSet > 0) {
|
|
|
|
|
|
|
|
|
|
if (inputLen - offSet > MAX_ENCRYPT_BLOCK) {
|
|
|
|
|
|
|
|
|
|
cache = cipher.doFinal(data, offSet, MAX_ENCRYPT_BLOCK);
|
|
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
|
|
|
|
|
cache = cipher.doFinal(data, offSet, inputLen - offSet);
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
out.write(cache, 0, cache.length);
|
|
|
|
|
|
|
|
|
|
i++;
|
|
|
|
|
|
|
|
|
|
offSet = i * MAX_ENCRYPT_BLOCK;
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
byte[] encryptedData = out.toByteArray();
|
|
|
|
|
|
|
|
|
|
out.close();
|
|
|
|
|
|
|
|
|
|
return encryptedData;
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 取得私钥
|
|
|
|
|
*
|
|
|
|
|
@ -232,7 +474,7 @@ public class RSAUtils {
|
|
|
|
|
public static Map<String, Object> initKey() throws Exception {
|
|
|
|
|
KeyPairGenerator keyPairGen = KeyPairGenerator
|
|
|
|
|
.getInstance(RSA);
|
|
|
|
|
keyPairGen.initialize(2048);
|
|
|
|
|
keyPairGen.initialize(DEFAULT_KEY_SIZE);
|
|
|
|
|
KeyPair keyPair = keyPairGen.generateKeyPair();
|
|
|
|
|
// 公钥
|
|
|
|
|
RSAPublicKey publicKey = (RSAPublicKey) keyPair.getPublic();
|
|
|
|
|
|