RSA algorithm is a public key encryption algorithm, RSA algorithm compared to other algorithms are very clear, but the difficulty is very difficult to crack. The RSA algorithm is based on a very simple theory of numbers fact: it is easy to multiply two primes to get a large number, but it is very difficult to multiply a large number into two primes. This algorithm was first unveiled in 1978 and is the first algorithm to be used for both data encryption and digital signatures, and is easy to understand. As early as 1973, the cipher learned a similar algorithm, but has been classified as a top secret until the 1998 was officially released.
The RSA algorithm is an asymmetric algorithm that requires a pair of keys to be decrypted by using one of the other encryption methods. First, let's Meet the Euler function:
The general formula of Euler's function is as follows:φ (x) =x (1-1/P1) (1-1/P2) (1-1/P3) (1-1/P4) ... ( 1-1/PN), the calculated value is less than the number of natural numbers in X, there is a strict proof on the number of correctness theory.
In the RSA algorithm our initial two prime numbers are p and Q, and the Euler function parameter x is p*q, then the Euler function can be deformed as:φ (p*q) = (p-1) * (q-1)
Then we need the following two steps to calculate a pair of keys that can be used for encryption and decryption:
(1) Find any number d, make D and P*q coprime, namely gcd (D,P*Q) =1 (greatest common divisor of greatest common divisor)
(2) Calculate an E to meet gcd (D*E,P*Q) =1
For example, if p=3,q=7 is the case, then assuming that d=5 satisfies gcd (21,5) =1, then the corresponding e should satisfy gcd (5*e,21) =1, e= 17, so we get a pair of public key and key d,e.
Assuming that you want to encrypt data 3, then there is (3) ^d mod p*q=243mod21=12 So the data we get after encryption is
For the decryption process, we have encrypted data (^e mod p*q=12^17mod21= (12^5) mod21 * (12^5) mod * (12^5) Mod21 * (a) ^2mod21= ((7^3) mod21) * 3mod21=3
The process of RSA encryption and decryption algorithm is simple some worrying about his security, but gave us a definite answer, with the increase of RSA key length, the age of secrecy will accelerate growth, in today's modern times if quantum computer brute force is not considered, the encryption security of RSA algorithm can be guaranteed, Unless the math community finds new ways to quickly figure out a large number (which can be very large), it is divided into two prime numbers.
With the above foundation, we can group long data to complete the encryption process:
1#include <stdio.h>2#include <stdlib.h>3 4 5 #defineKey_len_limit 10246 7 #defineTRUE 18 #defineFALSE 09 Ten unsigned prime1,prime2; One Charbuf[key_len_limit]={}; Aunsigned pub,sec=2; - - the voidSwap (unsigned*l,unsigned*R) { -unsigned tmp=*l; -*l=*R; -*r=tmp; + } - + unsigned gcd (unsigned a,unsigned b) { A if(a<b) Swap (&a,&b); at if(b==0)returnA; - Else returnGCD (b,a%b); - } - - - voidEncryptChar*p,unsigned key,unsigned MoD) { in Chartmp=*p; - for(Unsigned i=2; i<=key;i++){ totmp= (tmp*tmp)%MoD; + } -*p=tmp; the } * $ voidDecryptChar*p,unsigned key,unsigned MoD) {Panax Notoginseng Chartmp=*p; - for(Unsigned i=2; i<=key;i++){ thetmp= (tmp*tmp)%MoD; + } A*p=tmp; the } + - intMainintargcChar*argv[]) { $printf"Input Your Primary letter/number 1st to Generate Key:"); $scanf"%u",&prime1); -printf"Input Your Primary letter/number 2nd to Generate Key:"); -scanf"%u",&prime2); theunsigned tmp1= (unsigned) prime1,tmp2=(unsigned) prime2; -Unsigned mul= (tmp1-1) * (tmp2-1);Wuyiprintf"Input Your 1st public Key:"); thescanf"%u",&pub); - while(GCD (pub,mul)! =1){ Wuprintf"Input Your 1st public Key: (illegal)"); -scanf"%u",&pub); About } $printf"Now the Secret Key is:"); - while((PUB*SEC)%mul)! =1) sec++; -printf"%u\n", sec); -printf"Finally Input Your Plain Text to Encrypt:"); Ascanf"%s", buf); +printf"The Encrypted Text is:"); the for(intI=0; i<key_len_limit;i++){ - if(buf[i]==0) Break; $ Else{ theEncrypt (&Buf[i],pub,mul); theprintf"%u", Buf[i]); the } the}putchar ('\ n'); -printf"The decrypted Text is:"); in for(intI=0; i<key_len_limit;i++){ the if(buf[i]==0) Break; the Else{ AboutDecrypt (&Buf[i],sec,mul); theprintf"%c", Buf[i]); the } the}putchar ('\ n'); + return 0; -}
< Introduction to Cryptography > Cryptographic decryption and code implementation for RSA algorithms