RSA实现前端密码密文传输

本文介绍了如何在前端使用jQuery和Thymeleaf配合JSEncrypt进行公钥加密,以及在后端Java中,通过SpringBoot和ApacheCommons库实现私钥解密,展示了RSA算法在实际项目中的应用过程。
摘要由CSDN通过智能技术生成

RSA实现密码密文传输,前端jquery,后端java

模块技术栈

前端thymeleaf,jquery,css,h5,后端springboot

前端部分

公钥加密

var publicKeyString ="......";
var encrypt = new JSEncrypt();
encrypt.setPublicKey(publicKeyString);
var rsa_pwd = encrypt.encrypt(password);
//todo ajax请求

后端部分

私钥解密

import org.apache.commons.codec.binary.Base64;
import org.apache.commons.io.IOUtils;

import javax.crypto.Cipher;
import java.io.ByteArrayOutputStream;
import java.security.*;
import java.security.interfaces.RSAPrivateKey;
import java.security.interfaces.RSAPublicKey;
import java.security.spec.InvalidKeySpecException;
import java.security.spec.PKCS8EncodedKeySpec;
import java.security.spec.X509EncodedKeySpec;

public class RsaUtil {
    public static RSAPrivateKey getPrivateKey(String privateKey) throws NoSuchAlgorithmException, InvalidKeySpecException {
        KeyFactory keyFactory = KeyFactory.getInstance(RSA_ALGORITHM);
        PKCS8EncodedKeySpec pkcs8KeySpec = new PKCS8EncodedKeySpec(Base64.decodeBase64(privateKey));
        RSAPrivateKey key = (RSAPrivateKey) keyFactory.generatePrivate(pkcs8KeySpec);
        return key;
    }
}

jsencrypt.min.js 见附件

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值