java使用RSA工具進(jìn)行信息加解密
我們?cè)陂_(kāi)發(fā)中需要對(duì)用戶敏感數(shù)據(jù)進(jìn)行加解密,比如密碼
這邊科普一下RSA算法
RSA是非對(duì)稱加密算法,與對(duì)稱加密算法不同;在對(duì)稱加密中,相同的密鑰用于加密和解密數(shù)據(jù),因此密鑰的安全性至關(guān)重要;而在RSA非對(duì)稱加密中,有兩個(gè)密鑰,一個(gè)是公鑰,用于加密數(shù)據(jù),另一個(gè)是私鑰,用于解密數(shù)據(jù);這意味著公鑰可以公開(kāi)分發(fā),而私鑰必須保持秘密;
RSA非對(duì)稱加密的主要應(yīng)用包括:
數(shù)據(jù)加密:使用接收者的公鑰加密數(shù)據(jù),只有擁有相應(yīng)私鑰的接收者才能解密;
數(shù)字簽名:使用發(fā)送者的私鑰對(duì)數(shù)據(jù)簽名,接收者可以使用發(fā)送者的公鑰驗(yàn)證簽名,確保數(shù)據(jù)的完整性和來(lái)源的真實(shí)性;
密鑰協(xié)商:RSA也用于安全協(xié)議中,如TLS/SSL,用于安全地交換對(duì)稱加密密鑰,從而實(shí)現(xiàn)保密通信;
非對(duì)稱加密算法提供了更高的安全性,因?yàn)榧用芎徒饷苁褂貌煌拿荑€,攻擊者無(wú)法從公鑰推導(dǎo)出私鑰;但由于非對(duì)稱加密計(jì)算成本高昂,通常不用于大規(guī)模數(shù)據(jù)的加密,而是用于安全協(xié)商和數(shù)字簽名等場(chǎng)景
今天就實(shí)現(xiàn)了一個(gè)RSA工具類,可以很輕松的對(duì)數(shù)據(jù)進(jìn)行加解密
不需要加依賴,代碼如下
public class RSAUtils { /** * @param plaintext 要加密的字符串 * @param publicKeyStr 傳入的公鑰,是一個(gè)字符串 * @return 加密后的字符串, 以Base64編碼的形式返回 * @throws Exception 異常 * 這個(gè)方法接受一個(gè)要加密的字符串和一個(gè)公鑰字符串,使用公鑰進(jìn)行加密,然后返回加密后的字符串 */ public static String encrypt(String plaintext, String publicKeyStr) throws Exception { PublicKey publicKey = getPublicKeyFromString(publicKeyStr); Cipher cipher = Cipher.getInstance("RSA"); cipher.init(Cipher.ENCRYPT_MODE, publicKey); byte[] encryptedBytes = cipher.doFinal(plaintext.getBytes()); return Base64.getEncoder().encodeToString(encryptedBytes); } /** * @param encryptedText 要解密的字符串 * @param privateKeyStr 傳入的私鑰,是一個(gè)字符串 * @return 解密后的原始字符串 * @throws Exception 異常 * 這個(gè)方法接受一個(gè)要解密的字符串和一個(gè)私鑰字符串,使用私鑰進(jìn)行解密,然后返回解密后的原始字符串 */ public static String decrypt(String encryptedText, String privateKeyStr) throws Exception { PrivateKey privateKey = getPrivateKeyFromString(privateKeyStr); Cipher cipher = Cipher.getInstance("RSA"); cipher.init(Cipher.DECRYPT_MODE, privateKey); byte[] encryptedBytes = Base64.getDecoder().decode(encryptedText); byte[] decryptedBytes = cipher.doFinal(encryptedBytes); return new String(decryptedBytes); } /** * @return * @throws Exception * 隨機(jī)生成一個(gè)長(zhǎng)度為2048的RSA公私鑰對(duì) */ public static KeyPair generateKeyPair() throws Exception { KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance("RSA"); keyPairGenerator.initialize(2048); return keyPairGenerator.generateKeyPair(); } /** * @param publicKey * @return * 拿出剛生成Base64格式的私鑰對(duì)的公鑰字符串 */ public static String publicKeyToString(PublicKey publicKey) { return Base64.getEncoder().encodeToString(publicKey.getEncoded()); } /** * @param privateKey * @return * 拿出剛生成Base64格式的私鑰對(duì)的私鑰字符串 */ public static String privateKeyToString(PrivateKey privateKey) { return Base64.getEncoder().encodeToString(privateKey.getEncoded()); } /** * @param publicKeyStr * @return 公鑰私鑰對(duì)象 * @throws Exception * 將剛拿出的Base64格式的私鑰對(duì)的私鑰字符串生成公鑰對(duì)象 */ public static PublicKey getPublicKeyFromString(String publicKeyStr) throws Exception { byte[] publicKeyBytes = Base64.getDecoder().decode(publicKeyStr); X509EncodedKeySpec spec = new X509EncodedKeySpec(publicKeyBytes); KeyFactory keyFactory = KeyFactory.getInstance("RSA"); return keyFactory.generatePublic(spec); } /** * @param privateKeyStr * @return * @throws Exception * 將剛拿出的Base64格式的私鑰對(duì)的私鑰字符串生成私鑰對(duì)象 */ public static PrivateKey getPrivateKeyFromString(String privateKeyStr) throws Exception { byte[] privateKeyBytes = Base64.getDecoder().decode(privateKeyStr); PKCS8EncodedKeySpec spec = new PKCS8EncodedKeySpec(privateKeyBytes); KeyFactory keyFactory = KeyFactory.getInstance("RSA"); return keyFactory.generatePrivate(spec); } public static void main(String[] args) throws Exception { // 生成RSA密鑰對(duì) KeyPair keyPair = generateKeyPair(); PublicKey publicKey = keyPair.getPublic(); PrivateKey privateKey = keyPair.getPrivate(); // 將公鑰和私鑰轉(zhuǎn)換為字符串 String publicKeyStr = publicKeyToString(publicKey); String privateKeyStr = privateKeyToString(privateKey); System.out.println("公鑰: " + publicKeyStr); System.out.println("私鑰: " + privateKeyStr); // 加密和解密測(cè)試 String plaintext = "大白貓真厲害"; String encryptedText = encrypt(plaintext, publicKeyStr); System.out.println("加密后的子串: " + encryptedText); String decryptedText = decrypt(encryptedText, privateKeyStr); System.out.println("解密后的子串: " + decryptedText); } }
結(jié)果如下
將數(shù)據(jù)用公鑰加密,用私鑰解密,這樣就可以了
到此這篇關(guān)于java使用RSA工具進(jìn)行信息加解密的文章就介紹到這了,更多相關(guān)java RSA信息加解密內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
基于spring-boot-maven-plugin插件打包lib文件外置的方法(layout模式為ZIP模式)
Maven是一個(gè)插件執(zhí)行框架,所有工作都由插件完成,同時(shí)?Maven?基于構(gòu)建生命周期的核心概念,明確定義了構(gòu)建和分發(fā)特定工件(項(xiàng)目)的過(guò)程,接下來(lái)通過(guò)本文給大家介紹下基于spring-boot-maven-plugin插件打包lib文件外置(layout模式為ZIP模式),需要的朋友可以參考下2022-09-09spring?boot學(xué)習(xí)筆記之操作ActiveMQ指南
ActiveMQ是一種開(kāi)源的基于JMS規(guī)范的一種消息中間件的實(shí)現(xiàn),ActiveMQ的設(shè)計(jì)目標(biāo)是提供標(biāo)準(zhǔn)的,面向消息的,能夠跨越多語(yǔ)言和多系統(tǒng)的應(yīng)用集成消息通信中間件,這篇文章主要給大家介紹了關(guān)于spring?boot學(xué)習(xí)筆記之操作ActiveMQ指南的相關(guān)資料,需要的朋友可以參考下2021-11-11Maven實(shí)戰(zhàn)之搭建Maven私服和鏡像的方法(圖文)
本篇文章主要介紹了搭建Maven私服和鏡像的方法(圖文),小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-12-12Java實(shí)現(xiàn)短信驗(yàn)證碼詳細(xì)過(guò)程
這篇文章主要給大家介紹了關(guān)于Java實(shí)現(xiàn)短信驗(yàn)證碼的相關(guān)資料, 在業(yè)務(wù)需求中我們經(jīng)常會(huì)用到短信驗(yàn)證碼,比如手機(jī)號(hào)登錄、綁定手機(jī)號(hào)、忘記密碼、敏感操作等,需要的朋友可以參考下2023-09-09springboot簡(jiǎn)單接入websocket的操作方法
這篇文章主要介紹了springboot簡(jiǎn)單接入websocket的方法,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-05-05