Java Web 登录采用非对称加密(RSA算法)

登录时采用md5或者base64神马的加密都是不可靠的,被抓包了还是可以模拟登录的,基本没啥用,只能说好过没有...

接下来跟大家介绍下如何采用非对称加密,非对称加密的过程其实就是和https加密原理一样,我的处理过程是这样:

a. 在登录页面生成公钥和私钥,将私钥存在sesion中

b.公钥用于前端页面对数据进行加密

c.将数据传输给后台,后台从session中拿到私钥,然后对数据进行解密,把session中的私钥删除

下面简单记录下实现过程,具体的工具类RSAUtils.java的实现不在这里细说。

 

1、在JSP中生成公钥和私钥,将公钥放在session中:

 

   HashMap<String, Object> map = RSAUtils.getKeys();
   //生成公钥和私钥  
   RSAPublicKey publicKey = (RSAPublicKey) map.get("public");
   RSAPrivateKey privateKey = (RSAPrivateKey) map.get("private");
 //私钥保存在session中,用于解密
   session.setAttribute("privateKey", privateKey);
   //公钥信息保存在页面,用于加密
   String publicKeyExponent = publicKey.getPublicExponent().toString(16);
   String publicKeyModulus = publicKey.getModulus().toString(16);
   request.setAttribute("publicKeyExponent", publicKeyExponent);
   request.setAttribute("publicKeyModulus", publicKeyModulus);

 

 

 

2、对数据进行加密,用到的前端js工具封装在RSA.js中,需引入到页面。

 

   RSAUtils.setMaxDigits(200);
   var key = new RSAUtils.getKeyPair("${publicKeyExponent}", "", "${publicKeyModulus}");
   var encrypedPwd = RSAUtils.encryptedString(key,orgPwd.split("").reverse().join(""));

其中,orgPwd为原始数据,这里是我的密码。

 

 

3、后台对接收到的数据进行解密。

 

	String password=request.getParameter("password");
<span style="white-space:pre">	</span>RSAPrivateKey privateKey = (RSAPrivateKey)request.getSession().getAttribute("privateKey");
			if(privateKey!=null){
				long time1 = System.currentTimeMillis();
				password = RSAUtils.decryptByPrivateKey(password, privateKey);
				log.info("decrypt cost time:"+(Double)((System.currentTimeMillis()-time1)/1000d) +"s");
				request.getSession().removeAttribute("privateKey");
			}

 

 

特别注意事项:RSAUtils.java中用到org.bouncycastle.jce.provider.BouncyCastleProvider,部署在服务器上时需做如下两项配置:

a. 修改jdk目录下的 /jre/lib/security/java.security,增加如下配置:

b. 放bcprov-jdk16-146.jar 到 jdk目录下的/jre/lib/ext 下。

在eclipse中开发调试倒是不需要,但部署上服务器时需要,切记!

 

附件:

1. RSA.js

2.RSAUtils.java

3.bcprov-jdk16-146.jar 

链接: https://pan.baidu.com/s/1w6nb1aegUKtw_x8QdQAf8g 提取码: rwzb

 

 

  • 3
    点赞
  • 26
    收藏
    觉得还不错? 一键收藏
  • 11
    评论
对于实习非对称加密算法,你可以使用Java的加密库来实现。Java提供了一些常见的非对称加密算法,如RSA和DSA。下面是一个使用RSA算法进行加密和解密的示例代码: ``` import java.security.KeyPair; import java.security.KeyPairGenerator; import java.security.PrivateKey; import java.security.PublicKey; import javax.crypto.Cipher; public class AsymmetricEncryptionExample { public static void main(String[] args) throws Exception { // 生成RSA密钥对 KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance("RSA"); keyPairGenerator.initialize(2048); KeyPair keyPair = keyPairGenerator.generateKeyPair(); // 获取公钥和私钥 PublicKey publicKey = keyPair.getPublic(); PrivateKey privateKey = keyPair.getPrivate(); // 要加密的数据 String plainText = "Hello, World!"; // 使用公钥进行加密 Cipher cipher = Cipher.getInstance("RSA"); cipher.init(Cipher.ENCRYPT_MODE, publicKey); byte[] encryptedBytes = cipher.doFinal(plainText.getBytes()); // 使用私钥进行解密 cipher.init(Cipher.DECRYPT_MODE, privateKey); byte[] decryptedBytes = cipher.doFinal(encryptedBytes); String decryptedText = new String(decryptedBytes); System.out.println("原始数据: " + plainText); System.out.println("加密后数据: " + new String(encryptedBytes)); System.out.println("解密后数据: " + decryptedText); } } ``` 这段代码使用RSA算法生成了一个2048位的密钥对,然后使用公钥对数据进行加密,再使用私钥对加密后的数据进行解密。输出结果将显示原始数据、加密后的数据和解密后的数据。 请注意,加密和解密过程中使用的密钥必须匹配。同时,为了保证安全性,你还需要合理地保管私钥并避免泄露给他人。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 11
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值