C language to realize simple encryption and decryption of RSA

The simple encryption and decryption of
RSA in C language is not popular. It is available on the Internet. Now I use my experimental report to realize it: The
required data are:
two large prime numbers p,q;
n=p q;
t=(p -1)
(q-1);
public key e satisfies (e, t)=1;
private key d satisfies de==1 (mod n);
encryption formula c=m^e(mod n), c is ciphertext , M is plaintext;
pay attention to plaintext m<n when encrypting;

Here is another code block:

//判断两个数是不是互素。 
void gcd(int p,int q){
    
    
	int temp1,temp2;   //q=temp2*p+temp1 ;
	if(q<p){
    
    
		temp1=p;
		p=q;
		q=temp1;
	}
	temp1=q%p,temp2=q/p; 
	while(temp1!=0){
    
    
		
	    q=p;p=temp1;
		temp1=q%p;temp2=q/p;
		}
		if(temp1==0&&temp2==q){
    
    
			printf("符合条件!\n");
	    }
	    else{
    
    
	    	printf("不符合条件!\n");
		}
}
//求e关于模(p-1)(q-1)的逆元d:即私钥 
int extend(int e,int t){
    
    
	int d;
	for(d=0;d<t;d++){
    
    
		if(e*d%t==1)
	        return d;
	}
}

In the encryption function, the symbolic plaintext must be converted into digital plaintext to encrypt. The encryption principle is to encrypt the ascii codes corresponding to each letter in turn; the encryption formula pow(m,e)%n in the encryption function may cause overflow, which is After the solution:

//加密函数 
void encrypt(int e,int n){
    
           //自己指定指数e 

    //先将符号明文转换成字母所对应的ascii码。 
	char mingwen[100];    //符号明文 
	printf("请输入明文:\n");
	scanf("%s",mingwen);  //gets(mingwen);
	//gets(mingwen);
	changdu=strlen(mingwen);
	int ming[strlen(mingwen)];   //定义符号明文 
	for(int i=0;i<strlen(mingwen);i++){
    
    
	ming[i]=mingwen[i];        //将字母转换成对应的ascii码。 
	printf("%d",mingwen[i]);
	} 
	printf("\n"); 
	//开始加密 
	printf("加密开始…………………………\n");
	int zhuan=1;    //c为加密后的数字密文 
	for(int i=0;i<strlen(mingwen);i++){
    
    
	 
	   for(int j=0;j<e;j++){
    
    
		zhuan=zhuan*ming[i]%n;
		//zhuan=zhuan%n; 
	   }
	   c[i]=zhuan;
	   //printf("%d",mi[i]); 
	   zhuan=1;
	} 
	printf("加密密文为:\n");
	for(int i=0;i<strlen(mingwen);i++) 
	printf("%d",c[i]); 
	printf("\n加密结束…………………………\n");
	}

Solve the previous: (as long as the calculation of the large power exponent is calculated separately)

	//以下写法会导致溢出!
//	{
    
    
//		for(int i=0;i<strlen(mingwen);i++){
    
    
//		zhuan=pow()
//		mi[i]=int(pow(ming[i],e))%n;
//		printf("密文为:%d",mi[0]);
//	}
//	} 
	
	
//	printf("密文为:\n");
//	for(int i=0;i<strlen(mingwen);i++){
    
    
//		printf("%d",mi[i]);
//	}
//解密函数 
void decrypto(int d,int n){
    
    
	int de_mingwen[changdu],zhuan1=1;
	char de_ming[changdu];
	for(int i=0;i<changdu;i++){
    
    
	 
	   for(int j=0;j<d;j++){
    
    
		zhuan1=zhuan1*c[i]%n;
		//zhuan=zhuan%n; 
	   }
	   de_mingwen[i]=zhuan1;
	   //printf("%d",mi[i]); 
	   zhuan1=1;
	} 
	printf("解密开始…………………………\n");
	printf("解密后的数字明文为:\n");
	for(int i=0;i<changdu;i++) 
	printf("%d",de_mingwen[i]); 
	printf("\n");
	printf("解密后的符号明文为:\n");
	for(int i=0;i<changdu;i++){
    
    
		de_ming[i]=de_mingwen[i];
		printf("%c",de_ming[i]);
	}
	printf("\n解密结束…………………………\n");
}

Main function:

int main(){
    
    
	int q,p,e,d,n,t,x;
	printf("请输入p:",p);scanf("%d",&p);
	printf("请输入q:",q);scanf("%d",&q);
	n=q*p;
	t=(q-1)*(p-1); 
	gcd(p,q);
	printf("请输入一个指数e,使得(e,t)=1:");scanf("%d",&e);
	gcd(e,t);
	
	d=extend(e,t); 
	printf("密钥为:%d,一定保管好!",d);
	printf("\n\n~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n");
	encrypt(e,n);
	printf("\n请输入正确的密钥,密钥正确将解密上面的密文:");scanf("%d",&d);
	decrypto(d,n);
	printf("\n~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n");
	return 0;
}

Encryption function encrypt() cannot use gets() to input plaintext, because gets() will end when it encounters a carriage return, so that a single function test is no problem, but if the entire program is tested, it will end before the function is executed. Situation:
Insert picture description here
Replace with scanf("%s",mingwen), so it can be executed!
Insert picture description here
Remember to add the complete header file! Because I defined some global variables:

#include<stdio.h>
#include<string.h>
int changdu; 
int c[100];

Xiaobai definitely understands the series! Because I'm also a good cook, I will write in more detail to prevent me from not knowing what it is next time I read it hahaha.

This is how the whole experiment is. It may be a bit rush to write, the whole program still has some delays, and some functions need to be optimized, but the basic functions are realized!

Here is a question I would like to ask predecessors:
how to use pointers in a function to access the variables of another function? In addition to using global variables, I hope that the predecessors will give an example, such as accessing the encrypted plaintext c in the encryption function in the decryption function.
There is an additional problem: how does a function return an array?

Guess you like

Origin blog.csdn.net/weixin_44116874/article/details/109393329