RSA公私钥加解密方式-工具类
生活随笔
收集整理的這篇文章主要介紹了
RSA公私钥加解密方式-工具类
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
直接上代碼
? import java.io.ByteArrayOutputStream; import java.math.BigInteger; import java.security.*; 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 javax.crypto.Cipher; import org.apache.commons.codec.binary.Base64; /*** @author yao* @create 2018/1/20*/ public class RSAEncrypt {private static final String ALGORITHM = "RSA";private static final int MAX_ENCRYPT_BLOCK = 117;private static final int MAX_DECRYPT_BLOCK = 128;private static Map<Integer, String> keyMap = new HashMap<Integer, String>(); //用于封裝隨機產生的公鑰與私鑰public static final Integer PUBLICKEY = 0;//0表示公鑰public static final Integer PRIVATEKEY = 1;//1表示私鑰public RSAEncrypt() {}/*** 公鑰加密* @param str 明文參數* @param publicKey 公鑰* @return* @throws Exception*/public static String encryptPublic(String str, String publicKey) throws Exception {byte[] decoded = Base64.decodeBase64(publicKey);RSAPublicKey pubKey = (RSAPublicKey)KeyFactory.getInstance("RSA").generatePublic(new X509EncodedKeySpec(decoded));Cipher cipher = getCipher(1, pubKey);return splitEncrypt(str, cipher, pubKey.getModulus());}/*** 私鑰加密* @param str 明文參數* @param privateKey 私鑰* @return* @throws Exception*/public static String encryptPrivate(String str, String privateKey) throws Exception {byte[] decoded = Base64.decodeBase64(privateKey);RSAPrivateKey priKey = (RSAPrivateKey)KeyFactory.getInstance("RSA").generatePrivate(new PKCS8EncodedKeySpec(decoded));Cipher cipher = getCipher(1, priKey);return splitEncrypt(str, cipher, priKey.getModulus());}/*** 公鑰解密* @param str 密文參數* @param publicKey 公鑰* @return* @throws Exception*/public static String decryptPublic(String str, String publicKey) throws Exception {byte[] inputByte = Base64.decodeBase64(str.getBytes("UTF-8"));byte[] decoded = Base64.decodeBase64(publicKey);RSAPublicKey pubKey = (RSAPublicKey)KeyFactory.getInstance("RSA").generatePublic(new X509EncodedKeySpec(decoded));Cipher cipher = getCipher(2, pubKey);return splitDecrypt(str, cipher, pubKey.getModulus());}/*** 私鑰解密* @param str 密文參數* @param privateKey 私鑰* @return* @throws Exception*/public static String decryptPrivate(String str, String privateKey) throws Exception {byte[] inputByte = Base64.decodeBase64(str.getBytes("UTF-8"));byte[] decoded = Base64.decodeBase64(privateKey);RSAPrivateKey priKey = (RSAPrivateKey)KeyFactory.getInstance("RSA").generatePrivate(new PKCS8EncodedKeySpec(decoded));Cipher cipher = getCipher(2, priKey);return splitDecrypt(str, cipher, priKey.getModulus());}private static String splitDecrypt(String str, Cipher cipher, BigInteger modulus) throws Exception {byte[] bytes = Base64.decodeBase64(str);int inputLen = bytes.length;int offLen = 0;int i = 0;ByteArrayOutputStream byteArrayOutputStream;byte[] cache;for(byteArrayOutputStream = new ByteArrayOutputStream(); inputLen - offLen > 0; offLen = 128 * i) {if (inputLen - offLen > 128) {cache = cipher.doFinal(bytes, offLen, 128);} else {cache = cipher.doFinal(bytes, offLen, inputLen - offLen);}byteArrayOutputStream.write(cache);++i;}byteArrayOutputStream.close();cache = byteArrayOutputStream.toByteArray();return new String(cache);}private static Cipher getCipher(int model, Key key) throws Exception {Cipher cipher = Cipher.getInstance("RSA");cipher.init(model, key);return cipher;}private static String splitEncrypt(String str, Cipher cipher, BigInteger modulus) throws Exception {byte[] bytes = str.getBytes();int inputLen = bytes.length;int offLen = 0;int i = 0;ByteArrayOutputStream bops;byte[] cache;for(bops = new ByteArrayOutputStream(); inputLen - offLen > 0; offLen = 117 * i) {if (inputLen - offLen > 117) {cache = cipher.doFinal(bytes, offLen, 117);} else {cache = cipher.doFinal(bytes, offLen, inputLen - offLen);}bops.write(cache);++i;}bops.close();cache = bops.toByteArray();String encodeToString = Base64.encodeBase64String(cache);return encodeToString;}/*** 隨機生成密鑰對* @throws NoSuchAlgorithmException*/public static void genKeyPair() throws NoSuchAlgorithmException {// KeyPairGenerator類用于生成公鑰和私鑰對,基于RSA算法生成對象KeyPairGenerator keyPairGen = KeyPairGenerator.getInstance("RSA");// 初始化密鑰對生成器,密鑰大小為96-1024位keyPairGen.initialize(1024,new SecureRandom());// 生成一個密鑰對,保存在keyPair中KeyPair keyPair = keyPairGen.generateKeyPair();RSAPrivateKey privateKey = (RSAPrivateKey) keyPair.getPrivate(); // 得到私鑰RSAPublicKey publicKey = (RSAPublicKey) keyPair.getPublic(); // 得到公鑰String publicKeyString = new String(Base64.encodeBase64(publicKey.getEncoded()));// 得到私鑰字符串String privateKeyString = new String(Base64.encodeBase64((privateKey.getEncoded())));// 將公鑰和私鑰保存到MapkeyMap.put(PUBLICKEY ,publicKeyString); keyMap.put(PRIVATEKEY ,privateKeyString); }public static void main(String[] args) throws Exception {//參數String str = "{\"test\":\"001\"}";System.out.println("參數:" + str);//隨機生成密鑰對genKeyPair();//公鑰加密System.out.println("公鑰:" + keyMap.get(PUBLICKEY));System.out.println("私鑰:" + keyMap.get(PRIVATEKEY));String encrypt = RSAEncrypt.encryptPublic(str, keyMap.get(PUBLICKEY));System.out.println("公鑰加密:" + encrypt);//私鑰解密String decrypt = RSAEncrypt.decryptPrivate(encrypt, keyMap.get(PRIVATEKEY));System.out.println("私鑰解密:" + decrypt);//私鑰加密String encryptPrivate = RSAEncrypt.encryptPrivate(str, keyMap.get(PRIVATEKEY));System.out.println("私鑰加密:" + encryptPrivate);//公鑰解密String decryptPublic = RSAEncrypt.decryptPublic(encryptPrivate, keyMap.get(PUBLICKEY));System.out.println("公鑰解密:" + decryptPublic);} }?總結
以上是生活随笔為你收集整理的RSA公私钥加解密方式-工具类的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 年轻人换不动手机了 消息称联发科砍单5G
- 下一篇: 比混动丰田还省 吉利帝豪醇电版上市:12