在用户登录页面,用户输入密码后,在传送到服务器端时,为防止在密码传送过程中,被如360这种东东给拦截到,

需要在传送前对密码进行加密,然后再传送!

利用RSA加密,在客户端使用公钥对密码进行加密,在服务器端接收时使用私钥进行解密

JS端加密主要用到https://github.com/jasondavies/jsbn所述的功能来实现

1、使用linux服务器生成pem文件

$ openssl genrsa -out key.pem

2、提取modulus用作js加密的密钥(红字部份)

$ openssl rsa -in key.pem -noout -modulus

Modulus=C4E4B9345F5BA44DDE717385660EBF6A217CF2D59145A3DA3B57D460A285242E04D7CF57969E77749BC6D325B1381E29BD827F90F13909A97256B4B6B217141F
3、参照https://github.com/jasondavies/jsbn自己引用jsbn.js、prng4.js、rng.js、rsa.js、base64.js

4、JS脚本加密

var RSAPublicKey="C4E4B9345F5BA44DDE717385660EBF6A217CF2D59145A3DA3B57D460A285242E04D7CF57969E77749BC6D325B1381E29BD827F90F13909A97256B4B6B217141F";
var strPassword="123456";
var RSA = new RSAKey();
RSA.setPublic(RSAPublicKey, "10001");
strPassword = RSA.encrypt(strPassword);

在服务器端,使用Rsa来解密

package com.zufangbao.core.security;

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileReader;
import java.io.IOException;
import java.security.KeyFactory;
import java.security.PrivateKey;
import java.security.PublicKey;
import java.security.Security;
import java.security.spec.PKCS8EncodedKeySpec;
import java.security.spec.X509EncodedKeySpec;

import javax.crypto.Cipher;

import org.apache.commons.codec.binary.Base64;
import org.apache.commons.codec.binary.Hex;
import org.apache.log4j.Logger;
import org.bouncycastle.jce.provider.BouncyCastleProvider;
import org.bouncycastle.openssl.PEMKeyPair;
import org.bouncycastle.openssl.PEMParser;
import org.springframework.stereotype.Component;

/**
 * RSA加解密处理
 * @author Administrator
 *
 */
@Component
public class RsaUtil {
	private static final Logger log = Logger.getLogger(RsaUtil.class);

	public RsaUtil(){
	}
	/**
	 * 使用Pom初始化 @see setPomFile
	 * @param pomFile
	 */
	public RsaUtil(String pemFile){
		this.setPemFile(pemFile);
	}
	/**
	 * 使用公钥私钥初始化 @see setKey
	 * @param publicKey
	 * @param privateKey
	 */
	public RsaUtil(PublicKey publicKey, PrivateKey privateKey){
		this.setKey(publicKey, privateKey);
	}
	/**
	 * 使用公钥私钥文件初始化 @see setKey
	 * @param publicKeyFile
	 * @param privateKeyFile
	 */
	public RsaUtil(String publicKeyFile, String privateKeyFile){
		this.setKey(publicKeyFile, privateKeyFile);
	}
	private PublicKey publicKey;
	private PrivateKey privateKey;
	/**
	 * 获取公钥
	 * @return
	 */
	public PublicKey getPublicKey() {
		return publicKey;
	}
	/**
	 * 设置公钥
	 * @param publicKey
	 */
	public void setPublicKey(PublicKey publicKey) {
		this.publicKey = publicKey;
	}
	/**
	 * 获取私钥
	 * @return
	 */
	public PrivateKey getPrivateKey() {
		return privateKey;
	}
	/**
	 * 设置私钥
	 * @param privateKey
	 */
	public void setPrivateKey(PrivateKey privateKey) {
		this.privateKey = privateKey;
	}
	/**
	 * 设置POM文件方式指定公钥私钥
	 * @param pomFile
	 */
	public void setPemFile(String pemFile){
		BufferedReader br = null;
		try{
			br = new BufferedReader(new FileReader(pemFile));
			Security.addProvider(new BouncyCastleProvider());

			PEMKeyPair kp = (PEMKeyPair) new PEMParser(br).readObject();
			byte[] encodedPublicKey = kp.getPublicKeyInfo().getEncoded();
			byte[] encodedPrivateKey = kp.getPrivateKeyInfo().getEncoded();

			// Now convert to Java objects
			KeyFactory keyFactory = KeyFactory.getInstance( "RSA");
			X509EncodedKeySpec publicKeySpec = new X509EncodedKeySpec(encodedPublicKey);
			PublicKey publicKey = keyFactory.generatePublic(publicKeySpec);

			PKCS8EncodedKeySpec privateKeySpec = new PKCS8EncodedKeySpec(encodedPrivateKey);
			PrivateKey privateKey = keyFactory.generatePrivate(privateKeySpec);
			this.setKey(publicKey, privateKey);
		}catch(Exception e){
			log.fatal("setPomFile failed", e);
		}finally{
			if (br != null){
				try {
					br.close();
				} catch (IOException e) {
					log.fatal("setPomFile failed", e);
				}
			}
		}
	}
	/**
	 * 设置公钥私钥
	 * @param publicKey
	 * @param privateKey
	 */
	public void setKey(PublicKey publicKey, PrivateKey privateKey){
		this.publicKey = publicKey;
		this.privateKey = privateKey;
	}
	/**
	 * 设置公钥私钥
	 * @param publicKeyFile
	 * @param privateKeyFile
	 */
	public void setKey(String publicKeyFile, String privateKeyFile){
		PublicKey publicKey = this.readPublicKey(publicKeyFile);
		PrivateKey privateKey = this.readPrivateKey(privateKeyFile);
		this.setKey(publicKey, privateKey);
	}
	/**
	 * 读取公钥
	 * @param keyFile
	 * @return
	 */
	private PublicKey readPublicKey(String keyFile){
		FileInputStream fis = null;
		try{
			fis = new FileInputStream(keyFile);
			byte[] bytes = new byte[fis.available()];
			fis.read(bytes);
			PKCS8EncodedKeySpec keySpec = new PKCS8EncodedKeySpec(bytes);
			KeyFactory factory = KeyFactory.getInstance("RSA");
			PublicKey key = factory.generatePublic(keySpec);
			return key;
		}catch(Exception e){
			log.fatal("readPublicKey failed", e);
			return null;
		}finally{
			if (fis != null){
				try {
					fis.close();
				} catch (IOException e) {
					log.fatal("readPublicKey failed", e);
				}
			}
		}
	}
	/**
	 * 读取私钥
	 * @param keyFile
	 * @return
	 */
	private PrivateKey readPrivateKey(String keyFile){
		FileInputStream fis = null;
		try{
			fis = new FileInputStream(keyFile);
			byte[] bytes = new byte[fis.available()];
			fis.read(bytes);
			PKCS8EncodedKeySpec keySpec = new PKCS8EncodedKeySpec(bytes);
			KeyFactory factory = KeyFactory.getInstance("RSA");
			PrivateKey key = factory.generatePrivate(keySpec);
			return key;
		}catch(Exception e){
			log.fatal("readPublicKey failed", e);
			return null;
		}finally{
			if (fis != null){
				try {
					fis.close();
				} catch (IOException e) {
					log.fatal("readPublicKey failed", e);
				}
			}
		}
	}
	/**
	 * 加密字符串
	 * @param value 普通字符串 长度不能超过53个字节
	 * @return 返回Base64位编码的字符串
	 */
	public String encryptString2b64(String value){
		if (this.getPublicKey() == null){
			log.fatal("RSA公钥为空,加密失败");
			return null;
		}
		try{
			Cipher cipher = Cipher.getInstance("RSA");
	        cipher.init(Cipher.ENCRYPT_MODE, this.getPublicKey());
	        byte[] bytes = cipher.doFinal(value.getBytes());
	        return Base64.encodeBase64String(bytes);
		}catch(Exception e){
			log.fatal("encryptString failed", e);
			return null;
		}
	}
	/**
	 * 使用私钥解码Base64位字串
	 * @param value Base64位字串
	 * @return 返回原始字符串
	 */
	public String decryptString4b64(String value){
		if (this.getPrivateKey() == null){
			log.fatal("RSA私钥为空,加密失败");
			return null;
		}
		if (value == null){
			log.fatal("待RSA解密字串不能为空");
			return null;
		}
		try{
			Cipher cipher = Cipher.getInstance("RSA");
	        cipher.init(Cipher.DECRYPT_MODE, this.getPrivateKey());
	        byte[] bytes = Base64.decodeBase64(value);
	        bytes = cipher.doFinal(bytes);
	        return new String(bytes);
		}catch(Exception e){
			log.fatal("decryptString failed", e);
			return null;
		}
	}
	/**
	 * 加密字符串
	 * @param value 普通字符串 长度不能超过53个字节
	 * @return 返回hex编码的字符串
	 */
	public String encryptString2hex(String value){
		if (this.getPublicKey() == null){
			log.fatal("RSA公钥为空,加密失败");
			return null;
		}
		try{
			Cipher cipher = Cipher.getInstance("RSA");
	        cipher.init(Cipher.ENCRYPT_MODE, this.getPublicKey());
	        byte[] bytes = cipher.doFinal(value.getBytes());
	        return new String(Hex.encodeHex(bytes));
		}catch(Exception e){
			log.fatal("encryptString failed", e);
			return null;
		}
	}
	/**
	 * 使用私钥解码hex字串
	 * @param value hex字串
	 * @return 返回原始字符串
	 */
	public String decryptString4hex(String value){
		if (this.getPrivateKey() == null){
			log.fatal("RSA私钥为空,加密失败");
			return null;
		}
		if (value == null){
			log.fatal("待RSA解密字串不能为空");
			return null;
		}
		try{
			Cipher cipher = Cipher.getInstance("RSA");
	        cipher.init(Cipher.DECRYPT_MODE, this.getPrivateKey());
	        byte[] bytes = Hex.decodeHex(value.toCharArray());
	        bytes = cipher.doFinal(bytes);
	        return new String(bytes);
		}catch(Exception e){
			log.fatal("decryptString failed", e);
			return null;
		}
	}
}

  pom.xml主要依赖有

<dependency>
    <groupId>commons-codec</groupId>
    <artifactId>commons-codec</artifactId>
    <version>1.8</version>
</dependency>
<dependency>
    <groupId>org.bouncycastle</groupId>
    <artifactId>bcpkix-jdk15on</artifactId>
    <version>1.49</version>
</dependency>

 在项目中通过spring注入pem文件,实现解密

用户登录密码RSA加密后传输的实现,非明文密码传输的更多相关文章

  1. 电信级的RSA加密后的密码的破解方法

    一直以来,电信通过HTTP劫持推送广告的方式已经存在了很多年了,这种手段至今并未停止.这种手段月光博客曾经有多次曝光,见<电信级的网络弹出广告>.<获取了电信恶意弹出广告的罪证> ...

  2. Centos 用户登录失败N次后锁定用户禁止登陆

    针对linux上的用户,如果用户连续3次登录失败,就锁定该用户,几分钟后该用户再自动解锁 Linux有一个pam_tally2.so的PAM模块,来限定用户的登录失败次数,如果次数达到设置的阈值,则锁 ...

  3. Centos7下用户登录失败N次后锁定用户禁止登陆的方法

    前言 针对linux上的用户,如果用户连续3次登录失败,就锁定该用户,几分钟后该用户再自动解锁.Linux有一个pam_tally2.so的PAM模块,来限定用户的登录失败次数,如果次数达到设置的阈值 ...

  4. Centos7 用户登录失败N次后锁定用户禁止登陆

    参考网站:https://blog.csdn.net/qq_33285112/article/details/78813369  未试 思路是查找/var/log/secure中验证失败且出现的次数较 ...

  5. GS环境里面 9999 常用密码的加密后的值

    1. Test6530 APTZ5s6vrw1dglqO/63osA== 2. aaaaaa zgnewZXGGoqcPGtNpXTSXQ== 3. cwpass 3Me34S0+zY4xEGUFtz ...

  6. 系统开发中使用拦截器校验是否登录并使用MD5对用户登录密码进行加密

    项目名称:客户管理系统 项目描述: 项目基于javaEE平台,B/S模式开发.使用Struts2.Hibernate/Spring进行项目框架搭建.使用Struts中的Action 控制器进行用户访问 ...

  7. 兼容javascript和C#的RSA加密解密算法,对web提交的数据进行加密传输

    Web应用中往往涉及到敏感的数据,由于HTTP协议以明文的形式与服务器进行交互,因此可以通过截获请求的数据包进行分析来盗取有用的信息.虽然https可以对传输的数据进行加密,但是必须要申请证书(一般都 ...

  8. 如何正确对用户密码进行加密?转自https://blog.csdn.net/zhouyan8603/article/details/80473083

    本文介绍了对密码哈希加密的基础知识,以及什么是正确的加密方式.还介绍了常见的密码破解方法,给出了如何避免密码被破解的思路.相信读者阅读本文后,就会对密码的加密有一个正确的认识,并对密码正确进行加密措施 ...

  9. iOS动态部署之RSA加密传输Patch补丁

    概要:这一篇博客主要说明下iOS客户端动态部署方案中,patch(补丁)是如何比较安全的加载到客户端中. 在整个过程中,需要使用RSA来加密(你可以选择其它的非对称加密算法),MD5来做校验(同样,你 ...

随机推荐

  1. 【阿里云产品公测】云引擎ACE新手实战基于Wordpress

    [阿里云产品公测]云引擎ACE新手实战基于Wordpress 作者:阿里云用户imnpc ACE(Aliyun Cloud Engine) 是一款弹性.分布式的应用托管环境,支持Java.php多种语 ...

  2. 【MYSQL】数据类型

    转载 https://www.baidu.com/s?ie=UTF-8&wd=cnblog 原文 泪云山海的博客 mysql 数据类型 1.整型 MySQL数据类型 含义(有符号) tinyi ...

  3. cat命令的作用

    1.显示文件内容,如more的功能. 使用方法: cat  filename. 注:cat,无论文件多长,一次性全部显示:more,一次只显示一个屏幕高度的内容. 2.创建文件,如touch功能. 使 ...

  4. unity中UI界面显示FPS

    直接上代码     using UnityEngine; using System.Collections;   public class HUDFPS : MonoBehaviour {       ...

  5. 《Cortex-M0权威指南》之Cortex-M0技术综述

    转载请注明来源:cuixiaolei的技术博客 Cortex-M0 处理器简介 1. Cortex-M0 处理器基于冯诺依曼架构(单总线接口),使用32位精简指令集(RISC),该指令集被称为Thum ...

  6. ios 代码截屏模糊问题解决办法

    我们常用的截图方法如下所示: //尺寸是按照 UIGraphicsBeginImageContext(CGSizeMake(, )); //currentView 当前的view 创建一个基于位图的图 ...

  7. Java Script基础(十一) 表单验证

    一.表单验证的必要性: 表单验证在客户端和服务器端,客户端验证实际是直接调用JavaScript脚本对用户输入的数据进行验证:而服务器也同样可以实现数据验证,但是当服务器压力过大时,资源损耗会比较严重 ...

  8. C#中XML与对象之间的序列化、反序列化

    直接上代码: using System; using System.IO; using System.Text; using System.Xml; using System.Xml.Serializ ...

  9. TortoiseSVN 更新时忽略指定文件夹

    命令行可以这么来svn update –set-depth=exclude 文件夹 那么TortoiseSVN客户端呢?在文件夹右键中的”更新至版本(U)”更新深度选”排除”,确定,搞定下次更新就不会 ...

  10. Javascript中关于数组的认识

    昨天在练习js中cookie的时候,知道js中的cookie是一个字符串,这与php中的操作cookie还是有很大的差别的,起初我是以php的思维来学习怎么样使用使用js中的cookie. js中的c ...