RSA雏形
April 2, 2007初步的代码,不过思路差不多了。
//Main.c
#include”key_produce.h”
#include
using namespace std;
unsigned int p=11;
unsigned int q=19;
unsigned int* Ke=new unsigned int(0);
unsigned int* Kd= new unsigned int(0);
unsigned int* model= new unsigned int(0);
unsigned int* ibit= new unsigned int(-1);
KEY c;
int M;
int mi;
char s[1000];
int getlength(int i)
{
itoa(i,s,2);
int t=0;
while(s[t]!=NULL)
t++;
cout< <"指数的二进制 "<
return t;
}
void getb(int i,unsigned int* ibit)
{
int t=0;
while(s[t]!=NULL)
t++;
*ibit = s[t-i];
if(*ibit==49) *ibit=1;
else if(*ibit==48) *ibit=0;
}
int MO(int x,int e,int n)
{
int d=1; cout<<"模 = "< //加密 cout<<"计算出的Ke "<<*Ke< //解密 cout<<"解密得到的明文:"< } int main() //.h class KEY int JudgePrime(unsigned int prime) int Count_N_AoLa_Num(unsigned int p, unsigned int q, unsigned int * ao_la) unsigned int CountCommonData(unsigned int a, unsigned int b) int RandSelect_e( unsigned int ao_la, unsigned int* e ) *e = tmp;
cout<
cout<<"指数的长度 "<
{
d=(d*d)%n;
// cout< <"d=(d*d)%n 后"<
if(*ibit==1) d=(x*d)%n;
{
// cout<<"第"<
// cout<<"此时d=(x*d)%n 后 "<
// cout<<"此时d= "<
}
cout<<"模后的结果 "<
}
void Encryption()
{
c.GetKey(p, q,Ke,Kd,model);
cin>>M;
mi = MO(M,*Ke,*model);
// mi = MO(66,77,119);
cout< <"最终密文是 "<
void Decipher()
{
int MM = MO(mi,*Ke,*model);
cout<
{
Encryption();
// Encryption();
Decipher();
return 0;
}
#include
#include
{
public:
int GetKey(unsigned int p,unsigned int q, unsigned int* Ke, unsigned int* Kd, unsigned int* model)
{
unsigned int ao_la;
if( Count_N_AoLa_Num(p, q, &ao_la) )
{
if( RandSelect_e(ao_la, Ke) )
{
if( OverOneNum(*Ke, ao_la, Kd) )
{
*model= p*q;
return 1;
}
}
}
return 0;
}
{
unsigned int i;
unsigned int limit= (unsigned int)sqrt( (double)prime );
for(i=2; i < = limit; i++)
{
if(prime%i==0)
{
return 0;
}
}
return 1;
}
{
if( !JudgePrime(p) )
return 0;
if( !JudgePrime(q) )
return 0;
*ao_la = (p-1)*(q-1);
return 1;
}
{
unsigned int c ;
if( c= a%b )
return CountCommonData(b,c);
else
return b;
}
{
unsigned int tmp;
unsigned int div;
if( ao_la <= 2 )
{
return 0;
}
srand( time(0) );
div= ao_la - 2;
do
{
tmp = ( (unsigned int)rand()+90 ) % div + 2;
}while( (CountCommonData(tmp, ao_la)!=1)&&(tmp
std::cout<<"ao_la = "<
Comments »
The URI to TrackBack this entry is: http://lwj.blogsome.com/2007/04/02/p12/trackback/
No comments yet.
RSS feed for comments on this post.
Leave a comment
Line and paragraph breaks automatic, e-mail address never displayed, HTML allowed: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <code> <em> <i> <strike> <strong>

