JAVA RSA加密解密 分段加密解密
package com.neusoft.caeid.upms.license;import javax.crypto.Cipher;
import java.io.ByteArrayOutputStream;
import java.security.Key;
import java.security.KeyFactory;
import java.security.KeyPair;
import java.security.KeyPairGenerator;
import java.security.NoSuchAlgorithmException;
import java.security.PrivateKey;
import java.security.PublicKey;
import java.security.interfaces.RSAPrivateKey;
import java.security.interfaces.RSAPublicKey;
import java.security.spec.InvalidKeySpecException;
import java.security.spec.PKCS8EncodedKeySpec;
import java.security.spec.X509EncodedKeySpec;
import java.util.Base64;
import java.util.HashMap;
import java.util.Map;public class RSAUtil {public static final String KEY_ALGORITHM = "RSA";private static final String PUBLIC_KEY = "RSAPublicKey";private static final String PRIVATE_KEY = "RSAPrivateKey";private static final int MAX_ENCRYPT_BLOCK = 245;private static final int MAX_DECRYPT_BLOCK = 256;public static Map<String, Object> initKey() throws Exception {KeyPairGenerator keyPairGen = KeyPairGenerator.getInstance(KEY_ALGORITHM);keyPairGen.initialize(2048);KeyPair keyPair = keyPairGen.generateKeyPair();RSAPublicKey publicKey = (RSAPublicKey) keyPair.getPublic();RSAPrivateKey privateKey = (RSAPrivateKey) keyPair.getPrivate();Map<String, Object> keyMap = new HashMap(2);keyMap.put(PUBLIC_KEY, publicKey);keyMap.put(PRIVATE_KEY, privateKey);return keyMap;}public static String getPublicKeyStr(Map<String, Object> keyMap) throws Exception {Key key = (Key) keyMap.get(PUBLIC_KEY);return encryptBASE64(key.getEncoded());}public static String getPrivateKeyStr(Map<String, Object> keyMap) throws Exception {Key key = (Key) keyMap.get(PRIVATE_KEY);return encryptBASE64(key.getEncoded());}public static PublicKey getPublicKey(String publicKeyString) throws NoSuchAlgorithmException, InvalidKeySpecException {byte[] publicKeyByte = Base64.getDecoder().decode(publicKeyString);X509EncodedKeySpec keySpec = new X509EncodedKeySpec(publicKeyByte);KeyFactory keyFactory = KeyFactory.getInstance(KEY_ALGORITHM);PublicKey publicKey = keyFactory.generatePublic(keySpec);return publicKey;}public static PrivateKey getPrivateKey(String privateKeyString) throws Exception {byte[] privateKeyByte = Base64.getDecoder().decode(privateKeyString);PKCS8EncodedKeySpec keySpec = new PKCS8EncodedKeySpec(privateKeyByte);KeyFactory keyFactory = KeyFactory.getInstance(KEY_ALGORITHM);PrivateKey privateKey = keyFactory.generatePrivate(keySpec);return privateKey;}public static byte[] decryptBASE64(String key) {return Base64.getDecoder().decode(key);}public static String encryptBASE64(byte[] key) throws Exception {return new String(Base64.getEncoder().encode(key));}public static String encrypto(String text, String publicKeyStr) {try {System.out.println("明文lenth为"+text.length());Cipher cipher = Cipher.getInstance(KEY_ALGORITHM);cipher.init(Cipher.ENCRYPT_MODE, getPublicKey(publicKeyStr));byte tempBytes[] = cipher.doFinal(text.getBytes());String secretText = Base64.getEncoder().encodeToString(tempBytes);return secretText;} catch (Exception e) {throw new RuntimeException("加密字符串[" + text + "]时遇到异常", e);}}public static String decrypto(String secretText, String privateKeyStr) {try {Cipher cipher = Cipher.getInstance(KEY_ALGORITHM);cipher.init(Cipher.DECRYPT_MODE, getPrivateKey(privateKeyStr));byte[] secretText_decode = Base64.getDecoder().decode(secretText.getBytes());byte tempBytes[] = cipher.doFinal(secretText_decode);String text = new String(tempBytes);return text;} catch (Exception e) {throw new RuntimeException("解密字符串[" + secretText + "]时遇到异常", e);}}public static String encrypt(String plainText, String publicKeyStr) throws Exception {System.out.println("明文lenth为"+plainText.length());byte[] plainTextArray = plainText.getBytes();PublicKey publicKey = getPublicKey(publicKeyStr);Cipher cipher = Cipher.getInstance(KEY_ALGORITHM);cipher.init(Cipher.ENCRYPT_MODE, publicKey);int inputLen = plainTextArray.length;ByteArrayOutputStream out = new ByteArrayOutputStream();int offSet = 0;int i = 0;byte[] cache;while (inputLen - offSet > 0) {if (inputLen - offSet > MAX_ENCRYPT_BLOCK) {cache = cipher.doFinal(plainTextArray, offSet, MAX_ENCRYPT_BLOCK);} else {cache = cipher.doFinal(plainTextArray, offSet, inputLen - offSet);}out.write(cache, 0, cache.length);i++;offSet = i * MAX_ENCRYPT_BLOCK;}byte[] encryptText = out.toByteArray();out.close();return Base64.getEncoder().encodeToString(encryptText);}public static String decrypt(String encryptTextHex, String privateKeyStr) throws Exception {byte[] encryptText = Base64.getDecoder().decode(encryptTextHex);PrivateKey privateKey = getPrivateKey(privateKeyStr);Cipher cipher = Cipher.getInstance(KEY_ALGORITHM);cipher.init(Cipher.DECRYPT_MODE, privateKey);int inputLen = encryptText.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(encryptText, offSet, MAX_DECRYPT_BLOCK);} else {cache = cipher.doFinal(encryptText, offSet, inputLen - offSet);}out.write(cache, 0, cache.length);i++;offSet = i * MAX_DECRYPT_BLOCK;}byte[] plainText = out.toByteArray();out.close();return new String(plainText);}public static void main(String[] args) throws Exception {Map<String, Object> keyMap;String cipherText;String input = "数字测试爱上吃东西啊吃撒吃撒擦擦撒擦CCA擦擦擦C";try {keyMap = initKey();String publicKey = getPublicKeyStr(keyMap);System.out.println("公钥------------------");System.out.println(publicKey);System.out.println("length: " + publicKey.length());String privateKey = getPrivateKeyStr(keyMap);System.out.println("私钥------------------");System.out.println(privateKey);System.out.println("length: " + privateKey.length());cipherText = encrypt(input, publicKey);System.out.println("密文=======" + cipherText);System.out.println("length: " + cipherText.length());String plainText = decrypt(cipherText, privateKey);System.out.println("解密后明文===== " + plainText);} catch (Exception e) {e.printStackTrace();}}
}