Introduction to RSA algorithm and C implementation algorithm

Source: Internet
Author: User
Tags gcd

What is RSA:

RSA public Key cryptosystem. The so-called public key cryptosystem is to use different encryption keys and decryption keys, is a "cryptographic key derived from the known encryption key is not feasible in computing" cryptosystem.

In public key cryptography, the encryption key (that is, the public key) PK is public information, and the decryption key (that is, secret key) SK is required to be confidential. Both the cryptographic algorithm E and the decryption algorithm d are also public. Although the decryption key SK is determined by the public key PK, but can not calculate the SK based on PK.

RSA's Algorithm Description:
(1) Select a pair of different, large enough prime p,q.
(2) Calculate N=PQ.
(3) Calculate f (n) = (p-1) (q-1), while P, Q strictly confidential, not let anyone know.
(4) Find a number E with F (n) coprime, and 1<e<f (n).
(5) Calculate d, make de≡1 mod f (n). This formula can also be expressed as d≡e-1 mod f (n)

(6) Public key ku= (e,n), private key kr= (D,n).
(7) When encrypting, the plaintext is first transformed into an integer m of 0 to N-1. If the clear text is longer, it can be split into the appropriate group before swapping. Set ciphertext to C, then the encryption process is:.
(8) The decryption process is:.

#include <stdio.h>#include<math.h>#include<stdlib.h>intLength =0 ;BOOLJUDGEP (intPrime//Judging is not prime number{    intI=0; Long Doubletemp=0 ; if(Prime = =0|| Prime = =2)        return false; Else{Temp=sqrt ((float) prime);  for(i=2; i<=temp;i++)    {        if(prime%i = =0)                return false;//the input is not a prime number    }    return true;//number of inputs is prime    } }Long intINPUTP ()//until you enter a prime number{    intnum; scanf_s ("%d",&num);  while(!JUDGEP (num)) {printf ("The number you entered is not a prime number, please re-enter!\n"); scanf_s ("%d",&num); }    returnnum;}intgcdintAintb) {    returnd={0? A:GCD (b,a%b);}Long intChecke (Long intF_n)//determine if E meets the requirements 1<e <f_n and E and f_n{    Long inte; intK; printf ("Please enter e (1<e<f_n):"); scanf_s ("%d",&e);  while(E > F_n | | e<=1)//determine the scope of e{printf ("e is ' t fit, enter again! (1<e<f_n)"); scanf_s ("%d",&e); } k=gcd (f_n,e);  while(k! =1) {printf ("e with f_n relatively prime,enter again!"); scanf_s ("%d",&e); K=gcd (f_n,e); }    returne;}Long intCalculated (Long intF_n,Long  intE//Divide{    intI, j =1, T1, t2=1, k =0, M; intarry_1[ -] = {F_n,e}, arry_2[ -] ={0} ; printf ("The calculation:\n") ;  for(i=1; (Arry_1[i]! =0) && (arry_1[i]! =1); i + +) {Arry_1[i+1] =arry_1[i-1]%Arry_1[i]; arry_2 [i-1] = arry_1[i-1]/Arry_1[i]; printf ("%d =%d*%d+%d\n", arry_1[i-1],arry_1[i],arry_2[i-1],arry_1[i+1] ) ; } T1=1 ; T2=-arry_2[i-2] ;  while((I-2)!=k) {m=T1; T1=T2; if(j%2!=0) T2= (ABS (m) +arry_2[i-3]*ABS (T2)); ElseT2=-(ABS (m) +arry_2[i-3]*ABS (T2)); I-- ; J++ ; }         while(T2 <0) {T2= F_n +T2; } k=T2;  while(t2 = t2/f_n) {k= t2%f_n; } printf ("k =%d\n", K); returnK;}int* Dec2bin (intE//Convert to binary {  inti =0 ; intstr1[ -], str2[ -] ;  while(e) {str1[i]= e%2 ; E= e/2 ; I++ ; } I--; printf ("The binary is:");  for(length =0; i>=0; Length + +) {Str2[length]=Str1[i]; printf ("%d", Str2[length]); I-- ; } returnstr2; } intEncrypt (intMintEintN//squared multiplication {     intc =1, I; int*p =Dec2bin (e);  for(i =0; I < Length;i + +) {C= c%N; C=c*c%N; if(* (p+i) = =1) {C=c%N; C=c*m%N; }     }     returnC;}intMainvoid){    Long intP,q, N,f_n, E, d,m,c,l; printf ("Please enter P:");//p,q Inputp =INPUTP (); printf ("Please enter Q"); Q=INPUTP (); N= P*q;//Calculate N,f_nF_n = (P-1) * (q1); E= Checke (f_n);//Calculation eD=calculated (f_n,e);//Calculate Dprintf ("Please enter M"); scanf_s ("%d",&m); C=Encrypt (m,e,n); printf ("C is%d\n", c); L=Encrypt (c,d,n); printf ("L is%d\n", L); system ("Pause");return 0 ;}

Introduction to RSA algorithm and C implementation algorithm

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.