|
|
|
@ -4,6 +4,7 @@ package com.bjzc.zjyxdxj.utils;
|
|
|
|
import android.util.Base64;
|
|
|
|
import android.util.Base64;
|
|
|
|
import android.util.Log;
|
|
|
|
import android.util.Log;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
import java.io.ByteArrayOutputStream;
|
|
|
|
import java.security.Key;
|
|
|
|
import java.security.Key;
|
|
|
|
import java.security.KeyFactory;
|
|
|
|
import java.security.KeyFactory;
|
|
|
|
import java.security.KeyPair;
|
|
|
|
import java.security.KeyPair;
|
|
|
|
@ -24,11 +25,20 @@ public class RSAUtils {
|
|
|
|
* RSA算法
|
|
|
|
* RSA算法
|
|
|
|
*/
|
|
|
|
*/
|
|
|
|
public static final String RSA = "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 PUBLIC_KEY = "RSAPublicKey";
|
|
|
|
private static final String PRIVATE_KEY = "RSAPrivateKey";
|
|
|
|
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 = "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAhpRgDGj3dk8kaNnnQIJ6YjbaEDBYEzDkv3qR3a55odU4/fJ3LFtGQfHM7LzQ3bpYaC8cZiYAu0ByPENW0G7HzVmxPfKdbLZtrRe+9kH8Fm+M2B26/XZd9QfT9+F2NQtFq68UA6yV5Z4wvxDjp2ZEzVjd+6ODQHuxf5JzySyWtgg844sHdB47iSdC06PnDfjLGMp3AtYeCbQH1mNOjuV44vbiccoTP3gCLnm9BS9Ez4F0fM04kTnCLpOdphJub341gUUnuoFbo+CGrnM5NfwXnw1MUkl5H25BHwK4So/L9DwbBsktiFOuzC8L4DuWvJJSLZnKXIXmPGoRVqTe5zCMkQIDAQAB";
|
|
|
|
public static final String public_key = "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAhpRgDGj3dk8kaNnnQIJ6YjbaEDBYEzDkv3qR3a55odU4/fJ3LFtGQfHM7LzQ3bpYaC8cZiYAu0ByPENW0G7HzVmxPfKdbLZtrRe+9kH8Fm+M2B26/XZd9QfT9+F2NQtFq68UA6yV5Z4wvxDjp2ZEzVjd+6ODQHuxf5JzySyWtgg844sHdB47iSdC06PnDfjLGMp3AtYeCbQH1mNOjuV44vbiccoTP3gCLnm9BS9Ez4F0fM04kTnCLpOdphJub341gUUnuoFbo+CGrnM5NfwXnw1MUkl5H25BHwK4So/L9DwbBsktiFOuzC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ursnsWQB6BIcRbww9FSIpARXPbXS2FbqjN+3aS8REEVJrIOPP2PQ62SeQSnA/eZBy1DjIIwKBgD4prtg1nq18v8hAbsrCXba/mCENJIPozH4mo2BSKFOiZtjtHpH4MvJRk0YLxYxnuuW5rkN1BEDQhmytCE1haMaB6pKBjCfw4tV8D4pj6VYeJZuyHE96uEsPMntLPQ8GV/c83qHNcAxqWGNE+yQbTzOB7aWYgQ4VJescvzEuuzWHAoGAZiUQ16t2IQqzyn7flRUeW1H1xWQ3iXQ7TnaYGNh9LvW5Rb9aTDP7ut6Bp7IJS9fK74mnpSfi2+kXBEWAVrx9TigdO/ParytE/JtCcl793IWInvqj0p9niqcokd3+jTzyqWtDaelDBP+VYM/elfSh1UQfZOMjU/064bwBIId9DKECgYBYXBe6oU7QiMYxa3YwAi9CcY9HXX6HRF4MpzOsOQ8wer8uJi6ca2th+jssz4SMeYZHnb7Ae+aORD7X+LefVuQiolwigAeTFgDihveb6WgxkKMarbqO8Ww7+laoGQjHvMQMTSkzt6KyhzeNu3F7Ma5HZkfCqwPAHzTOunGWUosRvg==";
|
|
|
|
public static final String private_key = "MIIEvAIBADANBgkqhkiG9w0BAQEFAASCBKYwggSiAgEAAoIBAQCGlGAMaPd2TyRo2edAgnpiNtoQMFgTMOS/epHdrnmh1Tj98ncsW0ZB8czsvNDdulhoLxxmJgC7QHI8Q1bQbsfNWbE98p1stm2tF772QfwWb4zYHbr9dl31B9P34XY1C0WrrxQDrJXlnjC/EOOnZkTNWN37o4NAe7F/knPJLJa2CDzjiwd0HjuJJ0LTo+cN+MsYyncC1h4JtAfWY06O5Xji9uJxyhM/eAIueb0FL0TPgXR8zTiROcIuk52mEm5vfjWBRSe6gVuj4Iauczk1/BefDUxSSXkfbkEfArhKj8v0PBsGyS2IU67MLwvgO5a8klItmcpcheY8ahFWpN7nMIyRAgMBAAECggEAPkrplnTzrlh9subByrNacKGRGBM4gVGNYDAc2m6LMGRgp/MWRHrPL3D7+MyBJVC+4SKFU6bdic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8Ww7+laoGQjHvMQMTSkzt6KyhzeNu3F7Ma5HZkfCqwPAHzTOunGWUosRvg==";
|
|
|
|
@ -51,7 +61,17 @@ public class RSAUtils {
|
|
|
|
public static String decryptBASE64Str(String encryptString) {
|
|
|
|
public static String decryptBASE64Str(String encryptString) {
|
|
|
|
try {
|
|
|
|
try {
|
|
|
|
byte[] decodeByte = decryptBASE64(encryptString);
|
|
|
|
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) {
|
|
|
|
} catch (Exception e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
e.printStackTrace();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
@ -61,7 +81,7 @@ public class RSAUtils {
|
|
|
|
public static String decryptBASE64StrLocal(String encryptString) {
|
|
|
|
public static String decryptBASE64StrLocal(String encryptString) {
|
|
|
|
try {
|
|
|
|
try {
|
|
|
|
byte[] decodeByte = decryptBASE64(encryptString);
|
|
|
|
byte[] decodeByte = decryptBASE64(encryptString);
|
|
|
|
return new String(decryptByPrivateKey(decodeByte, private_key_local));
|
|
|
|
return new String(decryptByPrivateKeyLongText(decodeByte, private_key_local));
|
|
|
|
} catch (Exception e) {
|
|
|
|
} catch (Exception e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
e.printStackTrace();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
@ -71,7 +91,7 @@ public class RSAUtils {
|
|
|
|
public static String decryptBASE64StrMqtt(String encryptString) {
|
|
|
|
public static String decryptBASE64StrMqtt(String encryptString) {
|
|
|
|
try {
|
|
|
|
try {
|
|
|
|
byte[] decodeByte = decryptBASE64(encryptString);
|
|
|
|
byte[] decodeByte = decryptBASE64(encryptString);
|
|
|
|
return new String(decryptByPrivateKey(decodeByte, private_key_mqtt));
|
|
|
|
return new String(decryptByPrivateKeyLongText(decodeByte, private_key_mqtt));
|
|
|
|
} catch (Exception e) {
|
|
|
|
} catch (Exception e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
e.printStackTrace();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
@ -80,7 +100,7 @@ public class RSAUtils {
|
|
|
|
|
|
|
|
|
|
|
|
public static String encryptBASE64Str(String text) {
|
|
|
|
public static String encryptBASE64Str(String text) {
|
|
|
|
try {
|
|
|
|
try {
|
|
|
|
return encryptBASE64(encryptByPublicKey(text.getBytes(), public_key));
|
|
|
|
return encryptBASE64(encryptByPublicKeyLongText(text.getBytes(), public_key));
|
|
|
|
} catch (Exception e) {
|
|
|
|
} catch (Exception e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
e.printStackTrace();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
@ -89,7 +109,7 @@ public class RSAUtils {
|
|
|
|
|
|
|
|
|
|
|
|
public static String encryptBASE64StrLocal(String text) {
|
|
|
|
public static String encryptBASE64StrLocal(String text) {
|
|
|
|
try {
|
|
|
|
try {
|
|
|
|
return encryptBASE64(encryptByPublicKey(text.getBytes(), public_key_local));
|
|
|
|
return encryptBASE64(encryptByPublicKeyLongText(text.getBytes(), public_key_local));
|
|
|
|
} catch (Exception e) {
|
|
|
|
} catch (Exception e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
e.printStackTrace();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
@ -98,7 +118,7 @@ public class RSAUtils {
|
|
|
|
|
|
|
|
|
|
|
|
public static String encryptBASE64StrMqtt(String text) {
|
|
|
|
public static String encryptBASE64StrMqtt(String text) {
|
|
|
|
try {
|
|
|
|
try {
|
|
|
|
return encryptBASE64(encryptByPublicKey(text.getBytes(), public_key_mqtt));
|
|
|
|
return encryptBASE64(encryptByPublicKeyLongText(text.getBytes(), public_key_mqtt));
|
|
|
|
} catch (Exception e) {
|
|
|
|
} catch (Exception e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
e.printStackTrace();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
@ -128,6 +148,44 @@ public class RSAUtils {
|
|
|
|
return cipher.doFinal(data);
|
|
|
|
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>
|
|
|
|
* 解密<br>
|
|
|
|
* 用公钥解密
|
|
|
|
* 用公钥解密
|
|
|
|
@ -137,8 +195,7 @@ public class RSAUtils {
|
|
|
|
* @return
|
|
|
|
* @return
|
|
|
|
* @throws Exception
|
|
|
|
* @throws Exception
|
|
|
|
*/
|
|
|
|
*/
|
|
|
|
public static byte[] decryptByPublicKey(byte[] data, String key)
|
|
|
|
public static byte[] decryptByPublicKey(byte[] data, String key) throws Exception {
|
|
|
|
throws Exception {
|
|
|
|
|
|
|
|
// 对密钥解密
|
|
|
|
// 对密钥解密
|
|
|
|
byte[] keyBytes = decryptBASE64(key);
|
|
|
|
byte[] keyBytes = decryptBASE64(key);
|
|
|
|
// 取得公钥
|
|
|
|
// 取得公钥
|
|
|
|
@ -151,6 +208,68 @@ public class RSAUtils {
|
|
|
|
return cipher.doFinal(data);
|
|
|
|
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>
|
|
|
|
* 加密<br>
|
|
|
|
* 用公钥加密
|
|
|
|
* 用公钥加密
|
|
|
|
@ -160,8 +279,7 @@ public class RSAUtils {
|
|
|
|
* @return
|
|
|
|
* @return
|
|
|
|
* @throws Exception
|
|
|
|
* @throws Exception
|
|
|
|
*/
|
|
|
|
*/
|
|
|
|
public static byte[] encryptByPublicKey(byte[] data, String key)
|
|
|
|
public static byte[] encryptByPublicKey(byte[] data, String key) throws Exception {
|
|
|
|
throws Exception {
|
|
|
|
|
|
|
|
// 对公钥解密
|
|
|
|
// 对公钥解密
|
|
|
|
byte[] keyBytes = decryptBASE64(key);
|
|
|
|
byte[] keyBytes = decryptBASE64(key);
|
|
|
|
// 取得公钥
|
|
|
|
// 取得公钥
|
|
|
|
@ -174,6 +292,69 @@ public class RSAUtils {
|
|
|
|
return cipher.doFinal(data);
|
|
|
|
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>
|
|
|
|
* 加密<br>
|
|
|
|
* 用私钥加密
|
|
|
|
* 用私钥加密
|
|
|
|
@ -183,8 +364,7 @@ public class RSAUtils {
|
|
|
|
* @return
|
|
|
|
* @return
|
|
|
|
* @throws Exception
|
|
|
|
* @throws Exception
|
|
|
|
*/
|
|
|
|
*/
|
|
|
|
public static byte[] encryptByPrivateKey(byte[] data, String key)
|
|
|
|
public static byte[] encryptByPrivateKey(byte[] data, String key) throws Exception {
|
|
|
|
throws Exception {
|
|
|
|
|
|
|
|
// 对密钥解密
|
|
|
|
// 对密钥解密
|
|
|
|
byte[] keyBytes = decryptBASE64(key);
|
|
|
|
byte[] keyBytes = decryptBASE64(key);
|
|
|
|
// 取得私钥
|
|
|
|
// 取得私钥
|
|
|
|
@ -197,6 +377,67 @@ public class RSAUtils {
|
|
|
|
return cipher.doFinal(data);
|
|
|
|
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;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
/**
|
|
|
|
* 取得私钥
|
|
|
|
* 取得私钥
|
|
|
|
*
|
|
|
|
*
|
|
|
|
@ -233,7 +474,7 @@ public class RSAUtils {
|
|
|
|
public static Map<String, Object> initKey() throws Exception {
|
|
|
|
public static Map<String, Object> initKey() throws Exception {
|
|
|
|
KeyPairGenerator keyPairGen = KeyPairGenerator
|
|
|
|
KeyPairGenerator keyPairGen = KeyPairGenerator
|
|
|
|
.getInstance(RSA);
|
|
|
|
.getInstance(RSA);
|
|
|
|
keyPairGen.initialize(2048);
|
|
|
|
keyPairGen.initialize(DEFAULT_KEY_SIZE);
|
|
|
|
KeyPair keyPair = keyPairGen.generateKeyPair();
|
|
|
|
KeyPair keyPair = keyPairGen.generateKeyPair();
|
|
|
|
// 公钥
|
|
|
|
// 公钥
|
|
|
|
RSAPublicKey publicKey = (RSAPublicKey) keyPair.getPublic();
|
|
|
|
RSAPublicKey publicKey = (RSAPublicKey) keyPair.getPublic();
|
|
|
|
|