C中的简单RSA

时间:2014-11-30 20:16:59

标签: c encryption rsa

任务是编写一个可以用RSA加密和解密消息的程序。

我开始写它,但是刚开始程序在我想决定是加密(e)还是解密(d)时不接受我的e。错误在哪里?

我该如何继续?我想我没有得到rsa系统..

谢谢!

#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <string.h>

/* Simple RSA:
encrypt text using a secure cryptographic function that makes decryption
impossible unless the recipient has the corresponding key for decryption.
 */

unsigned long long int p;
unsigned long long int q;
unsigned long long int N;   // modulus for encryption and decryption
unsigned int e;
unsigned long long int d;
unsigned long long int phi;
unsigned int c;
unsigned int m;
signed int s;
signed int t;
unsigned long long int e_or_d;
int i = 0;

int main () {

/* Ask the user about the numbers N and e */

printf("Enter N: ");
scanf("%u", &N);
getchar();
printf("Enter e: ");
scanf("%u", &e);
getchar();

/* Ask the user if he wants to decrypt or encrypt a message */

while(1) {
printf("Encrypt or Decrypt [e/d]? ");
scanf("%2c", &e_or_d);
getchar();

if(e_or_d != e | d) {

return 0;

}


if(e_or_d = e) {

printf("Enter message: \n");
scanf("%u", &m);
getchar();

for (i = 0; i < m; i++) {

N = p*q;
phi = (p-1)*(q-1);

}

if(N < 0) {

return 0;

}

else if(N = 0) {

return 1;

}

else if(N = 1) {

return ?

}

return 0;

}

if(e_or_d = d) {

printf("Enter d: \n");
scanf("%u", &d);
getchar();

return 0;

}
return 0;
}
return 0;
}

1 个答案:

答案 0 :(得分:0)

我不确定你想要实现的目标,我可能会误解这一点。但是在这里:

if(e_or_d != e | d) {
    return 0;
}

这个:|是按位OR。我相信你的意思是:

if ((e_or_d != 'e') && (e_or_d) != 'd'))
    return 0;

检查输入是char 'e'还是char 'd'