当前位置: 首页 > news >正文

RSA加密算法的简单案例

SA加密算法是目前最有影响力的公钥加密算法,它能够抵抗到目前为止已知的绝大多数密码攻击。

那关于RSA加密算法有哪些应用呢?以下举一个数据库身份验证的案例。

在使用数据集进行身份认证时,密码存在数据库中,认证时用户输入的密码与数据库中密码相同则认证通过,若数据库被破解了则对系统造成威胁,怎样保证系统安全呢?这里就可以应用RSA加密算法,对权限加密。

思路:

就是在url中传用户名密码时,先把用户名进行翻转,然后再进行加密,如输入的密码为12,实际后台进行加密的值为21,再与数据库进行验证,这样就可以避免数据库被破解查看到的是21的加密码,登陆系统时以21是无法登陆成功的。

以报表软件FineReport为例,这是一个能读取各类数据库的报表软件,分客户端和前端展示。

实现方案:

1、把RSA加密使用的第三方包,放到工程web-inf/lib文件夹下即可。

2、调用js文件

RSA文件夹为前端js加密时需要调用js文件,因此需要将Barrett.js、BigInt.js、RSA.js放到工程目录下如:WebReport/js,新建js文件夹放入js文件。

3、定义RSA加密类

定义RSAUtil.java类文件,先运行类中generateKeyPair()方法,会在服务器D盘中生成一个随机的RSAKey.txt文件,保存公钥和密钥,每访问一次这个方法会刷新一次txt文件。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
package  com.fr.privilege;
 
import  java.io.ByteArrayOutputStream;
import  java.io.FileInputStream;
import  java.io.FileOutputStream;
import  java.io.ObjectInputStream;
import  java.io.ObjectOutputStream;
import  java.math.BigInteger;
import  java.security.KeyFactory;
import  java.security.KeyPair;
import  java.security.KeyPairGenerator;
import  java.security.NoSuchAlgorithmException;
import  java.security.PrivateKey;
import  java.security.PublicKey;
import  java.security.SecureRandom;
import  java.security.interfaces.RSAPrivateKey;
import  java.security.interfaces.RSAPublicKey;
import  java.security.spec.InvalidKeySpecException;
import  java.security.spec.RSAPrivateKeySpec;
import  java.security.spec.RSAPublicKeySpec;
 
import  javax.crypto.Cipher;
 
/**
  * RSA 工具类。提供加密,解密,生成密钥对等方法。
  * 需要到http://www.bouncycastle.org下载bcprov-jdk14-123.jar。
 
  */
public  class  RSAUtil {
     /**
      * * 生成密钥对 *
     
      * @return KeyPair *
      * @throws EncryptException
      */
     public  static  KeyPair generateKeyPair()  throws  Exception {
         try  {
             KeyPairGenerator keyPairGen = KeyPairGenerator.getInstance( "RSA" ,
                     new  org.bouncycastle.jce.provider.BouncyCastleProvider());
             final  int  KEY_SIZE =  1024 ; // 没什么好说的了,这个值关系到块加密的大小,可以更改,但是不要太大,否则效率会低
             keyPairGen.initialize(KEY_SIZE,  new  SecureRandom());
             KeyPair keyPair = keyPairGen.generateKeyPair();
             saveKeyPair(keyPair);
             return  keyPair;
         catch  (Exception e) {
             throw  new  Exception(e.getMessage());
         }
     }
 
     public  static  KeyPair getKeyPair()  throws  Exception {
         FileInputStream fis =  new  FileInputStream( "C:/RSAKey.txt" );
         ObjectInputStream oos =  new  ObjectInputStream(fis);
         KeyPair kp = (KeyPair) oos.readObject();
         oos.close();
         fis.close();
         return  kp;
     }
 
     public  static  void  saveKeyPair(KeyPair kp)  throws  Exception {
 
         FileOutputStream fos =  new  FileOutputStream( "C:/RSAKey.txt" );
         ObjectOutputStream oos =  new  ObjectOutputStream(fos);
         // 生成密钥
         oos.writeObject(kp);
         oos.close();
         fos.close();
     }
 
     /**
      * * 生成公钥 *
     
      * @param modulus *
      * @param publicExponent *
      * @return RSAPublicKey *
      * @throws Exception
      */
     public  static  RSAPublicKey generateRSAPublicKey( byte [] modulus,
             byte [] publicExponent)  throws  Exception {
         KeyFactory keyFac =  null ;
         try  {
             keyFac = KeyFactory.getInstance( "RSA" ,
                     new  org.bouncycastle.jce.provider.BouncyCastleProvider());
         catch  (NoSuchAlgorithmException ex) {
             throw  new  Exception(ex.getMessage());
         }
 
         RSAPublicKeySpec pubKeySpec =  new  RSAPublicKeySpec( new  BigInteger(
                 modulus),  new  BigInteger(publicExponent));
         try  {
             return  (RSAPublicKey) keyFac.generatePublic(pubKeySpec);
         catch  (InvalidKeySpecException ex) {
             throw  new  Exception(ex.getMessage());
         }
     }
 
     /**
      * * 生成私钥 *
     
      * @param modulus *
      * @param privateExponent *
      * @return RSAPrivateKey *
      * @throws Exception
      */
     public  static  RSAPrivateKey generateRSAPrivateKey( byte [] modulus,
             byte [] privateExponent)  throws  Exception {
         KeyFactory keyFac =  null ;
         try  {
             keyFac = KeyFactory.getInstance( "RSA" ,
                     new  org.bouncycastle.jce.provider.BouncyCastleProvider());
         catch  (NoSuchAlgorithmException ex) {
             throw  new  Exception(ex.getMessage());
         }
 
         RSAPrivateKeySpec priKeySpec =  new  RSAPrivateKeySpec( new  BigInteger(
                 modulus),  new  BigInteger(privateExponent));
         try  {
             return  (RSAPrivateKey) keyFac.generatePrivate(priKeySpec);
         catch  (InvalidKeySpecException ex) {
             throw  new  Exception(ex.getMessage());
         }
     }
 
     /**
      * * 加密 *
     
      * @param key
      *            加密的密钥 *
      * @param data
      *            待加密的明文数据 *
      * @return 加密后的数据 *
      * @throws Exception
      */
     public  static  byte [] encrypt(PublicKey pk,  byte [] data)  throws  Exception {
         try  {
             Cipher cipher = Cipher.getInstance( "RSA" ,
                     new  org.bouncycastle.jce.provider.BouncyCastleProvider());
             cipher.init(Cipher.ENCRYPT_MODE, pk);
             int  blockSize = cipher.getBlockSize(); // 获得加密块大小,如:加密前数据为128个byte,而key_size=1024
             // 加密块大小为127
             // byte,加密后为128个byte;因此共有2个加密块,第一个127
             // byte第二个为1个byte
             int  outputSize = cipher.getOutputSize(data.length); // 获得加密块加密后块大小
             int  leavedSize = data.length % blockSize;
             int  blocksSize = leavedSize !=  0  ? data.length / blockSize +  1
                     : data.length / blockSize;
             byte [] raw =  new  byte [outputSize * blocksSize];
             int  i =  0 ;
             while  (data.length - i * blockSize >  0 ) {
                 if  (data.length - i * blockSize > blockSize)
                     cipher.doFinal(data, i * blockSize, blockSize, raw, i
                             * outputSize);
                 else
                     cipher.doFinal(data, i * blockSize, data.length - i
                             * blockSize, raw, i * outputSize);
                 // 这里面doUpdate方法不可用,查看源代码后发现每次doUpdate后并没有什么实际动作除了把byte[]放到
                 // ByteArrayOutputStream中,而最后doFinal的时候才将所有的byte[]进行加密,可是到了此时加密块大小很可能已经超出了
                 // OutputSize所以只好用dofinal方法。
 
                 i++;
             }
             return  raw;
         catch  (Exception e) {
             throw  new  Exception(e.getMessage());
         }
     }
 
     /**
      * * 解密 *
     
      * @param key
      *            解密的密钥 *
      * @param raw
      *            已经加密的数据 *
      * @return 解密后的明文 *
      * @throws Exception
      */
     public  static  byte [] decrypt(PrivateKey pk,  byte [] raw)  throws  Exception {
         try  {
             Cipher cipher = Cipher.getInstance( "RSA" ,
                     new  org.bouncycastle.jce.provider.BouncyCastleProvider());
             cipher.init(cipher.DECRYPT_MODE, pk);
             int  blockSize = cipher.getBlockSize();
             ByteArrayOutputStream bout =  new  ByteArrayOutputStream( 64 );
             int  j =  0 ;
 
             while  (raw.length - j * blockSize >  0 ) {
                 bout.write(cipher.doFinal(raw, j * blockSize, blockSize));
                 j++;
             }
             return  bout.toByteArray();
         catch  (Exception e) {
             throw  new  Exception(e.getMessage());
         }
     }
 
     /**
      * * *
     
      * @param args *
      * @throws Exception
      */
     public  static  void  main(String[] args)  throws  Exception {
         RSAPublicKey rsap = (RSAPublicKey) RSAUtil.generateKeyPair()
                 .getPublic();
         String test =  "hello world" ;
         byte [] en_test = encrypt(getKeyPair().getPublic(), test.getBytes());
         System.out.println( "123:"  new  String(en_test));
         byte [] de_test = decrypt(getKeyPair().getPrivate(), en_test);
         System.out.println( new  String(de_test));
     }
}

4、定义密码验证类

定义TestPasswordValidatorRSA.java密码验证类

定义一个类,命名为TestPasswordValidatorRSA.java,扩展于AbstractPasswordValidator,重写其中密码验证方法encodePassword,先把输入的密码进行翻转,然后再进行加密,返回密码进行验证,具体代码如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
package  com.fr.privilege;  
 
import  com.fr.privilege.providers.dao.AbstractPasswordValidator;  
public  class  TestPasswordValidatorRSA  extends  AbstractPasswordValidator{  
     //@Override
     public  String encodePassword( String clinetPassword) {
         try  {
             //对密码进行翻转如输入ab翻转后为ba
             StringBuffer sb =  new  StringBuffer();  
             sb.append( new  String(clinetPassword));
             String bb = sb.reverse().toString();
             //进行加密
             byte [] en_test = RSAUtil.encrypt(RSAUtil.getKeyPair().getPublic(),bb.getBytes());          
             //进行解密,如果数据库里面保存的是加密码,则此处不需要进行解密
             byte [] de_test = RSAUtil.decrypt(RSAUtil.getKeyPair().getPrivate(),en_test);  
             //返回加密密码
             clinetPassword= new  String(de_test);       
         catch  (Exception e) {
             // TODO Auto-generated catch block
             e.printStackTrace();
         }
         return  clinetPassword;  //即获取加密密码再与数据库密码匹配。  
     }
 
     @Override
     public  boolean  validatePassword(String arg0, String arg1) {
         // TODO Auto-generated method stub
         return  false ;
     }
 
 
}

5、编译类文件

首先编译RSAUtil.java类文件在服务器的D盘生成RSAKey.txt文件,再编译TestPasswordValidatorRSA.java类,把编译后的class文件放到项目工程web-inf/classes/com/fr/privilege文件夹中。

6、登陆Login.jsp页面设置

客户端请求到登录页面,随机生成一字符串,此随机字符串作为密钥加密密码,如下代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
<%@page contentType= "text/html"  pageEncoding= "UTF-8" %>
<%@page import= "com.fr.privilege.providers.dao.RSAUtil" %>
<%!public String Testmo() {
         String module =  "" ;
         try  {
             java.security.interfaces.RSAPublicKey rsap = (java.security.interfaces.RSAPublicKey) RSAUtil
                     .getKeyPair().getPublic();
             module = rsap.getModulus().toString(16);
         catch  (Exception e) {
             // TODO Auto-generated catch block
             e.printStackTrace();
         }
         return  module;
     }%>
<%!public String Testem() {
         String empoent =  "" ;
         try  {
             java.security.interfaces.RSAPublicKey rsap = (java.security.interfaces.RSAPublicKey) RSAUtil
                     .getKeyPair().getPublic();
             empoent = rsap.getPublicExponent().toString(16);
         catch  (Exception e) {
             // TODO Auto-generated catch block
             e.printStackTrace();
         }
         return  empoent;
     }%>
<html>
     <head>
         <script type= "text/javascript"
             src= "ReportServer?op=emb&resource=finereport.js" ></script>
         <script type= "text/javascript"  src= "js/RSA.js" ></script>
         <script type= "text/javascript"  src= "js/BigInt.js" ></script>
         <script type= "text/javascript"  src= "js/Barrett.js" ></script>
         <script type= "text/javascript" >    
     function  bodyRSA()  
     {  
     setMaxDigits(130);  
     var  a =  "<%=Testmo()%>" ;
     var  b =  "<%=Testem()%>" ;
     key =  new  RSAKeyPair(b, "" ,a);
    
function  doSubmit() {   
     bodyRSA(); 
     var  username = FR.cjkEncode(document.getElementById( "username" ).value);  //获取输入的用户名    
     var  password = FR.cjkEncode(document.getElementById( "password" ).value);   //获取输入的参数    
     $.ajax({    
         url :  "ReportServer?op=auth_login&fr_username="  + username +  "&fr_password="  + password,    //将用户名和密码发送到报表认证地址op=auth_login   
         data : {__redirect__ :  'false' },        
         complete :  function (res) {    
             var  jo = FR.jsonDecode(res.responseText);    
             if (jo.url) {    
                window.location=jo.url+  "&_="  new  Date().getTime();    //认证成功跳转页面,因为ajax不支持重定向所有需要跳转的设置  
             }    
             else {    
                alert( "用户名密码错误!" )   //认证失败    
             }    
         }    
     })    
}    
</script>
     </head>
     <body>
         <p>
             请登录
         </p>
         <form name= "login"  method= "POST" >
             <p>
                 用户名:
                 <input id= "username"  type= "text"  />
             </p>
             <p>
                 密 码:
                 <input id= "password"  type= "password"  />
             </p>
             <input type= "button"  value= "登录"  onclick= "doSubmit()"   />
         </form>
     </body>
</html>


本文转自 雄霸天下啦 51CTO博客,原文链接:http://blog.51cto.com/10549520/1839220,如需转载请自行联系原作者

相关文章:

  • 第二次作业
  • Oracle数据库入门——初级系列教程
  • CentOS 网络基础:(4)设置单网卡多IP
  • PostgreSQL 9.6 for Centos7.4 最佳实践安装
  • charles抓包工具
  • OpenSL ES
  • C#.NET 剪切板复制粘贴泛型的例子代码参考 Clipboard Copy Paste List
  • 关于《停止学习框架》 - 讨论
  • Educational Codeforces Round 33 (Rated for Div. 2) E. Counting Arrays [数论II][组合数学]
  • [学习笔记]拉格朗日插值
  • 网络的分类
  • IIS 6.0/7.0/7.5、Nginx、Apache 等Web Service解析漏洞总结
  • Python3 标准库概览
  • C#进阶系列——AOP?AOP!
  • QT5 进度条传文件
  • 【5+】跨webview多页面 触发事件(二)
  • 【comparator, comparable】小总结
  • CentOS从零开始部署Nodejs项目
  • Go 语言编译器的 //go: 详解
  • Next.js之基础概念(二)
  • orm2 中文文档 3.1 模型属性
  • React 快速上手 - 07 前端路由 react-router
  • 案例分享〡三拾众筹持续交付开发流程支撑创新业务
  • 从零搭建Koa2 Server
  • 分布式熔断降级平台aegis
  • 开源中国专访:Chameleon原理首发,其它跨多端统一框架都是假的?
  • 聊聊flink的TableFactory
  • 设计模式走一遍---观察者模式
  • 源码之下无秘密 ── 做最好的 Netty 源码分析教程
  • 摩拜创始人胡玮炜也彻底离开了,共享单车行业还有未来吗? ...
  • ###STL(标准模板库)
  • #pragma once
  • #我与Java虚拟机的故事#连载01:人在JVM,身不由己
  • (¥1011)-(一千零一拾一元整)输出
  • (a /b)*c的值
  • (aiohttp-asyncio-FFmpeg-Docker-SRS)实现异步摄像头转码服务器
  • (floyd+补集) poj 3275
  • (二)linux使用docker容器运行mysql
  • (附源码)spring boot建达集团公司平台 毕业设计 141538
  • (六)库存超卖案例实战——使用mysql分布式锁解决“超卖”问题
  • (转)socket Aio demo
  • (轉貼) UML中文FAQ (OO) (UML)
  • ./和../以及/和~之间的区别
  • .net core 6 集成和使用 mongodb
  • .net framework4与其client profile版本的区别
  • .NET gRPC 和RESTful简单对比
  • .NET MVC 验证码
  • .NET 中小心嵌套等待的 Task,它可能会耗尽你线程池的现有资源,出现类似死锁的情况
  • .NET/C# 反射的的性能数据,以及高性能开发建议(反射获取 Attribute 和反射调用方法)
  • .NET应用架构设计:原则、模式与实践 目录预览
  • .secret勒索病毒数据恢复|金蝶、用友、管家婆、OA、速达、ERP等软件数据库恢复
  • .set 数据导入matlab,设置变量导入选项 - MATLAB setvaropts - MathWorks 中国
  • /usr/bin/python: can't decompress data; zlib not available 的异常处理
  • [2013AAA]On a fractional nonlinear hyperbolic equation arising from relative theory
  • [20180312]进程管理其中的SQL Server进程占用内存远远大于SQL server内部统计出来的内存...