【java】RSA前后端互通
后端
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 | import javax.crypto.Cipher; import java.security.KeyFactory; import java.security.PrivateKey; import java.security.PublicKey; import java.security.spec.PKCS8EncodedKeySpec; import java.security.spec.X509EncodedKeySpec; import java.util.Base64; public class RsaUtil { /** * @param plaintext 要加密的字符串 * @param publicKeyStr 传入的公钥,是一个字符串 * @return 加密后的字符串, 以Base64编码的形式返回 * @throws Exception 异常 * 这个方法接受一个要加密的字符串和一个公钥字符串,使用公钥进行加密,然后返回加密后的字符串 */ public static String encrypt(String plaintext, String publicKeyStr) throws Exception { PublicKey publicKey = getPublicKeyFromString(publicKeyStr); Cipher cipher = Cipher.getInstance( "RSA/ECB/PKCS1Padding" ); cipher.init(Cipher.ENCRYPT_MODE, publicKey); byte [] encryptedBytes = cipher.doFinal(plaintext.getBytes()); return Base64.getEncoder().encodeToString(encryptedBytes); } /** * @param encryptedText 要解密的字符串 * @param privateKeyStr 传入的私钥,是一个字符串 * @return 解密后的原始字符串 * @throws Exception 异常 * 这个方法接受一个要解密的字符串和一个私钥字符串,使用私钥进行解密,然后返回解密后的原始字符串 */ public static String decrypt(String encryptedText, String privateKeyStr) throws Exception { PrivateKey privateKey = getPrivateKeyFromString(privateKeyStr); Cipher cipher = Cipher.getInstance( "RSA/ECB/PKCS1Padding" ); cipher.init(Cipher.DECRYPT_MODE, privateKey); byte [] encryptedBytes = Base64.getDecoder().decode(encryptedText); byte [] decryptedBytes = cipher.doFinal(encryptedBytes); return new String(decryptedBytes); } /** * @param publicKeyStr * @return 公钥私钥对象 * @throws Exception 将刚拿出的Base64格式的私钥对的私钥字符串生成公钥对象 */ 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格式的私钥对的私钥字符串生成私钥对象 */ 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 { //密钥生成:生成地址:http://web.chacuo.net/netrsakeypair 密码格式:PKCS#8 String publicKeyStr = "" ; String privateKeyStr = "" ; String plaintext = "admin123455" ; String encryptedText = encrypt(plaintext, publicKeyStr); System.out.println( "加密后: " + encryptedText); System.out.println( "解密后: " + decrypt(encryptedText, privateKeyStr)); System.out.println( "公钥: " + publicKeyStr); System.out.println( "私钥: " + privateKeyStr); } } |
前端
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 单元测试从入门到精通
· 上周热点回顾(3.3-3.9)
· Vue3状态管理终极指南:Pinia保姆级教程