rsa加密解密算法代码
这个是我帮个朋友写的,写的时候发现其实这个没那么复杂,不过,时间复杂度要高于那些成型了的,为人所熟知的rsa算法的其他语言实现.
#include
int
candp(int
a,int
b,int
c)
{
int
r=1;
b=b+1;
while(b!=1)
{
r=r*a;
r=r%c;
b--;
}
printf("%d",r);
return
r;
}
void
main()
{
int
p,q,e,d,m,n,t,c,r;
char
s;
{printf("input
the
p:\n");
scanf("%d\n",&p);
printf("input
the
q:\n");
scanf("%d%d\n",&p);
n=p*q;
printf("so,the
n
is
%3d\n",n);
t=(p-1)*(q-1);
printf("so,the
t
is
%3d\n",t);
printf("please
intput
the
e:\n");
scanf("%d",&e);
if(e<1||e>t)
{printf("e
is
error,please
input
again;");
scanf("%d",&e);}
d=1;
while
(((e*d)%t)!=1)
d++;
printf("then
caculate
out
that
the
d
is
%5d",d);
printf("if
you
want
to
konw
the
cipher
please
input
1;\n
if
you
want
to
konw
the
plain
please
input
2;\n");
scanf("%d",&r);
if(r==1)
{
printf("input
the
m
:"
);/*输入要加密的明文数字*/
scanf("%d\n",&m);
c=candp(m,e,n);
printf("so
,the
cipher
is
%4d",c);}
if(r==2)
{
printf("input
the
c
:"
);/*输入要解密的密文数字*/
scanf("%d\n",&c);
m=candp(c,d,n);
printf("so
,the
cipher
is
%4d\n",m);
printf("do
you
want
to
use
this
programe:yes
or
no");
scanf("%s",&s);
}while(s=='y');
}
}
2. C++ rsa加密解密算法
你的程序直接运行结束了,所以你什么都看不见。
你可以在你的MAIN函数最后一行加一句:
getchar();
或者
system("pause");
另外如果你输出的内容是非可见字符,那你也看不见,你下个断点,看看变量的值就看见了。
3. 如何用VB实现RSA加密算法,网上找到了一份代码,没有注释看不懂,请大神解释!!!
RSA算法非常简单,概述如下:
找两素数p和q
取n=p*q
取t=(p-1)*(q-1)
取任何一个数e,要求满足e<t并且e与t互素(就是最大公因数为1)
取d*e%t==1
这样最终得到三个数: n d e
设消息为数M (M <n)
设c=(M**d)%n就得到了加密后的消息c
设m=(c**e)%n则 m == M,从而完成对c的解密。
注:**表示次方,上面两式中的d和e可以互换。
在对称加密中:
n d两个数构成公钥,可以告诉别人;
n e两个数构成私钥,e自己保留,不让任何人知道。
给别人发送的信息使用e加密,只要别人能用d解开就证明信息是由你发送的,构成了签名机制。
别人给你发送信息时使用d加密,这样只有拥有e的你能够对其解密。
rsa的安全性在于对于一个大数n,没有有效的方法能够将其分解
从而在已知n d的情况下无法获得e;同样在已知n e的情况下无法
求得d。
<二>实践
接下来我们来一个实践,看看实际的操作:
找两个素数:
p=47
q=59
这样
n=p*q=2773
t=(p-1)*(q-1)=2668
取e=63,满足e<t并且e和t互素
用perl简单穷举可以获得满主 e*d%t ==1的数d:
C:\Temp>perl -e "foreach $i (1..9999){ print($i),last if $i*63%2668==1 }"
847
即d=847
最终我们获得关键的
n=2773
d=847
e=63
取消息M=244我们看看
加密:
c=M**d%n = 244**847%2773
用perl的大数计算来算一下:
C:\Temp>perl -Mbigint -e "print 244**847%2773"
465
即用d对M加密后获得加密信息c=465
解密:
我们可以用e来对加密后的c进行解密,还原M:
m=c**e%n=465**63%2773 :
C:\Temp>perl -Mbigint -e "print 465**63%2773"
244
即用e对c解密后获得m=244 , 该值和原始信息M相等。
<三>字符串加密
把上面的过程集成一下我们就能实现一个对字符串加密解密的示例了。
每次取字符串中的一个字符的ascii值作为M进行计算,其输出为加密后16进制
的数的字符串形式,按3字节表示,如01F
代码如下:
#!/usr/bin/perl -w
#RSA 计算过程学习程序编写的测试程序
#watercloud 2003-8-12
#
use strict;
use Math::BigInt;
my %RSA_CORE = (n=>2773,e=>63,d=>847); #p=47,q=59
my $N=new Math::BigInt($RSA_CORE{n});
my $E=new Math::BigInt($RSA_CORE{e});
my $D=new Math::BigInt($RSA_CORE{d});
print "N=$N D=$D E=$E\n";
sub RSA_ENCRYPT
{
my $r_mess = shift @_;
my ($c,$i,$M,$C,$cmess);
for($i=0;$i < length($$r_mess);$i++)
{
$c=ord(substr($$r_mess,$i,1));
$M=Math::BigInt->new($c);
$C=$M->(); $C->bmodpow($D,$N);
$c=sprintf "%03X",$C;
$cmess.=$c;
}
return \$cmess;
}
sub RSA_DECRYPT
{
my $r_mess = shift @_;
my ($c,$i,$M,$C,$dmess);
for($i=0;$i < length($$r_mess);$i+=3)
{
$c=substr($$r_mess,$i,3);
$c=hex($c);
$M=Math::BigInt->new($c);
$C=$M->(); $C->bmodpow($E,$N);
$c=chr($C);
$dmess.=$c;
}
return \$dmess;
}
my $mess="RSA 娃哈哈哈~~~";
$mess=$ARGV[0] if @ARGV >= 1;
print "原始串:",$mess,"\n";
my $r_cmess = RSA_ENCRYPT(\$mess);
print "加密串:",$$r_cmess,"\n";
my $r_dmess = RSA_DECRYPT($r_cmess);
print "解密串:",$$r_dmess,"\n";
#EOF
测试一下:
C:\Temp>perl rsa-test.pl
N=2773 D=847 E=63
原始串:RSA 娃哈哈哈~~~
加密串:
解密串:RSA 娃哈哈哈~~~
C:\Temp>perl rsa-test.pl 安全焦点(xfocus)
N=2773 D=847 E=63
原始串:安全焦点(xfocus)
加密串:
解密串:安全焦点(xfocus)
4. 求正确的RSA加密解密算法C语言的,多谢。
RSA算法它是第一个既能用于数据加密也能用于数字签名的算法。它易于理解和操作,也很流行。算法的名字以发明者的名字命名:RonRivest,AdiShamir和LeonardAdleman。但RSA的安全性一直未能得到理论上的证明。它经历了各种攻击,至今未被完全攻破。一、RSA算法:首先,找出三个数,p,q,r,其中p,q是两个相异的质数,r是与(p-1)(q-1)互质的数p,q,r这三个数便是privatekey接着,找出m,使得rm==1mod(p-1)(q-1)这个m一定存在,因为r与(p-1)(q-1)互质,用辗转相除法就可以得到了再来,计算n=pqm,n这两个数便是publickey编码过程是,若资料为a,将其看成是一个大整数,假设a=n的话,就将a表成s进位(s因为rm==1mod(p-1)(q-1),所以rm=k(p-1)(q-1)+1,其中k是整数因为在molo中是preserve乘法的(x==ymodzan==vmodz=>xu==yvmodz),所以,c==b^r==(a^m)^r==a^(rm)==a^(k(p-1)(q-1)+1)modpq1.如果a不是p的倍数,也不是q的倍数时,则a^(p-1)==1modp(费马小定理)=>a^(k(p-1)(q-1))==1modpa^(q-1)==1modq(费马小定理)=>a^(k(p-1)(q-1))==1modq所以p,q均能整除a^(k(p-1)(q-1))-1=>pq|a^(k(p-1)(q-1))-1即a^(k(p-1)(q-1))==1modpq=>c==a^(k(p-1)(q-1)+1)==amodpq2.如果a是p的倍数,但不是q的倍数时,则a^(q-1)==1modq(费马小定理)=>a^(k(p-1)(q-1))==1modq=>c==a^(k(p-1)(q-1)+1)==amodq=>q|c-a因p|a=>c==a^(k(p-1)(q-1)+1)==0modp=>p|c-a所以,pq|c-a=>c==amodpq3.如果a是q的倍数,但不是p的倍数时,证明同上4.如果a同时是p和q的倍数时,则pq|a=>c==a^(k(p-1)(q-1)+1)==0modpq=>pq|c-a=>c==amodpqQ.E.D.这个定理说明a经过编码为b再经过解码为c时,a==cmodn(n=pq)但我们在做编码解码时,限制0intcandp(inta,intb,intc){intr=1;b=b+1;while(b!=1){r=r*a;r=r%c;b--;}printf("%d\n",r);returnr;}voidmain(){intp,q,e,d,m,n,t,c,r;chars;printf("pleaseinputthep,q:");scanf("%d%d",&p,&q);n=p*q;printf("thenis%3d\n",n);t=(p-1)*(q-1);printf("thetis%3d\n",t);printf("pleaseinputthee:");scanf("%d",&e);if(et){printf("eiserror,pleaseinputagain:");scanf("%d",&e);}d=1;while(((e*d)%t)!=1)d++;printf("thencaculateoutthatthedis%d\n",d);printf("thecipherpleaseinput1\n");printf("theplainpleaseinput2\n");scanf("%d",&r);switch(r){case1:printf("inputthem:");/*输入要加密的明文数字*/scanf("%d",&m);c=candp(m,e,n);printf("thecipheris%d\n",c);break;case2:printf("inputthec:");/*输入要解密的密文数字*/scanf("%d",&c);m=candp(c,d,n);printf("thecipheris%d\n",m);break;}getch();}
5. rsa算法实现代码
你看看这个行不行,位数可以自己改,今天在网上找到了,我也想用C生成512、1024位的大素数进行RSA加密。。如果谁有好方法麻烦共享下:[email protected],跪谢
package test;
import java.math.BigInteger;
// 生成一个随机大整数,然后找出比这个整数大的下一个素数
public class Primes {
// 下面的 BigInteger.ZERO 和 BigInteger.ONE 在 JDK 1.1 中是无效的
private static final BigInteger ZERO = BigInteger.ZERO;
private static final BigInteger ONE = BigInteger.ONE;
private static final BigInteger TWO = new BigInteger("2");
// 产生一个错误素数的概率小于 1/2 的 ERR_VAL 次方,可以将 ERR_VAL 定义为 200,降低其错误率
// Java 应该使用的是 Miller-Rabin 测试法,这种错误概率基本上可以认为是无错误。
private static final int ERR_VAL = 100;
private static StringBuffer[] digits = { new StringBuffer("0"), new StringBuffer("1"), new StringBuffer("2"), new StringBuffer("3"), new StringBuffer("4"), new StringBuffer("5"),
new StringBuffer("6"), new StringBuffer("7"), new StringBuffer("8"), new StringBuffer("9") };
private static StringBuffer randomDigit(boolean isZeroOK) {
// 产生一个随机的数字(字符串形式的),isZeroOK 决定这个数字是否可以为 0
int index;
if (isZeroOK)
index = (int) Math.floor(Math.random() * 10);
else
index = 1 + (int) Math.floor(Math.random() * 9);
return (digits[index]);
}
public static BigInteger bigRandom(int numDigits) {
// 产生一个随机大整数,各位上的数字都是随机产生的,首位不为 0
StringBuffer s = new StringBuffer("");
for (int i = 0; i < numDigits; i++)
if (i == 0)
s.append(randomDigit(false));
else
s.append(randomDigit(true));
return (new BigInteger(s.toString()));
}
private static boolean isEven(BigInteger n) {
// 测试一个大整数是否为偶数
return (n.mod(TWO).equals(ZERO));
}
public static BigInteger nextPrime(BigInteger start) {
// 产生一个比给定大整数 start 大的素数,错误率低于 1/2 的 ERR_VAL 次方
if (isEven(start))
start = start.add(ONE);
else
start = start.add(TWO);
if (start.isProbablePrime(ERR_VAL))
return (start);
else
// 采用递归方式(递归的层数会是个天文数字吗?)
return (nextPrime(start));
}
// 一个基于命令行的测试程序,如果位数错误,默认 150 位,输出 20 个素数
public static void main(String[] args) {
int numDigits;
try {
numDigits = Integer.parseInt(args[0]);
} catch (Exception e) {
numDigits = 128;
}
BigInteger start = bigRandom(numDigits);
start = nextPrime(start);
BigInteger end = bigRandom(5);
end = nextPrime(end);
System.out.println("大素数" + start);
System.out.println("大素数" + end);
BigInteger result = start.multiply(end);
System.out.println("结果数" + result);
6. C++实现RSA加密解密算法
#include <iostream>
using namespace std;
template <class HugeInt>
HugeInt Power( const HugeInt & x, const HugeInt & n, // 求x^n mod p
const HugeInt & p )
{
if( n == 0 )
return 1;
HugeInt tmp = Power( ( x * x ) % p, n / 2, p );
if( n % 2 != 0 )
tmp = ( tmp * x ) % p;
return tmp;
}
template <class HugeInt>
void fullGcd( const HugeInt & a, const HugeInt & b, //
HugeInt & x, HugeInt & y )
{
HugeInt x1, y1;
if( b == 0 )
{
x = 1;
y = 0;
}
else
{
fullGcd( b, a % b, x1, y1 );
x = y1;
y = x1 - ( a / b ) * y1;
}
}
template <class HugeInt>
HugeInt inverse( const HugeInt & p, const HugeInt & q, // 求d
const HugeInt & e )
{
int fyn = ( 1 - p ) * ( 1 - q );
HugeInt x, y;
fullGcd( fyn, e, x, y );
return x > 0 ? x : x + e;
}
int main( )
{
cout << "Please input the plaintext: " << endl;
int m;
cin >> m;
cout << "Please input p,q and e: " << endl;
int p, q, e;
cin >> p >> q >> e;
int n = p * q;
int d = inverse( p, q, e );
int C = Power( m, e, n );
cout << "The ciphertext is: " << C << endl;
cout << "\n\nPlease input the ciphertext: " << endl;
cin >> C;
cout << "\n\nPlease input p, q and d: " << endl;
cin >> p >> q >> d;
n = p * q;
m = Power( C, d, n );
cout <<"The plaintext is: " << m << endl << endl;
system( "pause" );
return 0;
}
这就是RSA加密解密算法
7. 求RSA加密解密算法,c++源代码
#include<iostream.h>
#include<stdio.h>
#include<math.h>
int pf_c(int m,int k);
int pf(int m1,int n1);
int gcd(int f);
int r;
int h;
void main()
{ int a,b,c,d,d1,a1,b1,c1;
cout<<"请输入你选择的2个大素数!"<<endl;
cin>>a1;
cin>>b1;
r=a1*b1;
c=(a1-1)*(b1-1);
c1=gcd(c);
cout<<"公开钥为:"<<c1<<endl;
cout<<"请选择你要的操作:1.加密 2.解密"<<endl;
cin>>a;
switch(a){
case 1: cout<<"请输入明文:"<<endl;
cin>>b;
cout<<"密文为:"<<pf_c(b,c1)<<endl;
break;
case 2: cout<<"请输入密文:"<<endl;
cin>>d;
d1=pf(c,c1);
cout<<"私密钥为:"<<d1<<endl;
cout<<"明文为:"<<pf_c(d,d1)<<endl;
break;
}
getchar();
}
int pf_c(int m,int k)
{
int a,i1,a1,b[50],c1,c;
c=0;c1=1;i1=0;
do{
a=k/2;
a1=k%2;
b[i1]=a1;
k=a;
i1++;
}while(a>0);
i1--;
for(int i=i1;i>=0;i--)
{
c=2*c;
c1=(c1*c1)%r;
if(b[i]==1)
{
c=c+1;
c1=(c1*m)%r;
}
}
return c1;
}
int pf(int m1,int n1)
{
int x1=1,x2=0,x3;
int y1=0,y2=1,y3;
x3=m1;
y3=n1;
int d;
for(int i=0; ;i++)
{
int q=x3/y3;
int t1=x1-q*y1;
int t2=x2-q*y2;
int t3=x3-q*y3;
x1=y1;
x2=y2;
x3=y3;
y1=t1;
y2=t2;
y3=t3;
if(y3==1)
{
if(y2<0) d=m1+y2;
else d=y2;
break;
}
}
return d;
}
int gcd(int f)
{
int x1=1,x2=0,x3;
int y1=0,y2=1,y3;
for(int i1=2;i1<f;i1++)
{
x3=f;
y3=i1;
int q=x3/y3;
int t1=x1-q*y1;
int t2=x2-q*y2;
int t3=x3-q*y3;
x1=y1;
x2=y2;
x3=y3;
y1=t1;
y2=t2;
y3=t3;
if(y3==1)
{
return i1;
break;
}
}
}
8. 有没有知道怎样用C语言来写RSA加密算法的代码的谢谢
#region RSA的加密函数 //##############################################################################
//RSA 方式加密
//说明KEY必须是XML的行式,返回的是字符串
//在有一点需要说明!!该加密方式有 长度 限制的!!
//##############################################################################
//RSA的加密函数 string public string RSAEncrypt(string xmlPublicKey, string m_strEncryptString)
{
byte[] PlainTextBArray; byte[] CypherTextBArray;
string Result;
RSACryptoServiceProvider rsa = new RSACryptoServiceProvider();
rsa.FromXmlString(xmlPublicKey);
PlainTextBArray = (new UnicodeEncoding()).GetBytes(m_strEncryptString);
CypherTextBArray = rsa.Encrypt(PlainTextBArray, false);
Result = Convert.ToBase64String(CypherTextBArray);
return Result;
} //RSA的加密函数 byte[]
public string RSAEncrypt(string xmlPublicKey, byte[] EncryptString)
{
byte[] CypherTextBArray; string Result;
RSACryptoServiceProvider rsa = new RSACryptoServiceProvider();
rsa.FromXmlString(xmlPublicKey);
CypherTextBArray = rsa.Encrypt(EncryptString, false);
Result = Convert.ToBase64String(CypherTextBArray);
return Result;
} #endregion
#region RSA的解密函数 //RSA的解密函数 string
public string RSADecrypt(string xmlPrivateKey, string m_strDecryptString)
{
byte[] PlainTextBArray;
byte[] DypherTextBArray;
string Result;
System.Security.Cryptography.RSACryptoServiceProvider rsa = new RSACryptoServiceProvider();
rsa.FromXmlString(xmlPrivateKey);
PlainTextBArray = Convert.FromBase64String(m_strDecryptString);
DypherTextBArray = rsa.Decrypt(PlainTextBArray, false);
Result = (new UnicodeEncoding()).GetString(DypherTextBArray);
return Result;
}
//RSA的解密函数 byte public string RSADecrypt(string xmlPrivateKey, byte[] DecryptString)
{
byte[] DypherTextBArray;
string Result;
System.Security.Cryptography.RSACryptoServiceProvider rsa = new RSACryptoServiceProvider();
rsa.FromXmlString(xmlPrivateKey);
DypherTextBArray = rsa.Decrypt(DecryptString, false);
Result = (new UnicodeEncoding()).GetString(DypherTextBArray);
return Result;
} #endregion
#endregion
9. 求python RSA 算法加密字符串的完整源代码。
import rsa rsaPublickey = int(pubkey, 16) key = rsa.PublicKey(rsaPublickey, 65537) #创建公钥 message = str(servertime) + '\t' + str(nonce) + '\n' + str(password) #拼接明文js加密文件中得到 passwd = rsa.encrypt(message, key) #加密 passwd = binascii.b2a_hex(passwd) #将加密信息转换为16进制。 return passwd