RSA公钥加密算法是1977年由Ron Rivest、Adi Shamirh和LenAdleman在(美国麻省理工学院)开发的。

这个算法的名字也是他们三个人名字首字母,RSA算法基于一个十分简单的数论事实:

将两个大素数相乘十分容易,但想要对其乘积进行因式分解却极其困难,因此可以将乘积公开作为加密密钥。

package rsa;
import java.math.BigInteger; public class RSA {
private long p,q,e,d,n;
public RSA(){
int pIndex = (int)(Math.random()*10);
int qIndex;
int eIndex;
do{
qIndex = (int)(Math.random()*10);
}
while(qIndex==pIndex);
do{
eIndex = (int)(Math.random()*10);
}
while(eIndex==pIndex||eIndex==pIndex);
p = 1033;
q = 2017;
e = 29437;
n = p*q;
d = calculateD();
}
private long calculateD(){
long t0 = 0,t1 = 1,t2 = -1;
long r0 = (p-1)*(q-1), m = r0,r1 = e ,r2 = -1;
do{
long q = r0/r1;
r2 = r0-r1*q;
if(r2==0)break;
t2 = t0 - t1*q;
while(t2<0){
t2+=m;
}
if(t2>=m){
t2 %= m;
}
r0 = r1;
r1 = r2;
t0 = t1;
t1 = t2;
}while(r2!=0);
if(r1!=1){
return 0;
}
else{
return t2;
}
} public long getE() {
return e;
}
public long getN() {
return n;
}
public long getD() {
return d;
}
public BigInteger encode(BigInteger data){
return pow(data,d).mod(new BigInteger(n+""));
}
public BigInteger decode(BigInteger code){
return pow(code,e).mod(new BigInteger(n+""));
}
public BigInteger pow(BigInteger data,long p){
data = data.pow((int)p);
return data;
}
public static void main(String args[]){
RSA rsa = new RSA(); BigInteger data = new BigInteger("222222");
long oldtime = System.currentTimeMillis();
BigInteger code = rsa.encode(data);
long newtime = System.currentTimeMillis();
double codetime = ((double)(newtime-oldtime))/1000;
oldtime = System.currentTimeMillis();
BigInteger decode = rsa.decode(code);
newtime = System.currentTimeMillis();
double decodetime = ((double)(newtime-oldtime))/1000;
System.out.println("privateKey:"+rsa.d);
System.out.println("publickKey:"+rsa.e);
System.out.println("N:"+rsa.n);
System.out.println("data:"+data);
System.out.println("code:"+code+" time:"+codetime);
System.out.println("decode:"+decode+" time:"+decodetime); } }

RSA 公钥加密算法的更多相关文章

  1. RSA公钥,私钥和数字签名通用理解

    一.公钥加密 假设一下,我找了两个数字,一个是1,一个是2.我喜欢2这个数字,就保留起来,不告诉你们(私钥),然后我告诉大家,1是我的公钥. 我有一个文件,不能让别人看,我就用1加密了.别人找到了这个 ...

  2. 公钥加密算法那些事 | RSA 与 ECC 系统对比

    一.背景 据记载,公元前 400 年,古希腊人发明了置换密码.1881 年世界上的第一个电话保密专利出现.在第二次世界大战期间,德国军方启用「恩尼格玛」密码机,密码学在战争中起着非常重要的作用. 随着 ...

  3. RSA—非对称加密算法

    RSA:非对称加密算法加解密原理如下:已知:p,q,n,e,d,m,c其中:p与q互为大质数,n=p*q 公钥Pk(n,e):加密使用,是公开的 私钥Sk(n,d):解密使用,不公开 c:明文 m:密 ...

  4. SSH加密原理、RSA非对称加密算法学习与理解

    首先声明一下,这里所说的SSH,并不是Java传统的三大框架,而是一种建立在应用层和传输层基础上的安全外壳协议,熟悉Linux的朋友经常使 用到一 个SSH Secure Shell Cilent的工 ...

  5. RSA非对称加密算法实现过程

    RSA非对称加密算法实现过程 非对称加密算法有很多,RSA算法就是其中比较出名的算法之一,下面是具体实现过程 <?php /** */ class Rsa { /** * private key ...

  6. RSA公钥验签

    1.业务场景,公司做理财业务,但是可能有第三方合作.与第三方合作获得更多客户流量.别人可以在第三方进行购买理财产品.那么怎么保证交易信息的安全性那,我们这里给出rsa加密实现原理. 2.工具类rsa: ...

  7. 生成 RSA 公钥和私钥的方法

    在使用 RSA 加密算法时,需要使用到一对 公钥 和 私钥,生成 公钥 和 私钥 需要借助 openssl 这款工具,下载这款工具的地址如下: http://slproweb.com/products ...

  8. rsa公钥和私钥到底哪个才是用来加密,哪个用来解密?

    本文转自:91博客:原文地址:http://www.9191boke.com/138589019.html 公钥和私钥在一些银行系统.第三方支付系统SDK中经常会遇到,刚接触公钥私钥的朋友们估计很难区 ...

  9. RSA非对称加密算法实现:Python

    RSA是1977年由罗纳德·李维斯特(Ron Rivest).阿迪·萨莫尔(Adi Shamir)和伦纳德·阿德曼(Leonard Adleman)一起提出的.当时他们三人都在麻省理工学院工作.RSA ...

随机推荐

  1. 封装一个优雅的element ui表格组件

    现在做后台系统用vue + elementUI 的越来越多,那element ui的 el-table 组件肯定也离不开.虽然element ui的table组件很好.但是表格和分页是分离的.每次写表 ...

  2. ubuntu 设置定时任务

    crontab -l  #查看详情crontab -e #设置定时任务 * * * * * command 分 时 日 月 周 命令 第1列表示分钟1-59 每分钟用*或者 */1表示 第2列表示小时 ...

  3. python3通过Beautif和XPath分别爬取“小猪短租-北京”租房信息,并对比时间效率(附源代码)

    爬虫思路分析: 1. 观察小猪短租(北京)的网页 首页:http://www.xiaozhu.com/?utm_source=baidu&utm_medium=cpc&utm_term ...

  4. LeetCode(44) Wildcard Matching

    题目 Implement wildcard pattern matching with support for '?' and '*'. '?' Matches any single characte ...

  5. Tourists Gym - 101002I LCA——dfs+RMQ在线算法

    LCA(Least Common Ancestors),即最近公共祖先,是指这样一个问题:在有根树中,找出某两个结点u和v最近的公共祖先(另一种说法,离树根最远的公共祖先). 知识需求:1)RMQ的S ...

  6. 再谈H2的MVStore与MVMap

    对H2的[MVStore]: http://www.cnblogs.com/simoncook/p/5188105.html 这篇文章的补充. 概述 我们通常用的map,比如HashMap Linke ...

  7. Clickonce - Change deployment URL after publish

    mage.exe -Update C:\inetpub\wwwroot\aspnet40\AminoScience\Uploads\Application Files\AccUFeed_1_0_0_5 ...

  8. selenium - Js处理滚动条操作

    # 11.Js处理滚动条操作 driver.execute_script('arguments[0].scrollIntoView();',target) target 为find_element_b ...

  9. 0016.Linux基础之常用基本命令讲解

    开启linux系统,开启xshell pwd:printing workding directory 打印当前目录 /:代表根目录 cd:change directory 改变目录 ls:list 查 ...

  10. Educational Codeforces Round 37 (Rated for Div. 2)

    我的代码应该不会被hack,立个flag A. Water The Garden time limit per test 1 second memory limit per test 256 mega ...