RSA算法代码#inclede<stdio.h> #include<math.h> int Prime(int _m){int i,k; if(m<2) return 0;补充剩余代码
时间: 2023-12-20 12:04:46 浏览: 81
int Prime(int _m){
int i,k;
if(m<2) return 0;
k = sqrt(m);
for(i=2;i<=k;i++){
if(m%i==0) return 0;
}
return 1;
}
// RSA加密
int RSA_Encrypt(int p,int q,int e,int m){
int n = p*q;
int fn = (p-1)*(q-1);
int i,d;
for(i=1;i<fn;i++){
if((i*e)%fn==1){
d = i;
break;
}
}
int c = pow(m,e);
c = fmod(c,n);
return c;
}
// RSA解密
int RSA_Decrypt(int p,int q,int e,int c){
int n = p*q;
int fn = (p-1)*(q-1);
int i,d;
for(i=1;i<fn;i++){
if((i*e)%fn==1){
d = i;
break;
}
}
int m = pow(c,d);
m = fmod(m,n);
return m;
}
相关问题
C语言实现RSA算法代码
以下是C语言实现RSA算法的代码示例:
```c
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
int main() {
int p, q, n, phi, e, d, plain_text, cipher_text, decrypted_text;
printf("Enter two prime numbers p and q: ");
scanf("%d %d", &p, &q);
n = p * q;
phi = (p - 1) * (q - 1);
// Select an integer e such that 1 < e < phi and gcd(e, phi) = 1.
printf("Enter the value of e such that gcd(e, phi) = 1: ");
scanf("%d", &e);
// Calculate d such that d ≡ e^(-1) (mod phi).
d = fmod(pow(e, -1), phi);
printf("Enter the plain text: ");
scanf("%d", &plain_text);
// Encryption: c = (plain_text^e) % n.
cipher_text = fmod(pow(plain_text, e), n);
printf("Cipher text: %d\n", cipher_text);
// Decryption: plain_text = (cipher_text^d) % n.
decrypted_text = fmod(pow(cipher_text, d), n);
printf("Decrypted text: %d\n", decrypted_text);
return 0;
}
```
在该示例中,用户需要输入两个素数`p`和`q`、加密指数`e`以及明文`plain_text`。程序计算出了`n`和`phi`,然后计算出了私钥`d`。使用公钥`(n, e)`对明文进行加密,使用私钥`(n, d)`对密文进行解密。
请注意,此示例仅用于演示目的,实际使用中需要考虑更多的安全性和性能方面的问题。
RSA算法的加解密C语言代码
以下是RSA算法的加解密C语言代码:
```c
#include<stdio.h>
#include<stdlib.h>
#include<math.h>
#include<string.h>
long int p, q, n, t, flag, e[100], d[100], temp[100], j, m[100], en[100], i;
char msg[100];
int prime(long int);
void ce();
long int cd(long int);
void encrypt();
void decrypt();
int main() {
printf("\nEnter two prime numbers: ");
scanf("%ld %ld", &p, &q);
flag = prime(p);
if(flag == 0) {
printf("\n%d is not prime\n", p);
exit(1);
}
flag = prime(q);
if(flag == 0 || p == q) {
printf("\n%d is not prime\n", q);
exit(1);
}
printf("\nEnter message: ");
fflush(stdin);
scanf("%s", msg);
for(i = 0; msg[i] != '\0'; i++)
m[i] = msg[i];
n = p * q;
t = (p - 1) * (q - 1);
ce();
printf("\nPossible values of e and d are:\n");
for(i = 0; i < j - 1; i++)
printf("\n%ld\t%ld", e[i], d[i]);
encrypt();
decrypt();
return 0;
}
int prime(long int pr) {
int i;
j = sqrt(pr);
for(i = 2; i <= j; i++) {
if(pr % i == 0)
return 0;
}
return 1;
}
void ce() {
int k;
k = 0;
for(i = 2; i < t; i++) {
if(t % i == 0)
continue;
flag = prime(i);
if(flag == 1 && i != p && i != q) {
e[k] = i;
flag = cd(e[k]);
if(flag > 0) {
d[k] = flag;
k++;
}
if(k == 99)
break;
}
}
}
long int cd(long int x) {
long int k = 1;
while(1) {
k = k + t;
if(k % x == 0)
return(k / x);
}
}
void encrypt() {
long int pt, ct, key = e[0], k, len;
i = 0;
len = strlen(msg);
while(i != len) {
pt = m[i];
pt = pt - 96;
k = 1;
for(j = 0; j < key; j++) {
k = k * pt;
k = k % n;
}
temp[i] = k;
ct = k + 96;
en[i] = ct;
i++;
}
en[i] = -1;
printf("\nEncrypted message: ");
for(i = 0; en[i] != -1; i++)
printf("%c", en[i]);
}
void decrypt() {
long int pt, ct, key = d[0], k;
i = 0;
while(en[i] != -1) {
ct = temp[i];
k = 1;
for(j = 0; j < key; j++) {
k = k * ct;
k = k % n;
}
pt = k + 96;
m[i] = pt;
i++;
}
m[i] = -1;
printf("\nDecrypted message: ");
for(i = 0; m[i] != -1; i++)
printf("%c", m[i]);
printf("\n");
}
```
阅读全文