日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

RSA算法生成2048位公私钥

發(fā)布時間:2023/12/14 编程问答 41 豆豆
生活随笔 收集整理的這篇文章主要介紹了 RSA算法生成2048位公私钥 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

RSA生成2048位公私鑰

包含公鑰私鑰的生成和base64加密解密,需要的朋友拿走!

public class RSAUtils {/*** 密鑰長度 于原文長度對應 以及越長速度越慢*/private final static int KEY_SIZE = 2048;/*** 用于封裝隨機產(chǎn)生的公鑰與私鑰*/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 + "秒");}

總結(jié)

以上是生活随笔為你收集整理的RSA算法生成2048位公私钥的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。