RSA. h file:
//! RSA dynamic link library implements hfile /*! @ Author Zhu mengbin @ E-mail zmb.tsubasa@gmail.com @ version 1.0 @ date 2011-03 @ {*/# ifndef rsa_h # define rsa_h # include <stdio. h> # include <string> # include <stdlib. h> # include <time. h> # include <math. h> # include <malloc. h> # include <iostream> # include <ppl. h> using namespace STD; using namespace concurrency ;//! Max is the maximum number of arrays, And Len is the memory space occupied by the structure slink */# define max 100 # define Len sizeof (struct slink )//! # The RSA class that can be compiled by the dynamic link library /*! @ See class _ declspec (dllexport) RSA writes the RSA algorithm into a dynamic link library for convenient calling, with the public key, private Key and plaintext encryption and decryption */class _ declspec (dllexport) RSA {public ://! # The newly defined data structure slink /*! @ See struct in the slink data structure, bignum stores the randomly generated large numbers, next is the pointer to the end @ See Int bignum [Max] */typedef struct slink {int bignum [Max];/*! <Bignum [98] is used to mark positive and negative numbers, 1 positive, 0 negative bignum [99] to mark the actual length */struct slink * Next;} slink; public ://! # RSA constructor /*! @ See RSA () some variables in the RSA class should be initialized accordingly */RSA ();//! # RSA destructor /*! @ See ~ RSA () releases memory */~ RSA (); Public ://! # RSA big data computing function library/** @ big data comparison function @ See Int CMP (INT, INT) */int cmp (INT A1 [Max], int A2 [Max]); /** @ @ see void mov (int A [Max], int * B); */void mov (int A [Max], int * B ); /** @ big number product function @ see void MUL (INT A1 [Max], int A2 [Max], int * C); */void MUL (INT A1 [Max], int A2 [Max], int * C);/** @ @ see void add (INT A1 [Max], int A2 [Max], int * C ); */void add (INT A1 [Max], int A2 [Max], int * C);/** @ @ see void sub (INT A1 [Max], int A2 [Max], int * C); */void sub (INT A1 [Max], int A2 [Max], int * C );/*! @ Big number modulo function @ see void Mod (int A [Max], int B [Max], int * C); @ attention/C = a mod B // note: it is tested that the arrays of A and C have changed. */Void Mod (int A [Max], int B [Max], int * C );/*! @ Big Data Division function @ see void divt (int t [Max], int B [Max], int * C, int * w ); @ attention // trial commercial law // after the call, W is a mod B, and C is a div B; */void divt (int t [Max], int B [Max], int * C, int * w );/*! @ Solved M = a * B mod n ;/*! @ See void mulmod (int A [Max], int B [Max], int N [Max], int * m); */void mulmod (int A [Max], int B [Max], int N [Max], int * m );/*! @ Solve the function problem of M = a ^ P mod n /*! @ See void expmod (int A [Max], int P [Max], int N [Max], int * m); */void expmod (int A [Max], int P [Max], int N [Max], int * m );/*! @ Determine if it is a prime number @ See Int is_prime_san (int p [Max]); */INT is_prime_san (int p [Max]);/*! @ Determine whether two large numbers are mutually dependent @ See Int Coprime (int e [Max], int s [Max]); */INT Coprime (int e [Max], int s [Max]);/*! @ Randomly generate prime number @ see void prime_random (int * P, int * q); */void prime_random (int * P, int * q );/*! @ Generate prime E @ see void erand (int e [Max], int M [Max]); */void erand (int e [Max], int M [Max]); /*! @ Generate other numbers based on the Rule @ see void rsad (int e [Max], int G [Max], int * D); */void rsad (int e [Max], int G [Max], int * D );/*! @ Function for decryption key D (based on Euclid algorithm) @ see unsigned long RSA (unsigned long P, unsigned Long Q, unsigned long e ); */unsigned long RSA (unsigned long P, unsigned Long Q, unsigned long e );//! # RSA functions that generate large numbers of public keys and private keys /*! @ See bool rsakey (); @ Param does not have any input. @ Param randomly generates functions E, D, and N, which have a long running time, you need to wait for @ return [bool] To return true if it succeeds, and return false if it fails */bool rsakey ();//! # RSA file encryption function /*! @ See string tencrypto (int e [Max], int N [Max], char * text); @ Param [int [] e, n is a random public key, use the public key to encrypt @ Param [char * text as the plain text. The plaintext is stored in the format of char * @ return [String] and the returned value is the ciphertext after the encryption is successful, use the string type for storage */string tencrypto (int e [Max], int N [Max], char * text );//! # RSA file decryption function /*! @ See string tdecrypto (INT d [Max], int N [Max], string text); @ Param [int [] d, n is the private key, and the rsakey () function () generate @ Param [String text as ciphertext, corresponding to the encryption function, the ciphertext format is string @ return [String]. The decrypted plaintext is stored as string */string tdecrypto (INT d [Max], int N [Max], string text ); public:/** @ global variables defined by brief, storage key */int p [Max], Q [Max], n [Max], d [Max], E [Max], M [Max], P1 [Max], Q1 [Max]; private: int I; char C; // struct slink * head, * H1, * H2 ;}; # endif // rsa_h
RSA. c file:
/*! * @ RSA dynamic link library CPP file * @ author Zhu mengbin * @ E-mail zmb.tsubasa@gmail.com * @ version 1.0 * @ date 2011-03 * @ {*/# include "RSA. H "RSA: RSA () {} RSA ::~ RSA () {}/ * ---------------------------- create your own big data computing database ------------------------------- */int rsa: CMP (INT A1 [Max], int A2 [Max]) {int L1, l2; int I; L1 = A1 [99]; L2 = a2 [99]; If (L1> l2) return 1; if (L1 <l2) Return-1; for (I = (l1-1); I> = 0; I --) {If (A1 [I]> A2 [I]) return 1; if (A1 [I] <A2 [I]) Return-1;} return 0;} void RSA: mov (int A [Max], int * B) {Int J; For (j = 0; j <Max; j ++) B [J] = A [J]; return;} void RSA :: mul (INT A1 [Max], int A2 [Max], int * C) {int I, j; int y; int X; int Z; int W; int L1, L2; L1 = A1 [MAX-1]; L2 = a2 [MAX-1]; if (A1 [MAX-2] = '-' & A2 [MAX-2] = '-') C [MAX-2] = 0; else if (A1 [MAX-2] = '-') C [MAX-2] = '-'; else if (A2 [MAX-2] = '-') c [MAX-2] = '-'; for (I = 0; I <L1; I ++) {for (j = 0; j <L2; j ++) {x = A1 [I] * A2 [J]; y = X/10; Z = x % 10; W = I + J; c [w] = C [w] + z; C [W + 1] = C [W + 1] + Y + C [w]/10; c [w] = C [w] % 10;} W = l1 + L2; If (C [W-1] = 0) W = W-1; c [MAX-1] = W; return;} void RSA: add (INT A1 [Max], int A2 [Max], I Nt * c) {int I, L1, L2; int Len, temp [Max]; int K = 0; L1 = A1 [MAX-1]; L2 = a2 [MAX-1]; if (A1 [MAX-2] = '-') & (A2 [MAX-2] = '-') {C [MAX-2] = '-';} else if (A1 [MAX-2] = '-') {mov (A1, temp); temp [MAX-2] = 0; sub (A2, temp, c); return ;} else if (A2 [MAX-2] = '-') {mov (A2, temp); temp [98] = 0; sub (A1, temp, c); return ;} if (L1 <l2) Len = L1; else Len = L2; for (I = 0; I <Len; I ++) {c [I] = (A1 [I] + A2 [I] + k) % 10; k = (A1 [I] + A2 [I] + k) /10;} If (L1> Len) {for (I = Len; I <L1; I ++) {C [I] = (A1 [I] + k) % 10; k = (A1 [I] + k)/10;} If (K! = 0) {C [L1] = K; Len = l1 + 1;} else Len = L1;} else {for (I = Len; I <L2; I ++) {c [I] = (A2 [I] + k) % 10; k = (A2 [I] + k)/10;} If (K! = 0) {C [L2] = K; Len = l2 + 1;} else Len = L2;} C [99] = Len; return;} void RSA :: sub (INT A1 [Max], int A2 [Max], int * c) {int I, L1, L2; int Len, T1 [Max], T2 [Max]; int K = 0; L1 = A1 [MAX-1]; L2 = a2 [MAX-1]; If (A1 [MAX-2] = '-') & (A2 [MAX-2] = '-') {mov (A1, T1); MoV (A2, T2); T1 [MAX-2] = 0; t2 [MAX-2] = 0; sub (t2, T1, c); return;} else if (A2 [MAX-2] = '-') {mov (A2, T2 ); t2 [MAX-2] = 0; add (A1, T2, c); return;} else if (A1 [MAX-2] = '-') {mov (A2, T2 ); t2 [MAX-2] = '-'; add (A1, T2, c); return;} If (CMP (a1, a2) = 1) {Len = L2; for (I = 0; I <Len; I ++) {If (A1 [I]-k-a2 [I]) <0) {c [I] = (A1 [I]-A2 [I]-K + 10) % 10; k = 1 ;} else {C [I] = (A1 [I]-A2 [I]-k) % 10; k = 0 ;}} for (I = Len; I <L1; I ++) {If (A1 [I]-k) <0) {C [I] = (A1 [I]-K + 10) % 10; k = 1;} else {C [I] = (A1 [I]-k) % 10; k = 0;} If (C [l1-1] = 0) /* makes the front of array C so 0 characters are not displayed, such as 1000-20 = 0980 ---> display as 980 */{Len = l1-1; I = 2; while (C [l1-i] = 0)/* 111456-111450 = 00006, after removing 0 becomes 6; */{Len = l1-i; I ++ ;}} else {Len = L 1 ;}} elseif (CMP (a1, a2) = (-1) {C [MAX-2] = '-'; Len = L1; for (I = 0; I <Len; I ++) {If (A2 [I]-k-a1 [I]) <0) {c [I] = (A2 [I]-A1 [I]-K + 10) % 10; k = 1 ;} else {C [I] = (A2 [I]-A1 [I]-k) % 10; k = 0 ;}} for (I = Len; I <L2; I ++) {If (A2 [I]-k) <0) {C [I] = (A2 [I]-K + 10) % 10; k = 1;} else {C [I] = (A2 [I]-k) % 10; k = 0;} If (C [l2-1] = 0) {Len = l2-1; I = 2; while (C [l1-i] = 0) {Len = l1-i; I ++ ;}} else Len = L2 ;} else if (CMP (a1, a2) = 0) {Len = 1; C [len-1] = 0;} C [MAX-1] = Len; return;} void RSA:: Mod (int A [Max], int B [Max], int * C)/*/C = a mod B // note: it is tested that the arrays of A and C have changed. */{Int d [Max]; MoV (a, d); While (CMP (D, B )! = (-1)/*/C = a-B ....... until (C <B) */{sub (D, B, c); MoV (c, d);/*/C copy to a */} return ;} void RSA: divt (int t [Max], int B [Max], int * C, int * W) /* // trial commercial law // after the call, W is a mod B, and C is a div B; */{int A1, B1, I, j, M; /* W is used to temporarily save data */INT d [Max], E [Max], F [Max], G [Max], a [Max]; MoV (t, a); for (I = 0; I <Max; I ++) E [I] = 0; for (I = 0; I <Max; I ++) d [I] = 0; for (I = 0; I <Max; I ++) g [I] = 0; a1 = A [MAX-1]; b1 = B [MAX-1]; If (CMP (a, B) = (-1) {C [0] = 0; C [MAX-1] = 1; moV (T, W); Return ;} Else if (CMP (a, B) = 0) {C [0] = 1; C [MAX-1] = 1; W [0] = 0; W [MAX-1] = 1; return;} M = (a1-b1); for (I = m; I> = 0; I --) /* 341245/3 = 341245-300000*1 ---> 41245-30000*1 ---> 11245-3000*3 ---> 2245-300*7 ---> 145-30*4 = 25 ---> 25-3*8 = 1 */{for (j = 0; j <Max; j ++) d [J] = 0; d [I] = 1; d [MAX-1] = I + 1; MoV (B, G ); mul (G, d, e); While (CMP (a, e )! = (-1) {C [I] ++; sub (A, E, F); MoV (F, ); /* f copy to G */} For (j = I; j <Max; j ++)/* High-position clearing */E [J] = 0 ;} moV (A, W); If (C [m] = 0) C [MAX-1] = m; else C [MAX-1] = m + 1; return;} void RSA:: mulmod (int A [Max], int B [Max], int N [Max], int * m)/* solves M = a * B mod N; */{int C [Max], d [Max]; int I; for (I = 0; I <Max; I ++) d [I] = C [I] = 0; MUL (A, B, C); divt (C, N, D, m); // for (I = 0; I <m [MAX-1]; I ++) // printf ("% d", M [M [MAX-1]-i-1]); // printf ("\ nm length is: % d \ n", M [MAX-1]);}/*- ------------ The next task is to solve the function problem of M = a ^ P mod n ------------ */void RSA: expmod (int A [Max], int P [Max], int N [Max], int * m) {int T [Max], L [Max], temp [Max];/*/t put 2, l put 1; */int w [Max], s [Max], C [Max], B [Max], I; for (I = 0; I <MAX-1; I ++) B [I] = L [I] = T [I] = W [I] = 0; t [0] = 2; t [MAX-1] = 1; L [0] = 1; L [MAX-1] = 1; MoV (L, temp); MoV (a, m); MoV (p, B ); while (CMP (B, L )! = 0) {for (I = 0; I <Max; I ++) W [I] = C [I] = 0; divt (B, T, W, c);/* // C = P mod 2 W = p/2 */mov (W, B ); /* // P = p/2 */If (CMP (C, L) = 0)/*/remainder c = 1 */{for (I = 0; I <Max; I ++) W [I] = 0; MUL (temp, M, W); MoV (W, temp); for (I = 0; I <Max; I ++) W [I] = C [I] = 0; divt (temp, N, W, C ); /*/C is the remainder c = TEMP % N, W is the quotient W = temp/N */mov (C, temp);} for (I = 0; I <Max; I ++) s [I] = 0; MUL (M, M, S); // s = A * afor (I = 0; I <Max; I ++) c [I] = 0; divt (S, N, W, c);/*/W = s/n; C = s mod N */mov (C, m) ;}for (I = 0; I <Max; I ++) s [I] = 0; Mul (M, temp, S); for (I = 0; I <Max; I ++) C [I] = 0; divt (S, N, W, c); MoV (C, M);/* remainder s to M */M [MAX-2] = A [MAX-2];/* for the subsequent Chinese character display needs, use the 99th bits as the mark */return;/*/k = temp * K % N; */} int RSA: is_prime_san (int p [Max]) {int I, A [Max], t [Max], s [Max], O [Max]; for (I = 0; I <Max; I ++) s [I] = O [I] = A [I] = T [I] = 0; t [0] = 1; t [MAX-1] = 1; A [0] = 2; // {2, 3, 5, 7} A [MAX-1] = 1; sub (P, T, S); expmod (A, S, P, o); If (CMP (O, T )! = 0) {return 0;} A [0] = 3; for (I = 0; I <Max; I ++) O [I] = 0; expmod (, s, P, O); If (CMP (O, T )! = 0) {return 0;} A [0] = 5; for (I = 0; I <Max; I ++) O [I] = 0; expmod (, s, P, O); If (CMP (O, T )! = 0) {return 0;} A [0] = 7; for (I = 0; I <Max; I ++) O [I] = 0; expmod (, s, P, O); If (CMP (O, T )! = 0) {return 0;} return 1;} int RSA: Coprime (int e [Max], int s [Max]) /* /// determine whether the two large numbers are mutually qualitative. // */{int A [Max], B [Max], C [Max], d [Max], O [Max], L [Max]; int I; for (I = 0; I <Max; I ++) L [I] = O [I] = C [I] = d [I] = 0; O [0] = 0; O [MAX-1] = 1; L [0] = 1; L [MAX-1] = 1; MoV (E, B); MoV (s, A); do {If (CMP (B, L) = 0) {return 1 ;}for (I = 0; I <Max; I ++) C [I] = 0; divt (A, B, D, c); MoV (B, A);/* B ---> A */mov (C, B);/* C ---> B */} while (CMP (C, o )! = 0);/* printf ("ihey are not Coprime! \ N "); */return 0;} void RSA: prime_random (int * P, int * q) {int I, K; time_t; P [0] = 1; Q [0] = 3; p [MAX-1] = 10; Q [MAX-1] = 11; do {T = Time (null ); srand (unsigned long) T); for (I = 1; I <p [MAX-1]-1; I ++) {k = rand () % 10; P [I] = K;} k = rand () % 10; while (k = 0) {k = rand () % 10 ;} P [p [MAX-1]-1] = K;} while (is_prime_san (p ))! = 1); do {T = Time (null); srand (unsigned long) T); for (I = 1; I <q [MAX-1]; I ++) {k = rand () % 10; Q [I] = K ;}} while (is_prime_san (q ))! = 1); return;} void RSA: erand (int e [Max], int M [Max]) {int I, K; time_t T; E [MAX-1] = 5; do {T = Time (null); srand (unsigned long) T); for (I = 0; I <E [MAX-1]-1; I ++) {k = rand () % 10; E [I] = K;} while (k = rand () % 10) = 0) k = rand () % 10; E [E [MAX-1]-1] = K;} while (Coprime (E, m )! = 1); return;} void RSA: rsad (int e [Max], int G [Max], int * D) {int R [Max], n1 [Max], N2 [Max], K [Max], W [Max]; int I, T [Max], B1 [Max], B2 [Max], temp [Max]; MoV (G, N1); MoV (E, N2); for (I = 0; I <Max; I ++) K [I] = W [I] = R [I] = temp [I] = b1 [I] = b2 [I] = T [I] = 0; b1 [MAX-1] = 0; B1 [0] = 0;/*/b1 = 0; */B2 [MAX-1] = 1; B2 [0] = 1; /*/b2 = 1; */while (1) {for (I = 0; I <Max; I ++) K [I] = W [I] = 0; divt (N1, N2, K, W);/*/k = N1/N2; */for (I = 0; I <Max; I ++) temp [I] = 0; MUL (K, N2, temp);/*/temp = K * N2; */For (I = 0; I <Max; I ++) R [I] = 0; sub (N1, temp, R ); if (R [MAX-1] = 1) & (R [0] = 0)/*/r = 0 */{break ;} else {mov (N2, N1);/*/n1 = n2; */mov (R, N2);/*/N2 = r; */mov (B2, t);/*/T = B2; */for (I = 0; I <Max; I ++) temp [I] = 0; MUL (K, B2, temp);/*/b2 = b1-k * B2; */for (I = 0; I <Max; I ++) B2 [I] = 0; sub (B1, temp, B2); MoV (T, B1) ;}} for (I = 0; I <Max; I ++) T [I] = 0; add (B2, g, T); for (I = 0; I <Max; I ++) temp [I] = d [I] = 0; divt (T, G, temp, d);}/*/function for decryption key D (based on Euclid algorithm) 96403770511368768 000 */unsigned long RSA: RSA (unsigned long P, unsigned Long Q, unsigned long e)/*/function for decryption key D (based on Euclid algorithm) */{unsigned long g, K, R, N1, N2, T; unsigned long b1 = 0, b2 = 1; G = (p-1) * (q-1 ); n1 = g; N2 = E; while (1) {k = N1/N2; r = n1-k * N2; If (R! = 0) {n1 = n2; N2 = r; t = B2; b2 = b1-k * B2; b1 = T;} else {break;} return (G + b2) % G ;} //////////////////////////////////////// //////////////////////////////////////// //////////////////////////////////////// //////////////////////////////////////// ////////////////////! The file encryption function of RSA /*! * @ Param [in] E. N is a random public key and is encrypted using the public key. * @ Param [in] text is a plain text, plaintext is stored in char * format * @ return [String]. The returned value is the ciphertext after the encryption is successful, the data type is converted to * @ see E, D, N & text */string RSA :: tencrypto (int e [Max], int N [Max], char * Text)/* // encrypt files in need */{int I, K, count, temp, C; char ch; struct slink * P, * P1, * P2; struct slink * h; H = P = p1 = P2 = (struct slink *) malloc (LEN); H = NULL; If (text = NULL) {return NULL;} COUNT = 0; Int J; For (j = 0; j <strlen (text); j ++) {CH = text [J]; C = CH; k = 0; If (C <0) {c = ABS (c);/*/positive the negative number and make a mark */P1-> bignum [MAX-2] = '0 ';} else {p1-> bignum [MAX-2] = '1';} while (C/10! = 0) {temp = C % 10; C = C/10; P1-> bignum [k] = temp; k ++ ;} p1-> bignum [k] = C; P1-> bignum [MAX-1] = k + 1; Count = count + 1; if (COUNT = 1) h = p1; else P2-> next = p1; P2 = p1; P1 = (struct slink *) malloc (LEN);} P2-> next = NULL; string res; P = p1 = (struct slink *) malloc (LEN); P = H; If (H! = NULL) do {expmod (p-> bignum, E, N, P1-> bignum); CH = p1-> bignum [MAX-2]; Res + = CH; if (P1-> bignum [MAX-1]/10) = 0)/*/judge whether P1-> bignum [99] is greater than 10; */{CH = 0 + 48; Res + = CH; CH = p1-> bignum [MAX-1] + 48; Res + = CH ;} else {CH = p1-> bignum [MAX-1]/10 + 48; Res + = CH; CH = p1-> bignum [MAX-1] % 10 + 48; res + = CH;} for (I = 0; I <P1-> bignum [MAX-1]; I ++) {CH = p1-> bignum [I] + 48; res + = CH;} p = p-> next; P1 = (struct slink *) malloc (LEN);} while (P! = NULL); Return res ;}//! The file decryption function of RSA /*! * @ Param [in] d, n is the private key, generated by the function rsakey () * @ Param [in] text is the ciphertext, corresponding to the encryption function, the ciphertext format is string * @ return [String]. The decrypted plaintext uses string for storage * @ pre \ e. The data type is converted during decryption. * @ see E, d, n & text */string RSA: tdecrypto (INT d [Max], int N [Max], string text) {struct slink * H, * P1, * P2; char ch; int I, J, K, C, Count, temp; I = 0; j = 3; Count = 0; H = p1 = P2 = (struct slink *) malloc (LEN); int KK; For (KK = 0; KK <text. length (); KK ++) {CH = text. at (kk); C = CH; If (j = 3) {p1-> bignum [MAX-2] = C; j --;} else if (j = 2) {temp = c-48; j --;} else if (j = 1) {p1-> bignum [MAX-1] = temp * 10 + c-48; j --;} else if (j = 0) {p1-> bignum [I] = c-48; I ++; if (I = p1-> bignum [MAX-1]) {I = 0; j = 3; count ++; If (COUNT = 1) h = p1; else P2-> next = p1; P2 = p1; P1 = (struct slink *) malloc (LEN) ;}}p2-> next = NULL; P2 = (struct slink *) malloc (LEN); P1 = H; k = 0; string res; if (H! = NULL)/*/temp is the int value of the temporary asiic Code */do {for (I = 0; I <Max; I ++) p2-> bignum [I] = 0; expmod (P1-> bignum, d, n, P2-> bignum ); temp = P2-> bignum [0] + P2-> bignum [1] * 10 + P2-> bignum [2] * 100; if (P2-> bignum [MAX-2]) = '0') {temp = 0-temp;}/*/convert to the correct asiic code, for example, a Chinese character */CH = temp is formed in-78-96;/* STR [k] ---> CH */RES + = CH; k ++; P1 = p1-> next; p2 = (struct slink *) malloc (LEN);} while (P1! = NULL); Return res ;} //////////////////////////////////////// //////////////////////////////////////// //////////////////////////////////////// //////////////////////////////////////// /// // ** generate the key, the public key and private key * @ Param [in] randomly generate a large number of keys * @ Param [in] calculate using custom large number calculation rules * @ return returns whether the generation succeeds *-false indicates failed to generate key * @ pre \ e the key generated is stored in the defined class variable * @ see E, d, n */bool RSA: rsakey () {for (I = 0; I <Max; I ++) M [I] = P [I] = Q [I] = N [I] = d [I] = E [I] = 0; /*/initialize it briefly */prime_random (p, q);/*/randomly generate two large prime numbers */MUL (p, q, n); MoV (p, q, p1); P1 [0] --; MoV (Q, Q1); Q1 [0] --;/*/q-1; */MUL (P1, Q1, M ); // M = (p-1) * (q-1) erand (E, m); rsad (E, M, d); Return true ;} //////////////////////////