You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
160 lines
4.9 KiB
160 lines
4.9 KiB
package helper; |
|
|
|
import sun.misc.BASE64Decoder; |
|
import sun.misc.BASE64Encoder; |
|
|
|
import javax.crypto.BadPaddingException; |
|
import javax.crypto.Cipher; |
|
import javax.crypto.IllegalBlockSizeException; |
|
import java.io.IOException; |
|
import java.nio.charset.StandardCharsets; |
|
import java.security.Key; |
|
import java.security.KeyFactory; |
|
import java.security.PrivateKey; |
|
import java.security.PublicKey; |
|
import java.security.spec.PKCS8EncodedKeySpec; |
|
import java.security.spec.X509EncodedKeySpec; |
|
|
|
|
|
public class RSAUtils { |
|
|
|
private static final int FRAGMENT_LENGTH = 245; |
|
private static final int FRAGMENT_LENGTH_DECRYPT = 256; |
|
|
|
|
|
/** |
|
* RSA加密 |
|
* |
|
* @param plainText 要加密的文本内容 |
|
* @param publicKey 用于加密的公钥 |
|
* @return 加密后的内容 |
|
*/ |
|
public static String encrypt(String plainText, Key publicKey) { |
|
if (StringUtils.isEmpty(plainText)) { |
|
return plainText; |
|
} |
|
byte[] publicEncrypt = encrypt(plainText.getBytes(StandardCharsets.UTF_8), publicKey); |
|
return RSAUtils.byte2Base64(publicEncrypt); |
|
} |
|
|
|
|
|
/** |
|
* RSA解密 |
|
* |
|
* @param cipherText 密文数据 |
|
* @return 解密后的内容 |
|
*/ |
|
public static String decrypt(String cipherText, Key privateKey) { |
|
|
|
if (StringUtils.isEmpty(cipherText)) { |
|
return cipherText; |
|
} |
|
byte[] bytes = null; |
|
try { |
|
bytes = decrypt(base642Byte(cipherText), privateKey); |
|
} catch (Exception e) { |
|
LoggerFactory.getLogger().error(e.getMessage(), e); |
|
} |
|
if (bytes == null) { |
|
return null; |
|
} |
|
return new String(bytes, StandardCharsets.UTF_8); |
|
} |
|
|
|
/** |
|
* RSA加密 |
|
* |
|
* @param plainTextData 要加密的内容 |
|
* @param publicKey 用于加密的公钥 |
|
* @return 加密后的内容 |
|
*/ |
|
public static byte[] encrypt(byte[] plainTextData, Key publicKey) { |
|
|
|
if (ArrayUtils.isEmpty(plainTextData)) { |
|
return plainTextData; |
|
} |
|
try { |
|
Cipher c1 = Cipher.getInstance("RSA"); |
|
c1.init(Cipher.ENCRYPT_MODE, publicKey); |
|
return dealEncryptFragment(plainTextData, c1); |
|
} catch (Exception e) { |
|
LoggerFactory.getLogger().error(e.getMessage(), e); |
|
} |
|
return null; |
|
} |
|
|
|
private static byte[] dealEncryptFragment(byte[] data, Cipher cipher) throws IllegalBlockSizeException, BadPaddingException { |
|
|
|
return getBytes(data, cipher, FRAGMENT_LENGTH); |
|
} |
|
|
|
|
|
/** |
|
* RSA解密 |
|
* |
|
* @param cipherData 密文数据 |
|
* @param privateKey 用于解密的私钥 |
|
* @return 解密后的内容 |
|
*/ |
|
public static byte[] decrypt(byte[] cipherData, Key privateKey) { |
|
try { |
|
Cipher c1 = Cipher.getInstance("RSA"); |
|
c1.init(Cipher.DECRYPT_MODE, privateKey); |
|
return dealDecryptFragment(cipherData, c1); |
|
} catch (Exception e) { |
|
LoggerFactory.getLogger().error(e.getMessage(), e); |
|
} |
|
return null; |
|
} |
|
|
|
|
|
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 += fragmentLengthDecrypt) { |
|
byte[] fragment = ArrayUtils.subarray(data, i, i + fragmentLengthDecrypt); |
|
byte[] update = cipher.doFinal(fragment); |
|
result = ArrayUtils.addAll(result, update); |
|
} |
|
return result; |
|
} |
|
|
|
public static PublicKey string2PublicKey(String pubStr) { |
|
try { |
|
byte[] keyBytes = base642Byte(pubStr); |
|
X509EncodedKeySpec keySpec = new X509EncodedKeySpec(keyBytes); |
|
KeyFactory keyFactory = KeyFactory.getInstance("RSA"); |
|
return keyFactory.generatePublic(keySpec); |
|
} catch (Exception e) { |
|
LoggerFactory.getLogger().error(e.getMessage(), e); |
|
} |
|
return null; |
|
} |
|
|
|
public static PrivateKey string2PrivateKey(String priStr) { |
|
try { |
|
byte[] keyBytes = base642Byte(priStr); |
|
PKCS8EncodedKeySpec keySpec = new PKCS8EncodedKeySpec(keyBytes); |
|
KeyFactory keyFactory = KeyFactory.getInstance("RSA"); |
|
return keyFactory.generatePrivate(keySpec); |
|
} catch (Exception e) { |
|
LoggerFactory.getLogger().error(e.getMessage(), e); |
|
} |
|
return null; |
|
} |
|
|
|
public static String byte2Base64(byte[] bytes) { |
|
BASE64Encoder encoder = new BASE64Encoder(); |
|
return encoder.encode(bytes); |
|
} |
|
|
|
public static byte[] base642Byte(String base64Key) throws IOException { |
|
BASE64Decoder decoder = new BASE64Decoder(); |
|
return decoder.decodeBuffer(base64Key); |
|
} |
|
}
|
|
|