非对称加密体制(RSA)

mac2026-05-26  2

#include<stdio.h> int rsa(unsigned long long int a,unsigned long long int b,unsigned long long int c) { unsigned long long int r=1; while(b!=0) { r=r*a; r=r%c; b--; } return r; } int sushu( unsigned long long int a) { unsigned long long int b=2; for(b=2;b<a/2+1;b++) { if(a%b==0) return 1; } } int gcd( unsigned long long int a, unsigned long long int b) { if(b==0) return a; else return gcd(b,a%b); } int main() { int f; unsigned long long int p,q,e,m,a,t,c,n,d; printf("Please input p:"); scanf("%ld",&p); while(sushu(p)==1) { printf("输入不是素数,请再次输入。"); scanf("%ld",&p); } printf("Please input q:"); scanf("%ld",&q); while(sushu(p)==1) { printf("输入不是素数,请再次输入。"); scanf("%ld",&q); } n = p*q; printf("the n is %lld\n",n); t = (p-1)*(q-1); printf("the t is %lld\n",t); printf("please input the e: "); scanf("%ld", &e); while(e < 1 || e > t || gcd(e,t) != 1){ printf("e is error,please input again: "); scanf("%ld", &e); } d = 1; while(((e*d)%t)!=1) d++; while(1) { printf(" the d is %ld\n",d); printf("\n"); printf("加密 please input 1\n"); printf("解密 please input 2\n"); printf("结束 please input 3\n"); scanf("%d", &f); if(f==1) { printf("输入要加密的明文数字:"); scanf("%lld", &m); c = rsa(m, e, n); printf("加密后的数字为%ld\n",c); } else if(f==2) { printf("输入要解密的密文数字:"); scanf("%ld", &c); m = rsa(c, d, n); printf("解密后的数字为%ld\n",m); } else if(f==3) break; else printf("输入不正确!"); } return 0; }
最新回复(0)