RSA的原理这篇文章介绍的比较好:
根据介绍写了下面的代码:
#include "stdio.h" #include "stdlib.h" #include "string.h" static int rsa_check_prime_number(unsigned int n) { unsigned int i = 0; if(n < 1) { return -1; } for(i = 2;i < n;i++) { if((n % i) == 0) { return -1; } } return 0; } int rsa_create_key() { unsigned int prime_number1, prime_number2; unsigned int euler_n; unsigned int prime_number_e, prime_number_d, multiply_n; unsigned int i = 0; printf("enter two prime numbers: number1,number2\n\r"); scanf("%d,%d", &prime_number1, &prime_number2); if(0 != rsa_check_prime_number(prime_number1) || 0 != rsa_check_prime_number(prime_number2)) { printf("number invalid...\n\r"); return -1; } multiply_n = prime_number1 * prime_number2; euler_n = (prime_number1 - 1) * (prime_number2 - 1); printf("two prime numbers:%d , %d, %d , %d\n\r", prime_number1, prime_number2, multiply_n, euler_n); printf("select a prime number between 1 and %d :\n\r", euler_n); scanf("%d", &prime_number_e); for(i = 0;i < euler_n;i++) { if((i * prime_number_e - 1) % euler_n == 0) { break; } } if(i >= euler_n) { return -1; } prime_number_d = i; printf("public: %d, %d\n\r",multiply_n, prime_number_e); printf("private: %d, %d\n\r",multiply_n, prime_number_d); return 0; } typedef struct { unsigned int rsa_p; unsigned int rsa_e; unsigned int rsa_d; }rsa_factor_t; static rsa_factor_t rsa_factor; int rsa_set_public_key(unsigned int common_p, unsigned int public_e) { rsa_factor.rsa_p = common_p; rsa_factor.rsa_e = public_e; printf("rsa_p:%d, rsa_e:%d\n\r", rsa_factor.rsa_p, rsa_factor.rsa_e); return 0; } int rsa_set_private_key(unsigned int common_p, unsigned int private_d) { rsa_factor.rsa_p = common_p; rsa_factor.rsa_d = private_d; printf("rsa_p:%d, rsa_d:%d\n\r", rsa_factor.rsa_p, rsa_factor.rsa_d); return 0; } int rsa_encrypt(unsigned char *text , unsigned char *buff, unsigned int len) { unsigned int i = 0, j = 0, c = 0, tmp_c = 0; if(buff == NULL) { printf("buff invalid !\n\r"); return -1; } for(i = 0;i < len;i++) { tmp_c = text[i]; c = text[i]; for(j = 1; j < rsa_factor.rsa_e; j++) { tmp_c = (tmp_c * c) % rsa_factor.rsa_p; } ((unsigned int *)buff)[i] = tmp_c; } return (i * sizeof(unsigned int)); } int rsa_decrypt(unsigned char *text, unsigned char *buff, unsigned int len) { unsigned int i = 0, j = 0; unsigned long long int c = 0, tmp_c = 0; if(buff == NULL) { printf("buff invalid !\n\r"); return -1; } for(i = 0;i < (len / sizeof(int));i++) { tmp_c = ((unsigned int *)text)[i]; c = ((unsigned int *)text)[i]; for(j = 1; j < rsa_factor.rsa_d; j++) { tmp_c = (tmp_c * c) % rsa_factor.rsa_p; } buff[i] = (unsigned char)(tmp_c & 0xff); } return i; } int main() { unsigned char sel = 1; unsigned int key_1, key_2; unsigned char tmp_buf[128]; unsigned char rsa_buf[128]; unsigned int i = 0, count = 0; printf("*********************************************\n\r"); printf("******RSA encryption and decryption *********\n\r"); printf("*********************************************\n\r"); while(1) { printf("***1. create public and private key \n\r"); printf("***2. data encryption \n\r"); printf("***3. data decryption \n\r"); printf("***enter number : \n\r"); scanf("%d", &sel); if(sel < 1 || sel > 3) { continue; } switch(sel) { case 1: if(0 != rsa_create_key()) { printf("fail to create key ... \n\r"); } break; case 2: printf("set public key: common_p,rsa_e\n\r"); scanf("%d,%d", &key_1, &key_2); rsa_set_public_key(key_1, key_2); printf("enter your information: \n\r"); scanf("%s", (char *)tmp_buf); count = strlen((char *)tmp_buf); count = rsa_encrypt(tmp_buf, rsa_buf, count); printf("::::\n\r"); for(i = 0;i < count;i++) { printf("%d ", rsa_buf[i]); } printf("\n\r"); break; case 3: printf("set private key: common_p,rsa_d\n\r"); scanf("%d,%d", &key_1, &key_2); rsa_set_private_key(key_1, key_2); printf("enter your data len: \n\r"); scanf("%d", &count); printf("please enter %d numbers: \n\r", count); for(i = 0;i < count; i++) { scanf("%d", &rsa_buf[i]); } printf("::::\n\r"); count = rsa_decrypt(rsa_buf, tmp_buf, count); for(i = 0;i <= count;i++) { printf("%c", tmp_buf[i]); } printf("\n\r"); break; default: break; } } }
试了运行了一下,好像是可以的:
创建密钥:
用公共密钥加密"EEPW"
得到16个数据然后用私有密钥解密:
。。。。
大家可以玩玩