当前位置:   article > 正文

用户登录对密码进行加密_setpublickey

setpublickey

离开上家公司后才发现上家公司的网站 用户登录 对密码是没有任何加密的形式,自己整理了一下对密码加密的方法 如下:

不加密:

我们使用网络探测器能轻易的获取用户的用户名密码,如果这些信息被不发份子获得,后果可想而知;


这里我们可以看到 密码是以明文的方式 被网络探测器 嗅到

现对密码进行加密,如果对用户也不希望明文显示也可以进行加密:代码如下:

java 后台代码:

生成公钥方法:

  1. /**
  2. * 生成公钥
  3. * @return
  4. * @throws Exception
  5. */
  6. @ClearInterceptor(ClearLayer.ALL)
  7. public void Rdspwd() throws Exception{
  8. HttpServletResponse response = getResponse();;
  9. PrintWriter writer = response.getWriter();
  10. String publicKey = RSAUtils.generateBase64PublicKey();
  11. writer.write(publicKey);
  12. renderNull();
  13. }
页面代码:我们在页面一加载的时候就Ajax请求生成公钥的方法

需要先在Javascript 中声明 公钥变量

  1. //获取public key
  2. var publicKey = null;

  1. <button type="button" id="login-btn" class="width-35 btn btn-sm btn-primary" οnclick="doLogin()">
  2. <i class="ace-icon fa fa-key"></i>
  3. <span class="bigger-110" >登录</span>
  4. </button>

  1. function getPublicKey(dologin){
  2. $.ajax({
  3. url: "/login/Rdspwd",
  4. type: "post",
  5. dataType: "text",
  6. success: function(data) {
  7. if(data) publicKey = data;
  8. if(dologin==1){
  9. if(publicKey==null){
  10. $("#msg").html("获取publicKey失败,请联系管理员!");
  11. $("#login-btn").removeAttr("disabled");
  12. }else{
  13. doLogin(1);
  14. }
  15. }
  16. }
  17. });
  18. }

获取用户名和密码:

  1. var ustring = $.trim($("#ustring").val());
  2. var pstring = $.trim($("#pstring").val());

然后根据公钥进行私钥加密:

  1. //进行加密
  2. var encrypt = new JSEncrypt();
  3. if(publicKey != null){
  4. encrypt.setPublicKey(publicKey);
  5. var password = encrypt.encrypt(pstring);
  6. var username = encrypt.encrypt(ustring);
  7. //提交之前,检查是否已经加密。假设用户的密码不超过20位,加密后的密码不小于20位
  8. if(password.length < 20) {
  9. //加密失败提示
  10. alert("登录失败,请稍后重试...");
  11. }else{
  12. $.ajax({
  13. url: "${contextPath}/dologin",
  14. type: "post",
  15. data: {"usname": username,"pwd": password,"vcstring": vcstring},
  16. dataType: "json",
  17. }
  18. }

后台获取到加密后的用户名和密码后进行解密:

  1. //解密帐号
  2. username = RSAUtils.decryptBase64(username);
  3. //解密密码
  4. password = RSAUtils.decryptBase64(password);
RSAUtils 类:

  1. public class RSAUtils {
  2. //KeyPair is a simple holder for a key pair.
  3. private static final KeyPair keyPair = initKey();
  4. /**
  5. * 初始化方法,产生key pair,提供provider和random
  6. * @return KeyPair instance
  7. */
  8. private static KeyPair initKey() {
  9. try {
  10. //添加provider
  11. Provider provider = new org.bouncycastle.jce.provider.BouncyCastleProvider();
  12. Security.addProvider(provider);
  13. //产生用于安全加密的随机数
  14. SecureRandom random = new SecureRandom();
  15. KeyPairGenerator generator = KeyPairGenerator.getInstance("RSA", provider);
  16. generator.initialize(1024, random);
  17. return generator.generateKeyPair();
  18. } catch(Exception e) {
  19. throw new RuntimeException(e);
  20. }
  21. }
  22. /**
  23. * 产生public key
  24. * @return public key字符串
  25. */
  26. public static String generateBase64PublicKey() {
  27. PublicKey publicKey = (RSAPublicKey)keyPair.getPublic();
  28. //encodeBase64(): Encodes binary data using the base64
  29. //algorithm but does not chunk the output.
  30. //getEncoded():返回key的原始编码形式
  31. return new String(Base64.encodeBase64(publicKey.getEncoded()));
  32. }
  33. /**
  34. * 解密数据
  35. * @param string 需要解密的字符串
  36. * @return 破解之后的字符串
  37. */
  38. public static String decryptBase64(String string) {
  39. //decodeBase64():将Base64数据解码为"八位字节”数据
  40. return new String(decrypt(Base64.decodeBase64(string.getBytes())));
  41. }
  42. private static byte[] decrypt(byte[] byteArray) {
  43. try {
  44. Provider provider = new org.bouncycastle.jce.provider.BouncyCastleProvider();
  45. Security.addProvider(provider);
  46. //Cipher: 提供加密和解密功能的实例
  47. //transformation: "algorithm/mode/padding"
  48. Cipher cipher = Cipher.getInstance("RSA/ECB/PKCS1Padding", provider);
  49. PrivateKey privateKey = keyPair.getPrivate();
  50. //初始化
  51. cipher.init(Cipher.DECRYPT_MODE, privateKey);
  52. //doFinal(): 加密或者解密数据
  53. byte[] plainText = cipher.doFinal(byteArray);
  54. return plainText;
  55. } catch(Exception e) {
  56. throw new RuntimeException(e);
  57. }
  58. }
  59. }


 好了。 这样用户登录的 加密  和解密就完成了。 

前台页面加载时调用生成公钥方法 生成公钥, 然用户点击登录时获取用户输入的用户名、密码,根据之前生成的公钥 进行私钥加密,传入后台的数据是已经加过密的数据, 然后我们在后台在进行解密 ,得到用户输入的原始密码, 后面的就可以根据自己的业务需求进行处理。



声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/繁依Fanyi0/article/detail/730935
推荐阅读
相关标签
  

闽ICP备14008679号