Browse Source

精简代码

master
richie 6 years ago
parent
commit
16844a301f
  1. 127
      src/helper/RSAUtils.java

127
src/helper/RSAUtils.java

@ -7,12 +7,9 @@ import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.nio.charset.StandardCharsets;
import java.security.Key;
import java.security.KeyFactory;
import java.security.KeyPair;
import java.security.KeyPairGenerator;
import java.security.MessageDigest;
import java.security.PrivateKey;
import java.security.PublicKey;
import java.security.spec.PKCS8EncodedKeySpec;
@ -36,12 +33,8 @@ public class RSAUtils {
if (StringUtils.isEmpty(plainText)) {
return plainText;
}
try {
byte[] publicEncrypt = encrypt(plainText.getBytes(EncodeConstants.ENCODING_UTF_8), publicKey);
return RSAUtils.byte2Base64(publicEncrypt);
} catch (UnsupportedEncodingException e) {
throw new RuntimeException(e);
}
byte[] publicEncrypt = encrypt(plainText.getBytes(StandardCharsets.UTF_8), publicKey);
return RSAUtils.byte2Base64(publicEncrypt);
}
@ -65,15 +58,9 @@ public class RSAUtils {
if (bytes == null) {
return null;
}
try {
return new String(bytes, EncodeConstants.ENCODING_UTF_8);
} catch (UnsupportedEncodingException e) {
throw new RuntimeException(e);
}
return new String(bytes, StandardCharsets.UTF_8);
}
/**
* RSA加密
*
@ -98,14 +85,7 @@ public class RSAUtils {
private static byte[] dealEncryptFragment(byte[] data, Cipher cipher) throws IllegalBlockSizeException, BadPaddingException {
byte[] result = new byte[]{};
int i;
for (i = 0; i < data.length; i += FRAGMENT_LENGTH) {
byte[] fragment = ArrayUtils.subarray(data, i, i + FRAGMENT_LENGTH);
byte[] update = cipher.doFinal(fragment);
result = ArrayUtils.addAll(result, update);
}
return result;
return getBytes(data, cipher, FRAGMENT_LENGTH);
}
@ -130,100 +110,20 @@ public class RSAUtils {
private static byte[] dealDecryptFragment(byte[] data, Cipher cipher) throws IllegalBlockSizeException, BadPaddingException {
return getBytes(data, cipher, FRAGMENT_LENGTH_DECRYPT);
}
private static byte[] getBytes(byte[] data, Cipher cipher, int fragmentLengthDecrypt) throws IllegalBlockSizeException, BadPaddingException {
byte[] result = new byte[]{};
int i;
for (i = 0; i < data.length; i += FRAGMENT_LENGTH_DECRYPT) {
byte[] fragment = ArrayUtils.subarray(data, i, i + FRAGMENT_LENGTH_DECRYPT);
for (i = 0; i < data.length; i += fragmentLengthDecrypt) {
byte[] fragment = ArrayUtils.subarray(data, i, i + fragmentLengthDecrypt);
byte[] update = cipher.doFinal(fragment);
result = ArrayUtils.addAll(result, update);
}
return result;
}
public static String sha256(String plainTextData) {
if (StringUtils.isEmpty(plainTextData)) {
return plainTextData;
}
try {
byte[] bytes = sha256(plainTextData.getBytes(EncodeConstants.ENCODING_UTF_8));
return byteArrayToHexString(bytes);
} catch (UnsupportedEncodingException e) {
LoggerFactory.getLogger().error(e.getMessage(), e);
}
return plainTextData;
}
private static byte[] sha256(byte[] plainTextData) {
if (plainTextData == null || ArrayUtils.isEmpty(plainTextData)) {
return plainTextData;
}
try {
MessageDigest messageDigest = MessageDigest.getInstance("SHA-256");
messageDigest.update(plainTextData);
return messageDigest.digest();
} catch (Exception e) {
LoggerFactory.getLogger().error(e.getMessage(), e);
}
return plainTextData;
}
/**
* 将加密后的字节数组转换成字符串
*
* @param b 字节数组
* @return 字符串
*/
public static String byteArrayToHexString(byte[] b) {
StringBuilder hs = new StringBuilder();
String tempStr;
for (int n = 0; b != null && n < b.length; n++) {
tempStr = Integer.toHexString(b[n] & 0XFF);
if (tempStr.length() == 1)
hs.append('0');
hs.append(tempStr);
}
return hs.toString().toLowerCase();
}
/**
* 生成一个2048位的RSA秘钥对
*
* @return 秘钥对
* @throws Exception 如果无法生成秘钥对则抛出次异常
*/
public static KeyPair getKeyPair() throws Exception {
KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance("RSA");
keyPairGenerator.initialize(2048);
return keyPairGenerator.generateKeyPair();
}
/**
* 获取RSA公钥的base64编码字符串
*
* @param keyPair 秘钥对
* @return 公钥编码字符串
*/
public static String getPublicKey(KeyPair keyPair) {
PublicKey publicKey = keyPair.getPublic();
byte[] bytes = publicKey.getEncoded();
return byte2Base64(bytes);
}
/**
* 获取RSA私钥的base64编码字符串
*
* @param keyPair 秘钥对
* @return 私钥编码字符串
*/
public static String getPrivateKey(KeyPair keyPair) {
PrivateKey privateKey = keyPair.getPrivate();
byte[] bytes = privateKey.getEncoded();
return byte2Base64(bytes);
}
public static PublicKey string2PublicKey(String pubStr) {
try {
byte[] keyBytes = base642Byte(pubStr);
@ -257,9 +157,4 @@ public class RSAUtils {
BASE64Decoder decoder = new BASE64Decoder();
return decoder.decodeBuffer(base64Key);
}
public static void main(String... args) {
}
}

Loading…
Cancel
Save