Demo entry 6687956

111

   

Submitted by 111 on Jan 01, 2018 at 03:45
Language: C++. Code size: 3.2 kB.

#include <iostream>
#include <stdlib.h>
#include <NTL/ZZ.h>
#include <gmp.h>
#include <time.h>
using namespace std;
using namespace NTL;
ZZ Gcd(ZZ a,ZZ b){
    if(b==0)return a;
    else return Gcd(b,a%b);
}
ZZ Mod(ZZ a,ZZ b){
    ZZ n;n=0;
    ZZ res;res=0;
    n=a/b;
    res=a-n*b;
    return res;
}
int gcd(int a,int b){
    if(b==0)return a;
    else return gcd(b,a%b);
}
int mod(int a,int b){
    int n=0,res=0;
    n=a/b;
    res=a-n*b;
    return res;
}
int Pow_mod(int a,int b,int c){
    int res=1;
    if(b!=0){
        int i=0;
        for(i=b;i>0;i--)res=mod(res*a,c);
    }
    return res;
}
void Key_generation(void){
    ZZ p,q,n,l,e,d;p=0;q=0;n=0;l=0;e=0;d=0;
    cout<<"开始生成密钥:"<<endl;
    cout<<"请输入p,q的值:";
    cin>>p>>q;
    n=p*q;
    l=(p-1)*(q-1);
    ZZ i;i=1;
    for(i=2;i<l&&e==0;i++){
        if(Gcd(i,l)==1)e=i;
    }
    for(i=2;i<l&&d==0;i++){
        if(Mod((i*e),l)==1)d=i;
    }
    cout<<"公钥对为("<<e<<","<<n<<")"<<endl;
    cout<<"私钥对为("<<d<<","<<n<<")"<<endl;
}
void Plain_in(char plain[100]){
    int i=0;
    for(i=0;i<100;i++)plain[i]=0;
    cout<<"请输入待签明文:";
    scanf("%s",plain);
}
void Blind(char plain[100],int blind[100]){
    cout<<endl;
    cout<<"开始将明文盲化:"<<endl;
    srand(unsigned(time(NULL)));
    cout<<"请输入公钥对:";
    int e,n;
    cin>>e>>n;
    int r=4;
    while(gcd(r,n)!=1)r++;
    cout<<"盲化变量r的值为:"<<r<<endl;
    int i=0;
    for(i=0;i<100;i++)blind[i]=0;
    for(i=0;plain[i]!=0;i++)blind[i]=mod(plain[i],n)*Pow_mod(r,e,n);
    cout<<"盲化明文为:";
    for(i=0;blind[i]!=0;i++)cout<<blind[i]<<" ";
    cout<<endl;
}
void Sign(int blind[100],int sign[100]){
    cout<<endl;
    cout<<"开始对盲化明文进行盲签名:"<<endl;
    cout<<"请输入私钥对:";
    int d,n;
    cin>>d>>n;
    int i=0;
    for(i=0;i<100;i++)sign[i]=0;
    for(i=0;blind[i]!=0;i++)sign[i]=Pow_mod(blind[i],d,n);
    cout<<"签名后的盲化明文为:";
    for(i=0;sign[i]!=0;i++)cout<<sign[i]<<" ";
    cout<<endl;
}
void De_blind(int sign[100],int deblind[100]){
    cout<<endl;
    cout<<"开始除盲:"<<endl;
    cout<<"请输入公钥对:";
    int e,n;
    cin>>e>>n;
    cout<<"请输入盲化变量r:";
    int r,i;
    cin>>r;
    for(i=0;i<100;i++)deblind[i]=0;
    for(i=0;sign[i]!=0;i++){
        while(mod(sign[i],r)!=0)sign[i]+=n;
    }
    for(i=0;sign[i]!=0;i++)deblind[i]=mod(sign[i]/r,n);
    cout<<"签名后的非盲明文为:";
    for(i=0;deblind[i]!=0;i++)cout<<deblind[i]<<" ";
    cout<<endl;
}
void Confirmation(int deblind[100]){
    cout<<endl;
    cout<<"开始进行认证:"<<endl;
    cout<<"请输入公钥对:";
    int i=0,e,n;
    cin>>e>>n;
    int confirm[100];
    for(i=0;i<100;i++)confirm[i]=0;
    for(i=0;deblind[i]!=0;i++)confirm[i]=Pow_mod(deblind[i],e,n);
    char Confirm[100];
    for(i=0;i<100;i++)Confirm[i]=0;
    for(i=0;confirm[i]!=0;i++)Confirm[i]=confirm[i];
    cout<<"认证明文为:";
    for(i=0;Confirm[i]!=0;i++)cout<<Confirm[i];
    cout<<endl;
}
int main(void)
{
    char plain[100];
    int blind[100];
    int sign[100];
    int deblind[100];
    Key_generation();
    Plain_in(plain);
    Blind(plain,blind);
    Sign(blind,sign);
    De_blind(sign,deblind);
    Confirmation(deblind);
    return 0;
}

This snippet took 0.03 seconds to highlight.

Back to the Entry List or Home.

Delete this entry (admin only).