當前位置:編程學習大全網 - 源碼下載 - 查看文華加密源代碼

查看文華加密源代碼

//下面的程序是520慧琴寫的,已經用VC++ 6.0編譯通過。

# include & ltiostream.h & gt

# include & ltmath.h & gt

# include & ltstdio.h & gt

typedef int Elemtype

元素類型p,q,e;

Elemtype fn

元素類型m,c;

int flag = 0;

typedef void(* Msghandler)(void);

結構MsgMap {

char ch

Msghandler處理程序;

};

/*公鑰*/

結構PU {

elem type e;

元素類型n;

} pu

/*私鑰*/

結構PR {

元素類型d;

元素類型n;

} pr

/*確定壹個數是否是質數*/

bool test_prime(元素類型m) {

如果(m & lt= 1) {

返回false

}

else if (m == 2) {

返回true

}

否則{

for(int I = 2;我& lt= sqrt(m);i++) {

if((m % i) == 0) {

返回false

打破;

}

}

返回true

}

}

/*將十進制數據轉換為二進制數組*/

void switch_to_bit(元素類型b,元素類型bin[32]) {

int n = 0;

while(b & gt;0) {

bin[n]= b % 2;

n++;

b/= 2;

}

}

/*候選菜單,主界面*/

void Init() {

cout & lt& lt“* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * & lt;& ltendl

cout & lt& lt“***歡迎使用RSA編碼器* * *”& lt;& ltendl

cout & lt& lt“* * * a . about * * *”& lt;& ltendl

cout & lt& lt“* * * e . encrypt * * *”& lt;& ltendl

cout & lt& lt“* * * * d . decrypt * * *”& lt;& ltendl

cout & lt& lt“* * * s . setkey * * *”& lt;& ltendl

cout & lt& lt“* * * q . quit * * *”& lt;& ltendl

cout & lt& lt“* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * by *特裏* * *”& lt;& ltendl

cout & lt& lt"按壹個鍵:" & lt& ltendl

}

/*對兩個數排序,大的在前*/

無效訂單(Elemtype & ampin1,Elemtype & ampin2) {

elem type a =(in 1 & gt;in2?in 1:in2);

elem type b =(in 1 & lt;in2?in 1:in2);

in 1 = a;

in2 = b;

}

/*求最大公約數*/

元件類型gcd(元件類型a,元件類型b) {

順序(a,b);

int r;

if(b == 0) {

返回a;

}

否則{

while(true) {

r = a % b;

a = b;

b = r;

if (b == 0) {

返回a;

打破;

}

}

}

}

/*用擴展的歐幾裏德算法求乘法的逆運算*/

elem type extend _ Euclid(elem type m,Elemtype bin) {

順序(m,bin);

Elemtype a[3],b[3],t[3];

a[0] = 1,a[1] = 0,a[2]= m;

b[0] = 0,b[1] = 1,b[2]= bin;

if (b[2] == 0) {

return a[2] = gcd(m,bin);

}

if (b[2] ==1) {

return b[2] = gcd(m,bin);

}

while(true) {

if (b[2] ==1) {

return b[1];

打破;

}

int q = a[2]/b[2];

for(int I = 0;我& lt3;i++) {

t[I]= a[I]-q * b[I];

a[I]= b[I];

b[I]= t[I];

}

}

}

/*快速模冪算法*/

元素類型模乘(元素類型a,元素類型b,元素類型n) {

elem type f = 1;

電子信箱[32];

switch_to_bit(b,bin);

for(int I = 31;我& gt=0;我- ) {

f =(f * f)% n;

if(bin[i] == 1) {

f =(f * a)% n;

}

}

返回f;

}

/*生成密鑰*/

void produce_key() {

cout & lt& lt"輸入兩個素數p和q:";

CIN & gt;& gtp & gt& gtq;

而(!(test _ prime(p)amp;& amptest_prime(q))){

cout & lt& lt"輸入錯誤,請確認兩個數都是質數!"& lt& ltendl

cout & lt& lt"輸入兩個素數p和q:";

CIN & gt;& gtp & gt& gtq;

};

pr . n = p * q;

pu . n = p * q;

fn =(p-1)*(q-1);

cout & lt& lt" fn = " & lt& ltfn & lt& ltendl

cout & lt& lt”輸入e:“;

CIN & gt;& gte;

while((gcd(fn,e)!=1)) {

cout & lt& lt“e是錯誤,再試壹次!”;

cout & lt& lt”輸入e:“;

CIN & gt;& gte;

}

pr.d = (extend_euclid(fn,e)+fn)% fn;

pu.e = e

flag = 1;

cout & lt& lt" pr . d:" & lt;& ltpr.d & lt& lt" pr . n:" & lt;& lt公關& lt& ltendl

cout & lt& lt" pu . e:" & lt;& ltpu.e & lt& lt" pu . n:" & lt;& ltpu.n & lt& ltendl

}

/*加密*/

void encrypt() {

if(flag == 0) {

cout & lt& lt" setkey first:" & lt;& ltendl

produce _ key();

}

cout & lt& lt”輸入m:“;

CIN & gt;& gtm;

c =模_乘(m,pu.e,pu . n);

cout & lt& lt“c是:“& lt& ltc & lt& ltendl

}

/*解密*/

void解密(){

if(flag == 0) {

cout & lt& lt" setkey first:" & lt;& ltendl

produce _ key();

}

cout & lt& lt”輸入c:“;

CIN & gt;& gtc;

m =模乘(c,pr.d,pr . n);

cout & lt& lt“m是:“& lt& ltm & lt& ltendl

}

/*版權信息*/

void about() {

cout & lt& lt“* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * & lt;& ltendl

cout & lt& lt“* * * by Terry * * *”& lt;& ltendl

cout & lt& lt“***版權2010,版權所有* * *”& lt;& ltendl

cout & lt& lt“***特裏,佐為支持的技術!* * * " & lt& ltendl

cout & lt& lt“***如有任何問題,請發郵件* * *”& lt;& ltendl

cout & lt& lt“***去18679376@qq.com!* * * " & lt& ltendl

cout & lt& lt“***理工科的計算機* * *”& lt;& ltendl

cout & lt& lt“***西安電子科技大學2010-4-29 * * *”& lt;& ltendl

cout & lt& lt“* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * & lt;& ltendl

cout & lt& ltendl & lt& ltendl

init();

}

/*消息映射*/

MsgMap Messagemap[] = {

{'a ',大約},

{'s ',生產密鑰},

{'d ',解密},

{'e ',加密},

{'q ',NULL}

};

/*主函數,提供循環*/

void main() {

init();

char d;

while((d = getchar())!='q') {

int I = 0;

while(Messagemap[i].ch) {

if(Messagemap[i].ch == d) {

Messagemap[i]。handler();

打破;

}

i++;

}

}

}

//歡迎分享,偷竊可恥。

  • 上一篇:什麽免費論壇空間支持WAP?
  • 下一篇:什麽樣的糖尿病周圍神經病變可以出院?
  • copyright 2024編程學習大全網