前端使用jsencrypt加密后端使用java RSA解密操作步骤

  • 前端界面需要引入jquery.min.js和jsencrypt.min.js


  • 通过RSAUtils类生成公钥和私钥,核心代码如下:
            KeyPair kp = getRsaKP();
			PrivateKey priKey = kp.getPrivate();
			PublicKey pubKey = kp.getPublic();
			
			System.out.println("PublicKey:");
			System.out.println(base64Encode(pubKey.getEncoded()));
			System.out.println("PrivateKey:");
			System.out.println(base64Encode(priKey.getEncoded()));
  • 前端通过JSEncrypt实现对明文内容进行加密,代码如下:
 var encrypt = new JSEncrypt();
//设置公钥,公钥在第二步已生成
encrypt.setPublicKey("MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAlQdbImgpEA3EAJprxmiC3kV50D+udS9p9Ni4UHBc/88888888+IdX30osDhoq8ESZJh58ebj8Ai1T5dDUsw2UhOBCo2pjUIjbrT4Lt6m/Tz9ebc2aeEecQrIwbR+472HBdEwXQgqJaOvEUYztnWa9999999+lNf8Pi1zVqMunRUAaOHjWl+3vlt22WGqeCiFh5kO4A1JWsPXyLJkTM2Qb7fCJu+xiQAkkrF9ZAzIzZz/BI5rcqoBzP9evwCqY5EOPhe1y+ar3MwYiGlSxRiYUmB+NTDlwFhbAz/Du913ohTVeqKFNba5K01v0aO7aGaxUCcvFAknrJLtsTtNgEyWqwIDAQAB");
var encrypted = encrypt.encrypt('需加密的明文内容');
console.log(encrypted);
  • 后端通过RSAUtils类实现解密,核心代码如下:
jiami="Wqgt/Odz3I+06+NDIlu1oX4bRImevT8OYUG1ZArqRne790fIYnAOOy/KeuslRhgqRvxR2gccp5WAa7DSiFG+9+Wdd7mYGSfW92ksjHaTDmOMg18i4RRHpM91qsE51ezuUAkeP/oEwUc6JercsWucxL+7RCTPgIUEo9rzHLendN6Hie4aHpCwEGajgW1FjmoprtYwFZ6EsMRRKFoaZgRVYsJHh/TXot33y8GmmOInePnirmutoUGMBLe6X0LJEIP1Nya/mfMllCZcQYImWIzAeNKYtCyiDmACY3aTdCCrPmB7yn/1yNL9bXCaHuqHpD7vZLDBmxVeERGAd+1tBE/dWA==";
			System.out.println("JSEncrypt前端界面生成的加密内容:"+jiami);
			//java后端进行解密(公钥和私钥本案例中有改动,项目中按照实际公钥和私钥进行替换):
			//设置私钥
			PrivateKey prikey = RSAUtils.restorePrivateKey(RSAUtils.base64Decode("MIIEvAIBADANBgkqhkiG9w0BAQEFAASCBKYwggSiAgEAAoIBAQCVB1siaCkQDcQAmmvGaILeRXnQ"+
"P651L2n02LhQcFz8rWYncU7L4h1ffSiwOGirwRJkmHnx5uPwCLVPl0NSzDZSE4EKjamNQiNutPgu"+
"3qb9PP15tzZp4R5xCsjBtH7jvYcF0TBdCColo68RRjO2dZoGCChzc76U1/w+LXNWoy6dFQBo4eNa"+
"X7e+W3bZYap4KIWHmQ7gDUlaw9fIsmRMzZBvt8Im77GJACSSsX1kDMjNnP8EjmtyqgHM/16/AKpj"+
"kQ4+F7XL5qvczBiIaVLFGJhSYH41MOXAWFsDP8O73XeiFNV6ooU1trkrTW/Ro7toZrFQJy888888"+
"ku2xO02ATJarAgMBAAECggEABBRZeE2lohzeWq3feWTvACqitQKpQef0XJF6KimJQhl1DGtbntUn"+
"vK9uZ7SDmQAGAt7xp5+MacuzOuSHIUMZ0ZFbHPDXzVgcJDGsAYxywcfjoGtVf/x7mtiSukq4ln9f"+
"rNcXHdTVYoZiinSDgeEiQDwE73QhqW1oKEOSPrLk2cPojwtQSSeRpzuHbrvcB9XE2RpyV/777777"+
"V74hVYgANK+Ccvq710L4ieOXL8iSO2unqYBcCGdC1GXncopUEzSD8MUMSk7js4P7qVWj+2lo3QdX"+
"McJKekruW0e1hwl1mdkR7UNzdLBTo8EZURKXVDLeJOS1hsUZ/ssKJM4lpi85gQKBgQDn2qW9VMJQ"+
"s8GoEfLNvwCBwQeeN0BV4EqzKlJ9/+Dezk52uRL3ctAXUckVd/M+hpYkm1BxqCpqnhjbosw/brEd"+
"k97V5fDlTzPOguM9LWGf7guciGGaecQVamdEMwt+sba0KyfNhtkU6OkeIGntY6qkNa09qPb++/CO"+
"epKI7wYi4QKBgQCkjIpxTLL6MPXlSayvGoE/A3VDygVa5pmTK5P5L1ac++whHuyXjfAm0QOmg2Th"+
"5SbyykYIhdGbzFWNmzzkUPktRVYefehcU/1vyovjyJqtRTG+/DkkYOuCpECNaZI89kPn5fjCghb1"+
"lehN9SnD6rxJpDNj77C60+ArSyqO3aL3CwKBgFrf2SxvR0ZIXnQYrNk1ruYsmWbK8x11rvGweerG"+
"2wWZSSnBeoygZK1vMeiHPkDYkYrBkXMq3Y0ycD3N/k4yfZtBNQsTeCr4sy6TIKv+2I/NxqFQ1yir"+
"504ZiWYyt6Ot1hWiAJTO8NEARTa4r6wK73wzSbasPjtM1kmSA1NGlFohAoGAROiKDXp2NSoNv7CD"+
"vWyXFYFoOTQ/29pYE/aa6C530hP5xcECiVEqccb0BWZe9+6TyfcFLwqDRgBjD4MkE+UJBHlV4D0P"+
"8qJFruweewyGIeupMnbDo0VnOvV4BwBf+mGS/GklLKd33+6gMw1pKR+DLD+0ObSlyQdziA3ZS6zr"+
"UxMCgYB4THE+QAECQMH1PVFoE5bUUwSIKoWSoH4zdCZjhMstxMR/GklYbCvC7RS0I4H8W+1M8V8v"+
"e3xE7XHBSLAav/MoEloMKY19h7PC0jF2xiceJ9b9O2KmrFhkIBfiXozxElXJBOtwY7spxTd2oCHj"+
"+OVK/3uMgHMLLCihwQj999999=="));
			
			String jiemi=rsaDecrypt(prikey,RSAUtils.base64Decode(jiami));
			System.out.println("解密后:"+jiemi);

如果了解更详细的程序包功能,请通过以下资源连接进行下载:

https://download.csdn.net/download/u012510188/85981358

你可能感兴趣的:(java,jquery,前端,java,javascript,jquery)