RSA算法生成2048位公私钥
生活随笔
收集整理的這篇文章主要介紹了
RSA算法生成2048位公私钥
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
RSA生成2048位公私鑰
包含公鑰私鑰的生成和base64加密解密,需要的朋友拿走!
public class RSAUtils {/*** 密鑰長度 于原文長度對應 以及越長速度越慢*/private final static int KEY_SIZE = 2048;/*** 用于封裝隨機產生的公鑰與私鑰*/private static Map<Integer, String> keyMap = new HashMap<Integer, String>();/*** 隨機生成密鑰對*/public static void genKeyPair() throws NoSuchAlgorithmException {// KeyPairGenerator類用于生成公鑰和私鑰對,基于RSA算法生成對象KeyPairGenerator keyPairGen = KeyPairGenerator.getInstance("RSA");// 初始化密鑰對生成器keyPairGen.initialize(KEY_SIZE, new SecureRandom());// 生成一個密鑰對,保存在keyPair中KeyPair keyPair = keyPairGen.generateKeyPair();// 得到私鑰RSAPrivateKey privateKey = (RSAPrivateKey) keyPair.getPrivate();// 得到公鑰RSAPublicKey publicKey = (RSAPublicKey) keyPair.getPublic();String publicKeyString = Base64.getEncoder().encodeToString(publicKey.getEncoded());System.out.println("公鑰長度>>>"+publicKeyString.length());// 得到私鑰字符串String privateKeyString = Base64.getEncoder().encodeToString(privateKey.getEncoded());System.out.println("私鑰長度>>>"+privateKeyString.length());// 將公鑰和私鑰保存到Map//0表示公鑰keyMap.put(0, publicKeyString);//1表示私鑰keyMap.put(1, privateKeyString);}/*** RSA公鑰加密** @param str 加密字符串* @param publicKey 公鑰* @return 密文* @throws Exception 加密過程中的異常信息*/public static String encrypt(String str, String publicKey) throws Exception {//base64編碼的公鑰byte[] decoded = Base64.getDecoder().decode(publicKey);RSAPublicKey pubKey = (RSAPublicKey) KeyFactory.getInstance("RSA").generatePublic(new X509EncodedKeySpec(decoded));//RSA加密Cipher cipher = Cipher.getInstance("RSA");cipher.init(Cipher.ENCRYPT_MODE, pubKey);String outStr = Base64.getEncoder().encodeToString(cipher.doFinal(str.getBytes("UTF-8")));return outStr;}/*** RSA私鑰解密** @param str 加密字符串* @param privateKey 私鑰* @return 明文* @throws Exception 解密過程中的異常信息*/public static String decrypt(String str, String privateKey) throws Exception {//64位解碼加密后的字符串byte[] inputByte = Base64.getDecoder().decode(str);//base64編碼的私鑰byte[] decoded = Base64.getDecoder().decode(privateKey);RSAPrivateKey priKey = (RSAPrivateKey) KeyFactory.getInstance("RSA").generatePrivate(new PKCS8EncodedKeySpec(decoded));//RSA解密Cipher cipher = Cipher.getInstance("RSA");cipher.init(Cipher.DECRYPT_MODE, priKey);String outStr = new String(cipher.doFinal(inputByte));return outStr;}public static void main(String[] args) throws Exception {long temp = System.currentTimeMillis();//生成公鑰和私鑰genKeyPair();//加密字符串System.out.println("公鑰:" + keyMap.get(0));System.out.println("私鑰:" + keyMap.get(1));System.out.println("生成密鑰消耗時間:" + (System.currentTimeMillis() - temp) / 1000.0 + "秒");String message = "RSA測試---->";System.out.println("原文:" + message);temp = System.currentTimeMillis();String messageEn = encrypt(message, keyMap.get(0));System.out.println("密文:" + messageEn);System.out.println("加密消耗時間:" + (System.currentTimeMillis() - temp) / 1000.0 + "秒");temp = System.currentTimeMillis();String messageDe = decrypt(messageEn, keyMap.get(1));System.out.println("解密:" + messageDe);System.out.println("解密消耗時間:" + (System.currentTimeMillis() - temp) / 1000.0 + "秒");}總結
以上是生活随笔為你收集整理的RSA算法生成2048位公私钥的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 期刊格式
- 下一篇: 计算机基础是五笔吗,计算机基础(五笔打字