package com.xxxx.tax.util;import com.xxxxx.tax.util.ExceptionUtil;import lombok.extern.log4j.Log4j;import java.io.ByteArrayOutputStream;import java.security.Key;import java.security.KeyFactory;import java.security.KeyPair;import java.security.KeyPairGenerator;import java.security.PrivateKey;import java.security.PublicKey;import java.security.Signature;import java.security.interfaces.RSAPrivateKey;import java.security.interfaces.RSAPublicKey;import java.security.spec.PKCS8EncodedKeySpec;import java.security.spec.X509EncodedKeySpec;import java.util.HashMap;import java.util.Map;import org.apache.commons.codec.binary.Base64;import javax.crypto.Cipher;@Log4jpublic class RSAUtils { public static final String KEY_ALGORITHM = "RSA"; public static final String SIGNATURE_ALGORITHM = "MD5withRSA"; private static final String PUBLIC_KEY = "RSAPublicKey"; private static final String PRIVATE_KEY = "RSAPrivateKey"; private static final int MAX_ENCRYPT_BLOCK = 117; private static final int MAX_DECRYPT_BLOCK = 128; public static Map genKeyPair() throws Exception { KeyPairGenerator keyPairGen = KeyPairGenerator.getInstance(KEY_ALGORITHM); keyPairGen.initialize(1024); KeyPair keyPair = keyPairGen.generateKeyPair(); RSAPublicKey publicKey = (RSAPublicKey) keyPair.getPublic(); RSAPrivateKey privateKey = (RSAPrivateKey) keyPair.getPrivate(); Map keyMap = new HashMap(2); String publicKeyString = new String(Base64Util.encode(publicKey.getEncoded())); // 得到私钥字符串 String privateKeyString = new String(Base64Util.encode((privateKey.getEncoded()))); System.out.println(publicKeyString); System.out.println(privateKeyString); keyMap.put(PUBLIC_KEY, publicKey); keyMap.put(PRIVATE_KEY, privateKey); return keyMap; } public static String sign(byte[] data, String privateKey) throws Exception { byte[] keyBytes = Base64Util.decode(privateKey); PKCS8EncodedKeySpec pkcs8KeySpec = new PKCS8EncodedKeySpec(keyBytes); KeyFactory keyFactory = KeyFactory.getInstance(KEY_ALGORITHM); PrivateKey privateK = keyFactory.generatePrivate(pkcs8KeySpec); Signature signature = Signature.getInstance(SIGNATURE_ALGORITHM); signature.initSign(privateK); signature.update(data); return Base64Util.encode(signature.sign()); } public static boolean verify(byte[] data, String publicKey, String sign) throws Exception { byte[] keyBytes = Base64Util.decode(publicKey); X509EncodedKeySpec keySpec = new X509EncodedKeySpec(keyBytes); KeyFactory keyFactory = KeyFactory.getInstance(KEY_ALGORITHM); PublicKey publicK = keyFactory.generatePublic(keySpec); Signature signature = Signature.getInstance(SIGNATURE_ALGORITHM); signature.initVerify(publicK); signature.update(data); return signature.verify(Base64Util.decode(sign)); } public static String decryptByPrivateKey(byte[] encryptedData, String privateKey) throws Exception { byte[] keyBytes = Base64Util.decode(privateKey); PKCS8EncodedKeySpec pkcs8KeySpec = new PKCS8EncodedKeySpec(keyBytes); KeyFactory keyFactory = KeyFactory.getInstance(KEY_ALGORITHM); Key privateK = keyFactory.generatePrivate(pkcs8KeySpec); Cipher cipher = Cipher.getInstance("RSA/ECB/PKCS1Padding"); 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 Base64Util.encode(decryptedData); } public static String decryptByPublicKey(String publicKeyText, String text) throws Exception { X509EncodedKeySpec x509EncodedKeySpec = new X509EncodedKeySpec(Base64.decodeBase64(publicKeyText)); KeyFactory keyFactory = KeyFactory.getInstance("RSA"); PublicKey publicKey = keyFactory.generatePublic(x509EncodedKeySpec); Cipher cipher = Cipher.getInstance("RSA"); cipher.init(Cipher.DECRYPT_MODE, publicKey); byte[] result = cipher.doFinal(Base64.decodeBase64(text)); return new String(result); } public static byte[] decryptByPublicKey(byte[] encryptedData, String publicKey) throws Exception { byte[] keyBytes = Base64Util.decode(publicKey); X509EncodedKeySpec x509KeySpec = new X509EncodedKeySpec(keyBytes); KeyFactory keyFactory = KeyFactory.getInstance(KEY_ALGORITHM); Key publicK = keyFactory.generatePublic(x509KeySpec); Cipher cipher = Cipher.getInstance("RSA/ECB/PKCS1Padding"); 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; } public static String encryptByPublicKey(byte[] data, String publicKey) throws Exception { byte[] keyBytes = Base64Util.decode(publicKey); X509EncodedKeySpec x509KeySpec = new X509EncodedKeySpec(keyBytes); KeyFactory keyFactory = KeyFactory.getInstance(KEY_ALGORITHM); Key publicK = keyFactory.generatePublic(x509KeySpec); // 对数据加密 Cipher cipher = Cipher.getInstance("RSA/ECB/PKCS1Padding"); 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 Base64Util.encode(encryptedData); return bytesToHexString(encryptedData); } public static String encryptByPublicKeyByBase(byte[] data, String publicKey) throws Exception { byte[] keyBytes = Base64Util.decode(publicKey); X509EncodedKeySpec x509KeySpec = new X509EncodedKeySpec(keyBytes); KeyFactory keyFactory = KeyFactory.getInstance(KEY_ALGORITHM); Key publicK = keyFactory.generatePublic(x509KeySpec); // 对数据加密 Cipher cipher = Cipher.getInstance("RSA/ECB/PKCS1Padding"); 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 Base64Util.encode(encryptedData); // return bytesToHexString(encryptedData); } public static String bytesToHexString(byte[] bytes) { StringBuffer sb = new StringBuffer(bytes.length); String temp = null; for (int i = 0; i < bytes.length; i++) { temp = Integer.toHexString(0xFF & bytes[i]); if (temp.length() < 2) { sb.append(0); } sb.append(temp); } return sb.toString(); } public static byte[] encryptByPrivateKey(byte[] data, String privateKey) throws Exception { byte[] keyBytes = Base64Util.decode(privateKey); PKCS8EncodedKeySpec pkcs8KeySpec = new PKCS8EncodedKeySpec(keyBytes); KeyFactory keyFactory = KeyFactory.getInstance(KEY_ALGORITHM); Key privateK = keyFactory.generatePrivate(pkcs8KeySpec); Cipher cipher = Cipher.getInstance(keyFactory.getAlgorithm()); 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; } public static String getPrivateKey(Map keyMap) throws Exception { Key key = (Key) keyMap.get(PRIVATE_KEY); return Base64Util.encode(key.getEncoded()); } public static String getPublicKey(Map keyMap) throws Exception { Key key = (Key) keyMap.get(PUBLIC_KEY); return Base64Util.encode(key.getEncoded()); } public static void main(String[] args){ try {//RSAUtils.genKeyPair(); String IMAGE_PRIVATEKEY = "MIICdgIBADANBgkqhkiG9w0BAQEFAASCAmAwggJcAgEAAoGBALwNwRLuWJo7gzOIJDQSa2tnsu52bEESmI1yC+/pPEQ4tqXiuyhAqctwFXY97qLakUBdoGs/4CcjU9yTVjFMBE5kyaQncF83TwIoDyZlS5VoYL8gcNXt9RTemlURVYXpu1WxoOD60X6T24uUrko4AiEpu5E2HtmNypELxlJUPWqrAgMBAAECgYA3Mv5adTXyx8dCSBMAaeu4Ei9WidJK44997V0NpfO3vsH2PXBcZ2avvY2/MHystuVt9E2TTaOZOWhzVZg2Ti7w5j/L3P9WpgSB4M4Rpy2WaI31cLLu9cmRgfuQCNHJKwevlp9h144F65XJpHJjkCyP/rW5QvPRtoA/d0ujA4C10QJBAPY+HW5wl139ALfRVJx/GHdxMNLsJskJPwLAT/+vJu5eVerF3d/nQgpuDyEmNN0GvNhrZ/liP1i9IWBsy26TlJMCQQDDgV75r2TEt8wp4mqKP9ufm4tkmidsPlAAsx8G8/ETffekROv9ksWjSdTxvcQ5Z7AFfVq4NFKiXMOG86pwqniJAkAgvMda7HsjUsSqq5jtKXSORK0yDZVmeuU2r2yzWIz7ee1ARkgmQW+lRVabmKHElOW8fCMZo7c6TALI+A3fyJ0jAkEArgw2aUJkC101PPR8tKc09lqbNeymvA6dWoIcVSxGqnuyQ2O4U/6eREgucyfl0NUmNzzpzqOGDVVYHIRAcBzTQQJACt5qNapy/2id4m4KigEcEuNAVmjRL82fXeRdoNbCfch7xWDnszPR1vSXxR1uE2lXD0E2SBbRXOJSwuZpVhDPKw=="; String IMAGE_PUBLICKEY = "MIGfMA0GCSqGSIb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G539S2X30YGTKpPLWAL/zsmSYoFR6uZm0nM+gwHA8kR+EYK+XAVvPNcb/ULKkhUq0ZaaT3Rbsz04UMmXiVFanOtmDIAsAsnq8fAuuInpzn/vhqEXofGADtrgg5SlMAg5Czw3koycIR1YxuQBxnNjGRpJA9pJR3BMy3/Fyus8GD4mGIwnIwfHKvyuWuYTx2LzvKQRQa/qf3u2w0Wl3mC6is9hubvPg/njv/PiB7mJbz/Ec3+r1nfPXBwAvFM7gj59KEhvJuFo04HwjEaSl7XZGMh3YKlsTZnmRrArBNZzhBGxt3mRUZ0sZ93Yev2QUaw="; String res = new String(Base64Util.decode(RSAUtils.decryptByPrivateKey(Base64Util.decode(str), IMAGE_PRIVATEKEY))); System.out.println(res); String ss = RSAUtils.encryptByPublicKey("abcdef".getBytes(), IMAGE_PUBLICKEY); System.out.println(ss);} catch (Exception e) {log.error(ExceptionUtil.getMessage(e));} }}
如有问题,请私信。
xObP8s/gudi/zrPMoaJKU7K5u7e+s7/Os8yhokpBVkHP4LnYv86zzMjn0OjSqtKyv8nS1MGqz7VRUaGjDQoNCtf31d8gUVEgNDA0NTQwMjI5
题,请私信
来源地址:https://blog.csdn.net/liberty888/article/details/131835997