一. RSA算法说明
(1)选择长度相等的两个大的素数p,q,计算其积n=p.q
)2)计算n的欧拉数(n )=(p-1 ).(q-1 )
)3)随机选择加密密钥e,使e和(p-1 )、(q-1 )为素数
)4)使用生气长颈鹿的扩展算法计算解密密钥d,满足ed=1mod(n ),) e,n )为公钥,d为私钥。
)5)加密明文(ci=mi ^ e (modn ) ) ) ) ) ) ) ) ) )。
(6)密文的解密: mi=ci^d(MODN ) ) () ) ) ) ) ) ) ) ) ) ) ) ) ) ) ) ) ) ) ) ) ) ) ) ) ) )
二. RSA实现的Java代码
1.Java程序
包RSA;
//RSA算法的Java实现
import java.io.*;
公共类RSA
{
//p、q、n、e、d等定义并初始化值
私有输入p=0;
private int q=0;
私有龙格n=0;
私有龙格m=0;
私有long public _ key=0;
private long private_key=0; //键
私有长文本=0; //明文
private long secretword=0; //密文
私有长word=0; //解密成明文
//判断输入的p、q是否为primenumber (素数)
公共权限编号(longt )。
{
长k=0;
k=(long ) math.sqrt ) double ) t;
布尔标志=true;
outer:for(intI=2; i=k; I )
{
if () t%I )==0) ) ) )。
{
flag=false;
勇敢的饼干;
}
}
返回标志;
}
//通过键盘输入素数p,q
//判定输入是否为素数
public void inputPQ () throws Exception
{
do{
请输入System.out.println ('素数p: ' );
bufferedreaderstdin=newbufferedreader (newinputstreamreader ) system.in );
String br=stdin.readLine (;
this.p=integer.Parseint(br;
}
//输入不是素数时,在输入是素数之前继续画面上显示“请输入素数q”
while (! primenumber(this.p );
do{
请输入System.out.println ('素数q: ' );
bufferedreaderstdin=newbufferedreader (newinputstreamreader ) system.in );
String br=stdin.readLine (;
this.q=integer.Parseint(br;
}
//如果两个输入都满足条件(像素数),则计算它们的乘积
while (! primenumber(this.q );
this.n=this.p*this.q;
this.m=(p-1 ) * ) q-1 );
System.out.println ('这两个素数的乘积n为' this.n );
system.out.println('n的欧拉数=(p-1 ) ) q-1 ):this.m );
}
//求出最大公因子,调用java的gcd函数。 此函数返回两个或多个正数的最大公因子
publiclonggcd(longa,long b ) )。
{
龙gcd;
if(b==0) )。
gcd=a;
else
GCD=gcd(b,a%b );
System.out.println ('最大公因子:' gcd );
返回gcd;
}
//提供公匙,要求此值小于m,且与m互质值小于m,且与m互质
public void getPublic_key () throws Exception
{
do{
System.out.println ('请输入公钥:
bufferedreaderstdin=newbufferedreader (newinputstreamreader ) system.in );
String br=stdin.readLine (;
用parseLong分析字符串,恢复为Long型数据
this.public _ key=long.parse long (br;
}while((this.public_key=this.m )|) this.gcd ) this.m,this.Public_key!=1);
System.out.println (公钥为' this.public_key );
}
(根据愤怒的长颈鹿扩张法,计算钥匙
public void getPrivate_key ()
{
long value=1;
outer:for(longI=1; I )
{
value=i*this.m 1;
system.out.println(value: ) value;
if () value%this.public_key==0) (value/this.public_key this.m ) )
{
this.private _ key=value/this.public _ key;
勇敢的饼干;
}
}
System.out.println ('生成的私钥是' this.private_key );
}
//从外部提供必要的加密明文
public void getText () throws Exception
{
System.out.println ('请输入明文:');
调用BufferReader从输入的字符流中读取文本并显示输出
bufferedreaderstdin=newbufferedreader (newinputstreamreader ) system.in );
String br=stdin.readLine (;
this.text=long.parselong(br;
}
//加密、解密计算
强制//类型转换会发出警告,并且SuppressWarnings语句会屏蔽警告消息
@ suppress warnings (empty-statement ) )。
publiclongcolum(longy,long n,long key ) )。
{
龙卷;
if(key==1) )。
mul=y%n;
else
mul=y*this.colum(y,n,key-1 ) %n;
返回多;
}
//加密解密
public void pascolum () throws Exception
{
this.getText (;
System.out.println (输入明文为:(this.text );
//加密过程
this.secretword=this.colum (this.text,this.n,this.public_key );
System.out.println ('计算出的密文为' this.secretword );
//解密过程
this.word=this.colum (this.secretword,this.n,this.private_key );
System.out.println ()解码后的明文为(this.word );
}
//在//java语言中,一个方法发生异常,必须将该异常传递给调用方法进行处理
//声明抛出异常的语句是throws Exception
publicstaticvoidmain (string [ ] args ) throws Exception
{
RSA t=new RSA (;
t.inputPQ (;
t.getPublic_key (;
t.getPrivate_key (;
t.pascolum (;
}
}
2 .运行结果
当p、q为11、13、e=7时,计算d=103、m=85、c=123符合实际运算结果,表明了编程算法的正确性