Application -RSA encryption algorithm Euler function

If p and q are relatively prime, so that n = p * q is ola (n) = (p-1) * (q-1)

We know the value of p and q can easily know (p-1) * (q-1) value is the value ola function, but just know that n is the number, but it is very rare to p and q is the number, because when n it is large, such as when there are hundreds, it will have a lot of quality factor, to brute force for a long time. So This ensures the reliability of the RSA encryption algorithm.

RSA encryption is asymmetric encryption, a public key and a private key.

The formula is:

(Plaintext) ^ e% n = ciphertext where, e is one less than n and prime with the number n. 

(Ciphertext) ^ d% n = plaintext d = (1 + k * ola (n)) / e (where, k and d must be a positive integer.)                                       

E th plaintext to obtain the ciphertext n I taken.

D-th ciphertext to plaintext to obtain n I taken.

So after encryption using the public key (e, n) decrypting the private key (d, n) you construct a key pair, the public key to others, they are public key encrypted sent to you, you are using the public decryption key, to complete the plaintext encryption transmission.

Therefore, we first constructed two primes p and q is n = p * q ola (n) = (p-1) * (q-1)

Then a random configuration with less than n and prime to the number n e.

After obtaining en ola (n) to the left d

d = (1 + k * ola (n)) / e and d as k is a positive integer so we must start from the enumeration k 1, d is calculated until a positive integer.

Or change the equation d * e - 1 solved by the extended Euclidean d k * ola (n) =.

D can be obtained after the decryption of the ciphertext.

#include <iostream>
#include <stdio.h>
#include <cstring>
#include <stdlib.h>
#include <ctype.h>
#include <math.h>
typedef long long ll;
using namespace std;  
int e, d, n;
int gcd(int a, int b)
{
	if(b==0) return a;    // 当b等于0时, 结束递归。 
	else return gcd(b,a%b);  // 递归调用gcd(b, a%b) 
}

int exgcd(int a, int b, int& x, int& y){ // 扩展欧几里得 
	if(b==0){
		x = 1;
		y = 0;
		return a;
	}
	exgcd(b,a%b,y,x);
	y = y - a/b*x;
}
int PrimarityTest(int a, int i)  // 判断是不是质数 
{
	for(int j = a; j <= sqrt(i); j++){
		if(i%j==0)
			return 0;  //不是质数 
	}
	return 1;
}

int ModularExponention(int a, int m, int n) // 加密或者解密    
{
	int cipher = 1;         
	//快速幂 
	while(m){
		if(m&1){
			cipher = cipher * a % n;
		}
		m>>=1;
		a = a * a % n;
	} 
	return cipher%n;  
}

int ModularInverse(int a, int b)   //利用扩展欧几里得算法求解 d 
{     
	// d = (k*ola(n) + 1) / e
	// e*d - k*ola(n) = 1
	// 即 a*x - b*y = 1    贝祖等式 
	int x;
//	exgcd(b, a, x, y);
//	int t = a/gcd(a,b);
//	x = (x%t + t) % t;  //求最小整数解 
	for(int i = 1; ; i++){
		if((i*a + 1)%b==0){
			x = (i*a+1)/b;
			break;
		} 
	} 
	return x;
}


void KeyGeneration()  // 随机生成密钥 
{
	int p, q;
	int phi_n;

	do {
		do
			p = rand();
		while (p % 2 == 0);

	} while (!PrimarityTest(2, p));

	do {
		do
			q = rand();
		while (q % 2 == 0);
	} while (!PrimarityTest(2, q));

	n = p * q;  //两个质数     公钥  e n   私钥  d n    加密  明文^e mod n = 密文   
	phi_n = (p - 1) * (q - 1);     // n的欧拉函数值     解密  密文^d mod n = 明文 
	cout<<"n:  "<<n<<"   ola(n):    "<<phi_n<<endl; 
	do
		e = rand() % (phi_n - 2) + 2; // 1 < e < phi_n
	while (gcd(e, phi_n) != 1);  
	cout<<"e:   "<<e<<endl;  
	d = ModularInverse(phi_n, e);  //计算私钥中 d 的值   d = (k*ola(n)+1) / e 
	cout<<"d:   "<<d<<endl;
}

void Encryption(int value, FILE* out)  //加密函数 
{
	int cipher;
	cipher = ModularExponention(value, e, n);
	fprintf(out, "%d ", cipher);   //将密文写入out文件中。 每个密文以空格隔开。 
}

void Decryption(int value, FILE* out)  //解密函数 
{
	int decipher;
	decipher = ModularExponention(value, d, n);
	fprintf(out, "%c", decipher);
}

int main(void)
{
	FILE* inp, * out;
	char filepath[15], filename[100];
	KeyGeneration();  //生成密钥 
	inp = fopen("plain.txt", "r+"); //读写 
	if (inp == NULL) { //改文件不存在 
		printf("Error opening Source File.\n");
		exit(1);
	}

	out = fopen("cipher.txt", "w+");//写读 不存在创建 
	if (out == NULL) {
		printf("Error opening Destination File.\n");
		exit(1);
	}

	// encryption starts    
	while (1) {      // 加密过程,将密文输出到out文件中。 
		char ch = getc(inp);
		if (ch == -1)//到达结尾 
			break;
		int value = toascii(ch);  //得到ch的ASCII码 
		Encryption(value, out);  //调用加密函数 
	}

	fclose(inp);
	fclose(out);

	// decryption starts
	inp = fopen("cipher.txt", "r");  //读入密文 
	if (inp == NULL) {
		printf("Error opening Cipher Text.\n");
		exit(1);
	}

	out = fopen("decipher.txt", "w+");     //解密后存入decipher.txt 
	if (out == NULL) {
		printf("Error opening File.\n");
		exit(1);
	}

	while (1){
		int cip;
		if(fscanf(inp, "%d", &cip) == -1) 
			break;
		Decryption(cip, out); //解密 
	}
	fclose(out);

	return 0;
}

 

发布了52 篇原创文章 · 获赞 114 · 访问量 6000

Guess you like

Origin blog.csdn.net/GD_ONE/article/details/103464093