DBMNG数据库管理与应用

才能是来自独创性。独创性是思维、观察、理解和判断的一种独特的方式。
当前位置:首页 > 经验分享 > Javascript

用户登录密码RSA加密后传输的实现,非明文密码传输

在用户登录页面,用户输入密码后,在传送到服务器端时,为防止在密码传送过程中,被如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文件,实现解密

本站文章内容,部分来自于互联网,若侵犯了您的权益,请致邮件chuanghui423#sohu.com(请将#换为@)联系,我们会尽快核实后删除。
Copyright © 2006-2023 DBMNG.COM All Rights Reserved. Powered by DEVSOARTECH            豫ICP备11002312号-2

豫公网安备 41010502002439号