#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int p, q, e, d; //p, q是两个大素数, e是公开秘钥,d是保密的解密秘钥
int Euclid(int n, int u) {
//Euclid算法
int n1, n2, b1, b2, q, r, t;
n1 = n;
n2 = u;
b1 = 0;
b2 = 1;
q = n1 / n2;
r = n1 - q * n2;
while(r!=0) {
n1 = n2;
n2 = r;
t = b2;
b2 = b1 - q * b2;
b1 = t;
q = n1 / n2;
r = n1 - q * n2;
}
if(n2!=1) {
printf("%d模%d不存在逆元!\n", u, n);
exit(0);
}else {
while(b2<0||b2>n) {
if(b2<0) {
b2 += n;
}else {
b2 -= n;
}
}
printf("%d模%d的逆元为%d\n", u, n, b2);
}
return b2;
}
int Miller_Rabin(int n) {
//利用Miller_Rabin素性测试法测试一个大数是否为素数
int p = n - 1;
int s = 0;
int m, i, b, r, z=0, is;
m = p;
if(m%2==1) {
printf("%d是偶数\n", p);
exit(0);
}
while((m-m/2*2)==0) {
m = m / 2;
s++;
}
srand( (unsigned)time( NULL ) );
for(i=0; i<20; i++) {
z=0;
while(z==0) {
b = rand();
while(b>=n) {
b = b - n;
}
z = b;
}
r = 0;
for(is=1; is<m; is++) {
z = z * b;
while(z>n) {
z = z - n;
}
}
while(z!=n-1&&z!=1) {
if(r==s-1) {
return 0;
}else {
r++;
z = z * z - z * z / n * n;
}
}
}
printf("%d是素数\n", n);
return 1;
}
int mod_cal(int m, int k, int n) {
//m的k次方mod n的运算
int i, t;
t = m;
for(i=1; i<k; i++) {
t = t * m;
while(t > n) {
t = t - n;
}
}
return t;
}
int gcd(int m, int n) {
//辗转相除法
int p;
if(n > m) {
p = n;
n = m;
m = p;
}
p = m % n;
if(p==0) {
return n;
}else {
gcd(n, p);
}
}
int RSA_encryp() {
//RSA加密
return 0;
}
int main() {
int n, non_n, c, m, i = 99999;
char encryption[] = "I LOVE NANJING UNIVERSITY OF AERONAUTICS AND ASTRONAUTICS";
char decryption[60], encryptext[60];
int cryp[60];
printf("********************************************************************************\n");
printf(" RSA加密\n\n");
printf("********************************************************************************\n\n");
printf("请输入素数q:");
scanf("%d", &q);
getchar();
while(Miller_Rabin(q)==0) {
printf("%d 不是素数!请重输:", q);
scanf("%d", &q);
getchar();
}
printf("\n请输入素数p:");
scanf("%d", &p);
getchar();
while(Miller_Rabin(p)==0) {
printf("%d 不是素数", p);
scanf("%d", &p);
getchar();
}
printf("\n");
n = p * q;
non_n = (p-1) * (q-1);
srand( (unsigned)time( NULL ) );
e = rand();
e = e % non_n;
while(gcd(e,non_n)!=1) {
e = rand();
e = e % non_n;
}
d = Euclid(non_n, e);
printf("e:%d\t", e);
printf("d:%d\n\n", d);
i = 0;
while(encryption[i]!='\0') {
if(encryption[i]==' ') {
m = 0;
}else {
m = encryption[i] - 'A' + 1;
}
c = mod_cal(m, e, n);
cryp[i] = c;
c = c % 26;
if(c==0) {
decryption[i] = ' ';
}else {
decryption[i] = 'A' + c -1;
}
i++;
}
decryption[i] = '\0';
printf("明文:%s\n", encryption);
printf("密文:%s\n", decryption);
printf("解密:");
i = 0;
while(decryption[i]!='\0') {
c = mod_cal(cryp[i], d, n);
c = c % 26;
if(c==0) {
encryptext[i] = ' ';
}else {
encryptext[i] = 'A' + c -1;
}
i++;
}
encryptext[i] = '\0';
printf("%s\n", encryptext);
return 0;
}
RSA.zip_D-H_Love T.K.O_RSA字母
版权申诉
76 浏览量
2022-09-23
17:27:43
上传
评论
收藏 1KB ZIP 举报
朱moyimi
- 粉丝: 61
- 资源: 1万+
最新资源
- 最全空间计量实证方法(空间杜宾模型和检验以及结果解释文档).txt
- 5uonly.apk
- 蓝桥杯Python组的历年真题
- 2023-04-06-项目笔记 - 第一百十九阶段 - 4.4.2.117全局变量的作用域-117 -2024.04.30
- 2023-04-06-项目笔记 - 第一百十九阶段 - 4.4.2.117全局变量的作用域-117 -2024.04.30
- 前端开发技术实验报告:内含4四实验&实验报告
- Highlight Plus v20.0.1
- 林周瑜-论文.docx
- 基于MIC+NE555光敏电阻的声光控电路Multisim仿真原理图
- 基于JSP毕业设计-基于WEB操作系统课程教学网站的设计与实现(源代码+论文).zip
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈