RSA 公钥加密算法
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 公钥加密算法的更多相关文章
- RSA公钥,私钥和数字签名通用理解
一.公钥加密 假设一下,我找了两个数字,一个是1,一个是2.我喜欢2这个数字,就保留起来,不告诉你们(私钥),然后我告诉大家,1是我的公钥. 我有一个文件,不能让别人看,我就用1加密了.别人找到了这个 ...
- 公钥加密算法那些事 | RSA 与 ECC 系统对比
一.背景 据记载,公元前 400 年,古希腊人发明了置换密码.1881 年世界上的第一个电话保密专利出现.在第二次世界大战期间,德国军方启用「恩尼格玛」密码机,密码学在战争中起着非常重要的作用. 随着 ...
- RSA—非对称加密算法
RSA:非对称加密算法加解密原理如下:已知:p,q,n,e,d,m,c其中:p与q互为大质数,n=p*q 公钥Pk(n,e):加密使用,是公开的 私钥Sk(n,d):解密使用,不公开 c:明文 m:密 ...
- SSH加密原理、RSA非对称加密算法学习与理解
首先声明一下,这里所说的SSH,并不是Java传统的三大框架,而是一种建立在应用层和传输层基础上的安全外壳协议,熟悉Linux的朋友经常使 用到一 个SSH Secure Shell Cilent的工 ...
- RSA非对称加密算法实现过程
RSA非对称加密算法实现过程 非对称加密算法有很多,RSA算法就是其中比较出名的算法之一,下面是具体实现过程 <?php /** */ class Rsa { /** * private key ...
- RSA公钥验签
1.业务场景,公司做理财业务,但是可能有第三方合作.与第三方合作获得更多客户流量.别人可以在第三方进行购买理财产品.那么怎么保证交易信息的安全性那,我们这里给出rsa加密实现原理. 2.工具类rsa: ...
- 生成 RSA 公钥和私钥的方法
在使用 RSA 加密算法时,需要使用到一对 公钥 和 私钥,生成 公钥 和 私钥 需要借助 openssl 这款工具,下载这款工具的地址如下: http://slproweb.com/products ...
- rsa公钥和私钥到底哪个才是用来加密,哪个用来解密?
本文转自:91博客:原文地址:http://www.9191boke.com/138589019.html 公钥和私钥在一些银行系统.第三方支付系统SDK中经常会遇到,刚接触公钥私钥的朋友们估计很难区 ...
- RSA非对称加密算法实现:Python
RSA是1977年由罗纳德·李维斯特(Ron Rivest).阿迪·萨莫尔(Adi Shamir)和伦纳德·阿德曼(Leonard Adleman)一起提出的.当时他们三人都在麻省理工学院工作.RSA ...
随机推荐
- How to Install Zabbix Server on Centos6.7
Prerequisite Environment First you must use your Subscription Manager to enable SCL: [root@fileserve ...
- 【android】6大布局
线性布局 相对布局 绝对布局 网格布局 表格布局 帧布局
- 【php】【异步】php实现异步的几种方法
请参考 4种php常用的异步执行方式 ajax 和 img 的 src 属性 系统指令调用 (在php代码里面调用系统指令) curl socket通信
- oracle如何保证读一致性 第一弹
oracle保证读一致性原理 1:undo segment的概念 当数据库进行修改的时候,需要把保存到以前的old的数据保存到一个地方,然后进行修改,用于保存o ...
- The 2018 ACM-ICPC China JiangSu Provincial Programming Contest J. Set
Let's consider some math problems. JSZKC has a set A=A={1,2,...,N}. He defines a subset of A as 'Meo ...
- Linux学习-逻辑滚动条管理员 (Logical Volume Manager)
LVM 可以整合多个实体 partition 在一起, 让这些 partitions 看起来就像是一个磁盘一样!而且,还可以在未来新增或移除其他的实 体 partition 到这个 LVM 管理的磁盘 ...
- HDU 3594 Cactus 有向仙人掌图判定
题意 给出一个有向图,并给出仙人掌图的定义 图本身是强连通的 每条边属于且只属于一个环 判断输入的图是否是强连通的. 分析 杭电OJ上的数据比较弱,网上一些有明显错误的代码也能AC. 本着求真务实的精 ...
- base64转图片
y一个简单的工具类,附上: /** * @param imgStr 图片的base64 * @param path 将要生成的地址 * @return */ public static boolean ...
- luogu2293 [JSOI2008]Blue Mary开公司
ref好像叫什么李超线段树?--这篇不是太通用-- #include <iostream> #include <cstdio> #include <cmath> u ...
- selenium - 常用页面操作
# 2.常用页面操作 # 访问某一个页面url = 'http://www.baidu.com'driver.get(url) # 获取页面的标题title = driver.titleprint(t ...