#include <iostream>
#include <string.h>
#include <stdio.h>
#include <cmath>
#include <cstdlib>
#include <ctime>
using namespace std;
int exgcd(int a, int b,int *x,int *y) //拓展欧几里得算法
{
if(b==0)
{
*x=1;
*y=0;
return a;
}
int gcd=exgcd(b,a%b,x,y);
int temp=*x;
*x=*y;
*y=temp-a/b*(*y);
return gcd;
}
int isprime(int a) //素数判断
{
int i;
for(i=2;i<1+(a/2);i++){
if(a%i==0)return 1;
}
return 0;
}
void primegenerator(int prime[10]) //生成素数表
{
int i,j=0;
for(i=91;i<=1000;i++){
if(isprime(i)==0){
prime[j]=i;
j++;
}
if(j>9)break;
}
}
int main()
{
int prime[10];
primegenerator(prime);
int seed,p,q;
seed=time(0);
srand((unsigned int)seed); //生成在范围内的随机素数p,q
p=rand()%9;
do{
q=rand()%9;
}while(q==p);
int e,d,n,fi_n,r,nu,w1,w2;
int a;
cout<<"请选择加密/解密"<<endl;
cout<<"输入0代表加密"<<' '<<"输入1代表解密"<<endl;
cin>>a;
char minwen[1000];
int i,j,mi;
if(a==0){
n=prime[p]*prime[q];
fi_n=(prime[p]-1)*(prime[q]-1);
for(r=fi_n/2;n>=1;r--){ //求得公钥
if(exgcd(r,fi_n,&w1,&w2)==1){
e=r;
break;
}
}
r=exgcd(e,fi_n,&d,&nu);
cout<<"请输入明文"<<endl;
scanf("%s",minwen);
int shuma_minwen[strlen(minwen)];
for(i=0;i<strlen(minwen);i++){
shuma_minwen[i]=minwen[i];
}
int shuma_miwen[strlen(minwen)]; //录入结束,开始加密
for(i=0;i<strlen(minwen);i++){
mi=shuma_minwen[i];
shuma_miwen[i]=1;
for(j=1;j<=e;j++){
shuma_miwen[i]=(shuma_miwen[i]*mi)%n;
}
}
cout<<"密文为"<<endl;
for(i=0;i<strlen(minwen);i++){
cout<<shuma_miwen[i]<<' '; //加密结束,输出密文,私钥
}
cout<<endl<<"密文长度为"<<i<<endl;
cout<<endl<<"解密私钥为"<<endl;
cout<<d<<' '<<n<<endl;
}
else if(a==1){
int shuma_jiemiwen[10000];
cout<<"请输入密文长度"<<endl;
int k;
cin>>k;
cout<<"请输入密文"<<endl; //录入密文
int t=0;
for(i=0;i<k;i++){
cin>>shuma_jiemiwen[i];
}
int sizel=k;
cout<<"请输入私钥(d,n) (分别输入d,n用空格隔开)"<<endl;
int d1,n1;
cin>>d1>>n1;
int ming;
int shuma_jieminwen[sizel]; //开始解密
for(i=0;i<sizel;i++){
ming=shuma_jiemiwen[i];
shuma_jieminwen[i]=1;
for(j=0;j<d1;j++){
shuma_jieminwen[i]=shuma_jieminwen[i]*ming%n1;
}
}
char jieminwen[sizel];
for(i=0;i<sizel;i++){
jieminwen[i]=shuma_jieminwen[i];
}
cout<<"明文为"<<endl; //输出明文
for(i=0;i<sizel;i++){
cout<<jieminwen[i];
}
}
return 0;
}
I2luY2x1ZGUgPGlvc3RyZWFtPgojaW5jbHVkZSA8c3RyaW5nLmg+CiNpbmNsdWRlIDxzdGRpby5oPgojaW5jbHVkZSA8Y21hdGg+CiNpbmNsdWRlIDxjc3RkbGliPgojaW5jbHVkZSA8Y3RpbWU+CnVzaW5nIG5hbWVzcGFjZSBzdGQ7CgppbnQgZXhnY2QoaW50IGEsIGludCBiLGludCAqeCxpbnQgKnkpICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgIC8v5ouT5bGV5qyn5Yeg6YeM5b6X566X5rOVCnsKICAgIGlmKGI9PTApCiAgICB7CiAgICAgICAgKng9MTsKICAgICAgICAqeT0wOwogICAgICAgIHJldHVybiBhOwogICAgfQogICAgaW50IGdjZD1leGdjZChiLGElYix4LHkpOwogICAgaW50IHRlbXA9Kng7CiAgICAqeD0qeTsKICAgICp5PXRlbXAtYS9iKigqeSk7CiAgICByZXR1cm4gZ2NkOwp9CgppbnQgaXNwcmltZShpbnQgYSkgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgLy/ntKDmlbDliKTmlq0KewogICAgaW50IGk7CiAgICBmb3IoaT0yO2k8MSsoYS8yKTtpKyspewogICAgICAgIGlmKGElaT09MClyZXR1cm4gMTsKICAgIH0KICAgIHJldHVybiAwOwp9Cgp2b2lkIHByaW1lZ2VuZXJhdG9yKGludCBwcmltZVsxMF0pICAgICAgICAgICAgICAgICAgICAgICAgICAgICAvL+eUn+aIkOe0oOaVsOihqAp7CiAgICBpbnQgaSxqPTA7CiAgICBmb3IoaT05MTtpPD0xMDAwO2krKyl7CiAgICAgICAgaWYoaXNwcmltZShpKT09MCl7CiAgICAgICAgICAgIHByaW1lW2pdPWk7CiAgICAgICAgICAgIGorKzsKICAgICAgICB9CiAgICAgIGlmKGo+OSlicmVhazsKICAgIH0KfQoKCmludCBtYWluKCkKewogICAgaW50IHByaW1lWzEwXTsKICAgIHByaW1lZ2VuZXJhdG9yKHByaW1lKTsKICAgIGludCBzZWVkLHAscTsKICAgIHNlZWQ9dGltZSgwKTsKICAgIHNyYW5kKCh1bnNpZ25lZCBpbnQpc2VlZCk7ICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgLy/nlJ/miJDlnKjojIPlm7TlhoXnmoTpmo/mnLrntKDmlbBw77yMcQogICAgcD1yYW5kKCklOTsKICAgIGRvewogICAgICAgIHE9cmFuZCgpJTk7CiAgICB9d2hpbGUocT09cCk7CiAgICBpbnQgZSxkLG4sZmlfbixyLG51LHcxLHcyOwoKCiAgICBpbnQgYTsKICAgIGNvdXQ8PCLor7fpgInmi6nliqDlr4Yv6Kej5a+GIjw8ZW5kbDsKICAgIGNvdXQ8PCLovpPlhaUw5Luj6KGo5Yqg5a+GIjw8JyAnPDwi6L6T5YWlMeS7o+ihqOino+WvhiI8PGVuZGw7CiAgICBjaW4+PmE7CiAgICBjaGFyIG1pbndlblsxMDAwXTsKCiAgICBpbnQgaSxqLG1pOwogICAgaWYoYT09MCl7CiAgICAgICAgIG49cHJpbWVbcF0qcHJpbWVbcV07CiAgICAgICAgIGZpX249KHByaW1lW3BdLTEpKihwcmltZVtxXS0xKTsKICAgICAgICAgZm9yKHI9Zmlfbi8yO24+PTE7ci0tKXsgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgLy/msYLlvpflhazpkqUKICAgICAgICAgICBpZihleGdjZChyLGZpX24sJncxLCZ3Mik9PTEpewogICAgICAgICAgICAgZT1yOwogICAgICAgICAgICAgIGJyZWFrOwogICAgICAgICAgfQogICAgICAgfQogICAgICAgcj1leGdjZChlLGZpX24sJmQsJm51KTsKICAgICAgICBjb3V0PDwi6K+36L6T5YWl5piO5paHIjw8ZW5kbDsKICAgICAgICBzY2FuZigiJXMiLG1pbndlbik7CiAgICAgICAgaW50IHNodW1hX21pbndlbltzdHJsZW4obWlud2VuKV07CiAgICAgICAgZm9yKGk9MDtpPHN0cmxlbihtaW53ZW4pO2krKyl7CiAgICAgICAgICAgIHNodW1hX21pbndlbltpXT1taW53ZW5baV07CiAgICAgICAgfQogICAgICAgIGludCBzaHVtYV9taXdlbltzdHJsZW4obWlud2VuKV07ICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAvL+W9leWFpee7k+adn++8jOW8gOWni+WKoOWvhgogICAgICAgIGZvcihpPTA7aTxzdHJsZW4obWlud2VuKTtpKyspewogICAgICAgICAgICAgICAgbWk9c2h1bWFfbWlud2VuW2ldOwogICAgICAgICAgICAgICAgc2h1bWFfbWl3ZW5baV09MTsKICAgICAgICAgICAgZm9yKGo9MTtqPD1lO2orKyl7CiAgICAgICAgICAgICAgICBzaHVtYV9taXdlbltpXT0oc2h1bWFfbWl3ZW5baV0qbWkpJW47CiAgICAgICAgICAgIH0KICAgICAgICB9CiAgICAgICAgY291dDw8IuWvhuaWh+S4uiI8PGVuZGw7CiAgICAgICAgZm9yKGk9MDtpPHN0cmxlbihtaW53ZW4pO2krKyl7CiAgICAgICAgICAgIGNvdXQ8PHNodW1hX21pd2VuW2ldPDwnICc7ICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgLy/liqDlr4bnu5PmnZ/vvIzovpPlh7rlr4bmlofvvIznp4HpkqUKICAgICAgICB9CiAgICAgICAgY291dDw8ZW5kbDw8IuWvhuaWh+mVv+W6puS4uiI8PGk8PGVuZGw7CiAgICAgICAgY291dDw8ZW5kbDw8Iuino+WvhuengemSpeS4uiI8PGVuZGw7CiAgICAgICAgY291dDw8ZDw8JyAnPDxuPDxlbmRsOwogICAgfQogICAgZWxzZSBpZihhPT0xKXsKICAgICAgICBpbnQgc2h1bWFfamllbWl3ZW5bMTAwMDBdOwoKICAgICAgICAgY291dDw8Iuivt+i+k+WFpeWvhuaWh+mVv+W6piI8PGVuZGw7CiAgICAgICAgaW50IGs7CiAgICAgICAgY2luPj5rOwogICAgICAgIGNvdXQ8PCLor7fovpPlhaXlr4bmlociPDxlbmRsOyAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgIC8v5b2V5YWl5a+G5paHCiAgICAgICAgaW50IHQ9MDsKICAgICAgICBmb3IoaT0wO2k8aztpKyspewogICAgICAgICAgICBjaW4+PnNodW1hX2ppZW1pd2VuW2ldOwogICAgICAgIH0KICAgICAgICBpbnQgc2l6ZWw9azsKCiAgICAgICAgY291dDw8Iuivt+i+k+WFpeengemSpe+8iGTvvIxu77yJIO+8iOWIhuWIq+i+k+WFpWTvvIxu55So56m65qC86ZqU5byA77yJIjw8ZW5kbDsKICAgICAgICBpbnQgZDEsbjE7CiAgICAgICAgY2luPj5kMT4+bjE7CiAgICAgICAgaW50IG1pbmc7CiAgICAgICAgaW50IHNodW1hX2ppZW1pbndlbltzaXplbF07ICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgIC8v5byA5aeL6Kej5a+GCiAgICAgICAgZm9yKGk9MDtpPHNpemVsO2krKyl7CiAgICAgICAgICAgICAgICBtaW5nPXNodW1hX2ppZW1pd2VuW2ldOwogICAgICAgICAgICAgICAgc2h1bWFfamllbWlud2VuW2ldPTE7CiAgICAgICAgICAgIGZvcihqPTA7ajxkMTtqKyspewogICAgICAgICAgICAgICAgc2h1bWFfamllbWlud2VuW2ldPXNodW1hX2ppZW1pbndlbltpXSptaW5nJW4xOwogICAgICAgICAgICB9CiAgICAgICAgfQoKICAgICAgICBjaGFyIGppZW1pbndlbltzaXplbF07CiAgICAgICAgZm9yKGk9MDtpPHNpemVsO2krKyl7CiAgICAgICAgICAgIGppZW1pbndlbltpXT1zaHVtYV9qaWVtaW53ZW5baV07CiAgICAgICAgfQogICAgICAgIGNvdXQ8PCLmmI7mlofkuLoiPDxlbmRsOyAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgLy/ovpPlh7rmmI7mlocKICAgICAgICBmb3IoaT0wO2k8c2l6ZWw7aSsrKXsKICAgICAgICAgICAgY291dDw8amllbWlud2VuW2ldOwogICAgICAgIH0KICAgIH0KICAgIHJldHVybiAwOwp9CiA=