zoukankan      html  css  js  c++  java
  • RSA密码系统 基于大数环境编写 密码学课程设计

    RSA密码系统的实现

    1.问题描述

    RSA密码系统可具体描述为:取两个大素数p和q,令n=pq,N=(p-1)(q-1),随机选择整数d,满足gcd(d,N)=1,ed=1 modN。

    公开密钥:k1=(n,e)

    私有密钥:k2=(p,q,d)

    加密算法:对于待加密消息m,其对应的密文为c=E(m)=me(modn)

       解密算法:D(c)=cd(modn)

    2.基本要求

       p,q,d,e参数选取合理,程序要求界面友好,自动化程度高。

    3. 实现提示

    要实现一个真实的RSA密码系统,主要考虑对大整数的处理。P和q是1024位的,n取2048位

    本人在此设计中 选择了 3个连续的梅森素数     

    不知道我的程序是不是完善  发出来分享下吧

    #include <iostream>
    #include <cstring>
    #include<math.h>
    #include<stdio.h>
    #include<string.h>
    using namespace std;
    #define DIGIT 4      //四位隔开,即万进制
    #define DEPTH 10000        //万进制
    #define MAX     2000
    typedef int bignum_t[MAX+1];
    /************************************************************************/
    /* 读取操作数,对操作数进行处理存储在数组里                             */
    /*******************************************
    *****************************/
    int read(bignum_t a,istream&is=cin)
    {
        char buf[MAX*DIGIT+1],ch ;
        int i,j ;
        memset((void*)a,0,sizeof(bignum_t));
        if(!(is>>buf))return 0 ;
        for(a[0]=strlen(buf),i=a[0]/2-1;i>=0;i--)
    		ch=buf[i],buf[i]=buf[a[0]-1-i],buf[a[0]-1-i]=ch ;
        for(a[0]=(a[0]+DIGIT-1)/DIGIT,j=strlen(buf);j<a[0]*DIGIT;buf[j++]='0');
        for(i=1;i<=a[0];i++)
    		for(a[i]=0,j=0;j<DIGIT;j++)
    			a[i]=a[i]*10+buf[i*DIGIT-1-j]-'0' ;
    		for(;!a[a[0]]&&a[0]>1;a[0]--);
    		return 1 ;
    }
    void write(const bignum_t a,ostream&os=cout)
    {
        int i,j ;
        for(os<<a[i=a[0]],i--;i;i--)
    		for(j=DEPTH/10;j;j/=10)
    			os<<a[i]/j%10 ;
    }
    int comp(const bignum_t a,const bignum_t b)
    {
        int i ;
        if(a[0]!=b[0])
    		return a[0]-b[0];
        for(i=a[0];i;i--)
    		if(a[i]!=b[i])
    			return a[i]-b[i];
    		return 0 ;
    }
    int comp(const bignum_t a,const int b)
    {
        int c[12]=
        {
    		1
        }
        ;
        for(c[1]=b;c[c[0]]>=DEPTH;c[c[0]+1]=c[c[0]]/DEPTH,c[c[0]]%=DEPTH,c[0]++);
        return comp(a,c);
    }
    int comp(const bignum_t a,const int c,const int d,const bignum_t b)
    {
        int i,t=0,O=-DEPTH*2 ;
        if(b[0]-a[0]<d&&c)
    		return 1 ;
        for(i=b[0];i>d;i--)
        {
            t=t*DEPTH+a[i-d]*c-b[i];
            if(t>0)return 1 ;
            if(t<O)return 0 ;
        }
        for(i=d;i;i--)
        {
            t=t*DEPTH-b[i];
            if(t>0)return 1 ;
            if(t<O)return 0 ;
        }
        return t>0 ;
    }
    /************************************************************************/
    /* 大数与大数相加                                                       */
    /************************************************************************/
    void add(bignum_t a,const bignum_t b)
    {
        int i ;
        for(i=1;i<=b[0];i++)
    		if((a[i]+=b[i])>=DEPTH)
    			a[i]-=DEPTH,a[i+1]++;
    		if(b[0]>=a[0])
    			a[0]=b[0];
    		else 
    			for(;a[i]>=DEPTH&&i<a[0];a[i]-=DEPTH,i++,a[i]++);
    			a[0]+=(a[a[0]+1]>0);
    }
    /************************************************************************/
    /* 大数与小数相加                                                       */
    /************************************************************************/
    void add(bignum_t a,const int b)
    {
        int i=1 ;
        for(a[1]+=b;a[i]>=DEPTH&&i<a[0];a[i+1]+=a[i]/DEPTH,a[i]%=DEPTH,i++);
        for(;a[a[0]]>=DEPTH;a[a[0]+1]=a[a[0]]/DEPTH,a[a[0]]%=DEPTH,a[0]++);
    }
    /************************************************************************/
    /* 大数相减(被减数>=减数)                                               */
    /************************************************************************/
    void sub(bignum_t a,const bignum_t b)
    {
        int i ;
        for(i=1;i<=b[0];i++)
    		if((a[i]-=b[i])<0)
    			a[i+1]--,a[i]+=DEPTH ;
    		for(;a[i]<0;a[i]+=DEPTH,i++,a[i]--);
    		for(;!a[a[0]]&&a[0]>1;a[0]--);
    }
    /************************************************************************/
    /* 大数减去小数(被减数>=减数)                                           */
    /************************************************************************/
    void sub(bignum_t a,const int b)
    {
        int i=1 ;
        for(a[1]-=b;a[i]<0;a[i+1]+=(a[i]-DEPTH+1)/DEPTH,a[i]-=(a[i]-DEPTH+1)/DEPTH*DEPTH,i++);
        for(;!a[a[0]]&&a[0]>1;a[0]--);
    }
    void sub(bignum_t a,const bignum_t b,const int c,const int d)
    {
        int i,O=b[0]+d ;
        for(i=1+d;i<=O;i++)
    		if((a[i]-=b[i-d]*c)<0)
    			a[i+1]+=(a[i]-DEPTH+1)/DEPTH,a[i]-=(a[i]-DEPTH+1)/DEPTH*DEPTH ;
    		for(;a[i]<0;a[i+1]+=(a[i]-DEPTH+1)/DEPTH,a[i]-=(a[i]-DEPTH+1)/DEPTH*DEPTH,i++);
    		for(;!a[a[0]]&&a[0]>1;a[0]--);
    }
    /************************************************************************/
    /* 大数相乘,读入被乘数a,乘数b,结果保存在c[]                          */
    /************************************************************************/
    void mul(bignum_t c,const bignum_t a,const bignum_t b)
    {
        int i,j ;
        memset((void*)c,0,sizeof(bignum_t));
        for(c[0]=a[0]+b[0]-1,i=1;i<=a[0];i++)
    		for(j=1;j<=b[0];j++)
    			if((c[i+j-1]+=a[i]*b[j])>=DEPTH)
    				c[i+j]+=c[i+j-1]/DEPTH,c[i+j-1]%=DEPTH ;
    			for(c[0]+=(c[c[0]+1]>0);!c[c[0]]&&c[0]>1;c[0]--);
    }
    /************************************************************************/
    /* 大数乘以小数,读入被乘数a,乘数b,结果保存在被乘数                   */
    /************************************************************************/
    void mul(bignum_t a,const int b)
    {
        int i ;
        for(a[1]*=b,i=2;i<=a[0];i++)
        {
            a[i]*=b ;
            if(a[i-1]>=DEPTH)
    			a[i]+=a[i-1]/DEPTH,a[i-1]%=DEPTH ;
        }
        for(;a[a[0]]>=DEPTH;a[a[0]+1]=a[a[0]]/DEPTH,a[a[0]]%=DEPTH,a[0]++);
        for(;!a[a[0]]&&a[0]>1;a[0]--);
    }
    void mul(bignum_t b,const bignum_t a,const int c,const int d)
    {
        int i ;
        memset((void*)b,0,sizeof(bignum_t));
        for(b[0]=a[0]+d,i=d+1;i<=b[0];i++)
    		if((b[i]+=a[i-d]*c)>=DEPTH)
    			b[i+1]+=b[i]/DEPTH,b[i]%=DEPTH ;
    		for(;b[b[0]+1];b[0]++,b[b[0]+1]=b[b[0]]/DEPTH,b[b[0]]%=DEPTH);
    		for(;!b[b[0]]&&b[0]>1;b[0]--);
    }
    /**************************************************************************/
    /* 大数相除,读入被除数a,除数b,结果保存在c[]数组                         */
    /* 需要comp()函数                                                         */
    /**************************************************************************/
    void div(bignum_t c,bignum_t a,const bignum_t b)
    {
        int h,l,m,i ;
        memset((void*)c,0,sizeof(bignum_t));
        c[0]=(b[0]<a[0]+1)?(a[0]-b[0]+2):1 ;
        for(i=c[0];i;sub(a,b,c[i]=m,i-1),i--)
    		for(h=DEPTH-1,l=0,m=(h+l+1)>>1;h>l;m=(h+l+1)>>1)
    			if(comp(b,m,i-1,a))h=m-1 ;
    			else l=m ;
    			for(;!c[c[0]]&&c[0]>1;c[0]--);
    			c[0]=c[0]>1?c[0]:1 ;
    }
    void div(bignum_t a,const int b,int&c)
    {
        int i ;
        for(c=0,i=a[0];i;c=c*DEPTH+a[i],a[i]=c/b,c%=b,i--);
        for(;!a[a[0]]&&a[0]>1;a[0]--);
    }
    /************************************************************************/
    /* 大数平方根,读入大数a,结果保存在b[]数组里                           */
    /* 需要comp()函数                                                       */
    /************************************************************************/
    void sqrt(bignum_t b,bignum_t a)
    {
        int h,l,m,i ;
        memset((void*)b,0,sizeof(bignum_t));
        for(i=b[0]=(a[0]+1)>>1;i;sub(a,b,m,i-1),b[i]+=m,i--)
    		for(h=DEPTH-1,l=0,b[i]=m=(h+l+1)>>1;h>l;b[i]=m=(h+l+1)>>1)
    			if(comp(b,m,i-1,a))h=m-1 ;
    			else l=m ;
    			for(;!b[b[0]]&&b[0]>1;b[0]--);
    			for(i=1;i<=b[0];b[i++]>>=1);
    }
    /************************************************************************/
    /* 返回大数的长度                                                       */
    /************************************************************************/
    int length(const bignum_t a)
    {
        int t,ret ;
        for(ret=(a[0]-1)*DIGIT,t=a[a[0]];t;t/=10,ret++);
        return ret>0?ret:1 ;
    }
    /************************************************************************/
    /* 返回指定位置的数字,从低位开始数到第b位,返回b位上的数               */
    /************************************************************************/
    int digit(const bignum_t a,const int b)
    {
        int i,ret ;
        for(ret=a[(b-1)/DIGIT+1],i=(b-1)%DIGIT;i;ret/=10,i--);
        return ret%10 ;
    }
    /************************************************************************/
    /* 返回大数末尾0的个数                                                  */
    /************************************************************************/
    int zeronum(const bignum_t a)
    {
        int ret,t ;
        for(ret=0;!a[ret+1];ret++);
        for(t=a[ret+1],ret*=DIGIT;!(t%10);t/=10,ret++);
        return ret ;
    }
    void comp(int*a,const int l,const int h,const int d)
    {
        int i,j,t ;
        for(i=l;i<=h;i++)
    		for(t=i,j=2;t>1;j++)
    			while(!(t%j))
    				a[j]+=d,t/=j ;
    }
    void convert(int*a,const int h,bignum_t b)
    {
        int i,j,t=1 ;
        memset(b,0,sizeof(bignum_t));
        for(b[0]=b[1]=1,i=2;i<=h;i++)
    		if(a[i])
    			for(j=a[i];j;t*=i,j--)
    				if(t*i>DEPTH)
    					mul(b,t),t=1 ;
    				mul(b,t);
    }
    /************************************************************************/
    /* 组合数                                                               */
    /************************************************************************/
    void combination(bignum_t a,int m,int n)
    {
        int*t=new int[m+1];
        memset((void*)t,0,sizeof(int)*(m+1));
        comp(t,n+1,m,1);
        comp(t,2,m-n,-1);
        convert(t,m,a);
        delete[]t ;
    }
    /************************************************************************/
    /* 排列数                                                               */
    /************************************************************************/
    void permutation(bignum_t a,int m,int n)
    {
        int i,t=1 ;
        memset(a,0,sizeof(bignum_t));
        a[0]=a[1]=1 ;
        for(i=m-n+1;i<=m;t*=i++)
    		if(t*i>DEPTH)
    			mul(a,t),t=1 ;
    		mul(a,t);
    }
    #define SGN(x) ((x)>0?1:((x)<0?-1:0))
    #define ABS(x) ((x)>0?(x):-(x))
    int read(bignum_t a,int&sgn,istream&is=cin)
    {
        char str[MAX*DIGIT+2],ch,*buf ;
        int i,j ;
        memset((void*)a,0,sizeof(bignum_t));
        if(!(is>>str))return 0 ;
        buf=str,sgn=1 ;
        if(*buf=='-')sgn=-1,buf++;
        for(a[0]=strlen(buf),i=a[0]/2-1;i>=0;i--)
    		ch=buf[i],buf[i]=buf[a[0]-1-i],buf[a[0]-1-i]=ch ;
        for(a[0]=(a[0]+DIGIT-1)/DIGIT,j=strlen(buf);j<a[0]*DIGIT;buf[j++]='0');
        for(i=1;i<=a[0];i++)
    		for(a[i]=0,j=0;j<DIGIT;j++)
    			a[i]=a[i]*10+buf[i*DIGIT-1-j]-'0' ;
    		for(;!a[a[0]]&&a[0]>1;a[0]--);
    		if(a[0]==1&&!a[1])sgn=0 ;
    		return 1 ;
    }
    struct bignum 
    {
        bignum_t num ;
        int sgn ;
    public :
        inline bignum()
        {
            memset(num,0,sizeof(bignum_t));
            num[0]=1 ;
            sgn=0 ;
        }
        inline int operator!()
        {
            return num[0]==1&&!num[1];
        }
        inline bignum&operator=(const bignum&a)
        {
            memcpy(num,a.num,sizeof(bignum_t));
            sgn=a.sgn ;
            return*this ;
        }
        inline bignum&operator=(const int a)
        {
            memset(num,0,sizeof(bignum_t));
            num[0]=1 ;
            sgn=SGN (a);
            add(num,sgn*a);
            return*this ;
        }
        ;
        inline bignum&operator+=(const bignum&a)
        {
            if(sgn==a.sgn)add(num,a.num);
            else if         
    			(sgn&&a.sgn)
            {
                int ret=comp(num,a.num);
                if(ret>0)sub(num,a.num);
                else if(ret<0)
                {
                    bignum_t t ;
                    memcpy(t,num,sizeof(bignum_t));
                    memcpy(num,a.num,sizeof(bignum_t));
                    sub (num,t);
                    sgn=a.sgn ;
                }
                else memset(num,0,sizeof(bignum_t)),num[0]=1,sgn=0 ;
            }
            else if(!sgn)
    			memcpy(num,a.num,sizeof(bignum_t)),sgn=a.sgn ;
            return*this ;
        }
        inline bignum&operator+=(const int a)
        {
            if(sgn*a>0)add(num,ABS(a));
            else if(sgn&&a)
            {
                int  ret=comp(num,ABS(a));
                if(ret>0)sub(num,ABS(a));
                else if(ret<0)
                {
                    bignum_t t ;
                    memcpy(t,num,sizeof(bignum_t));
                    memset(num,0,sizeof(bignum_t));
                    num[0]=1 ;
                    add(num,ABS (a));
                    sgn=-sgn ;
                    sub(num,t);
                }
                else memset(num,0,sizeof(bignum_t)),num[0]=1,sgn=0 ;
            }
            else if 
    			(!sgn)sgn=SGN(a),add(num,ABS(a));
            return*this ;
        }
        inline bignum operator+(const bignum&a)
        {
            bignum ret ;
            memcpy(ret.num,num,sizeof (bignum_t));
            ret.sgn=sgn ;
            ret+=a ;
            return ret ;
        }
        inline bignum operator+(const int a)
        {
            bignum ret ;
            memcpy(ret.num,num,sizeof (bignum_t));
            ret.sgn=sgn ;
            ret+=a ;
            return ret ;
        }
        inline bignum&operator-=(const bignum&a)
        {
            if(sgn*a.sgn<0)add(num,a.num);
            else if         
    			(sgn&&a.sgn)
            {
                int ret=comp(num,a.num);
                if(ret>0)sub(num,a.num);
                else if(ret<0)
                {
                    bignum_t t ;
                    memcpy(t,num,sizeof(bignum_t));
                    memcpy(num,a.num,sizeof(bignum_t));
                    sub(num,t);
                    sgn=-sgn ;
                }
                else memset(num,0,sizeof(bignum_t)),num[0]=1,sgn=0 ;
            }
            else if(!sgn)add (num,a.num),sgn=-a.sgn ;
            return*this ;
        }
        inline bignum&operator-=(const int a)
        {
            if(sgn*a<0)add(num,ABS(a));
            else if(sgn&&a)
            {
                int  ret=comp(num,ABS(a));
                if(ret>0)sub(num,ABS(a));
                else if(ret<0)
                {
                    bignum_t t ;
                    memcpy(t,num,sizeof(bignum_t));
                    memset(num,0,sizeof(bignum_t));
                    num[0]=1 ;
                    add(num,ABS(a));
                    sub(num,t);
                    sgn=-sgn ;
                }
                else memset(num,0,sizeof(bignum_t)),num[0]=1,sgn=0 ;
            }
            else if 
    			(!sgn)sgn=-SGN(a),add(num,ABS(a));
            return*this ;
        }
        inline bignum operator-(const bignum&a)
        {
            bignum ret ;
            memcpy(ret.num,num,sizeof(bignum_t));
            ret.sgn=sgn ;
            ret-=a ;
            return ret ;
        }
        inline bignum operator-(const int a)
        {
            bignum ret ;
            memcpy(ret.num,num,sizeof(bignum_t));
            ret.sgn=sgn ;
            ret-=a ;
            return ret ;
        }
        inline bignum&operator*=(const bignum&a)
        {
            bignum_t t ;
            mul(t,num,a.num);
            memcpy(num,t,sizeof(bignum_t));
            sgn*=a.sgn ;
            return*this ;
        }
        inline bignum&operator*=(const int a)
        {
            mul(num,ABS(a));
            sgn*=SGN(a);
            return*this ;
        }
        inline bignum operator*(const bignum&a)
        {
            bignum ret ;
            mul(ret.num,num,a.num);
            ret.sgn=sgn*a.sgn ;
            return ret ;
        }
        inline bignum operator*(const int a)
        {
            bignum ret ;
            memcpy(ret.num,num,sizeof (bignum_t));
            mul(ret.num,ABS(a));
            ret.sgn=sgn*SGN(a);
            return ret ;
        }
        inline bignum&operator/=(const bignum&a)
        {
            bignum_t t ;
            div(t,num,a.num);
            memcpy (num,t,sizeof(bignum_t));
            sgn=(num[0]==1&&!num[1])?0:sgn*a.sgn ;
            return*this ;
        }
        inline bignum&operator/=(const int a)
        {
            int t ;
            div(num,ABS(a),t);
            sgn=(num[0]==1&&!num [1])?0:sgn*SGN(a);
            return*this ;
        }
        inline bignum operator/(const bignum&a)
        {
            bignum ret ;
            bignum_t t ;
            memcpy(t,num,sizeof(bignum_t));
            div(ret.num,t,a.num);
            ret.sgn=(ret.num[0]==1&&!ret.num[1])?0:sgn*a.sgn ;
            return ret ;
        }
        inline bignum operator/(const int a)
        {
            bignum ret ;
            int t ;
            memcpy(ret.num,num,sizeof(bignum_t));
            div(ret.num,ABS(a),t);
            ret.sgn=(ret.num[0]==1&&!ret.num[1])?0:sgn*SGN(a);
            return ret ;
        }
        inline bignum&operator%=(const bignum&a)
        {
            bignum_t t ;
            div(t,num,a.num);
            if(num[0]==1&&!num[1])sgn=0 ;
            return*this ;
        }
        inline int operator%=(const int a)
        {
            int t ;
            div(num,ABS(a),t);
            memset(num,0,sizeof (bignum_t));
            num[0]=1 ;
            add(num,t);
            return t ;
        }
        inline bignum operator%(const bignum&a)
        {
            bignum ret ;
            bignum_t t ;
            memcpy(ret.num,num,sizeof(bignum_t));
            div(t,ret.num,a.num);
            ret.sgn=(ret.num[0]==1&&!ret.num [1])?0:sgn ;
            return ret ;
        }
        inline int operator%(const int a)
        {
            bignum ret ;
            int t ;
            memcpy(ret.num,num,sizeof(bignum_t));
            div(ret.num,ABS(a),t);
            memset(ret.num,0,sizeof(bignum_t));
            ret.num[0]=1 ;
            add(ret.num,t);
            return t ;
        }
        inline bignum&operator++()
        {
            *this+=1 ;
            return*this ;
        }
        inline bignum&operator--()
        {
            *this-=1 ;
            return*this ;
        }
        ;
        inline int operator>(const bignum&a)
        {
            return sgn>0?(a.sgn>0?comp(num,a.num)>0:1):(sgn<0?(a.sgn<0?comp(num,a.num)<0:0):a.sgn<0);
        }
        inline int operator>(const int a)
        {
            return sgn>0?(a>0?comp(num,a)>0:1):(sgn<0?(a<0?comp(num,-a)<0:0):a<0);
        }
        inline int operator>=(const bignum&a)
        {
            return sgn>0?(a.sgn>0?comp(num,a.num)>=0:1):(sgn<0?(a.sgn<0?comp(num,a.num)<=0:0):a.sgn<=0);
        }
        inline int operator>=(const int a)
        {
            return sgn>0?(a>0?comp(num,a)>=0:1):(sgn<0?(a<0?comp(num,-a)<=0:0):a<=0);
        }
        inline int operator<(const bignum&a)
        {
            return sgn<0?(a.sgn<0?comp(num,a.num)>0:1):(sgn>0?(a.sgn>0?comp(num,a.num)<0:0):a.sgn>0);
        }
        inline int operator<(const int a)
        {
            return sgn<0?(a<0?comp(num,-a)>0:1):(sgn>0?(a>0?comp(num,a)<0:0):a>0);
        }
        inline int operator<=(const bignum&a)
        {
            return sgn<0?(a.sgn<0?comp(num,a.num)>=0:1):(sgn>0?(a.sgn>0?comp(num,a.num)<=0:0):a.sgn>=0);
        }
        inline int operator<=(const int a)
        {
            return sgn<0?(a<0?comp(num,-a)>=0:1):
            (sgn>0?(a>0?comp(num,a)<=0:0):a>=0);
        }
        inline int operator==(const bignum&a)
        {
            return(sgn==a.sgn)?!comp(num,a.num):0 ;
        }
        inline int operator==(const int a)
        {
            return(sgn*a>=0)?!comp(num,ABS(a)):0 ;
        }
        inline int operator!=(const bignum&a)
        {
            return(sgn==a.sgn)?comp(num,a.num):1 ;
        }
        inline int operator!=(const int a)
        {
            return(sgn*a>=0)?comp(num,ABS(a)):1 ;
        }
        inline int operator[](const int a)
        {
            return digit(num,a);
        }
    	friend inline istream&operator>>(istream&is,bignum&a)
        {
            read(a.num,a.sgn,is);
            return  is ;
        }
        friend inline ostream&operator<<(ostream&os,const bignum&a)
        {
            if(a.sgn<0)
    			os<<'-' ;
            write(a.num,os);
            return os ;
        }
    	
        friend inline bignum sqrt(const bignum&a)
        {
            bignum ret ;
            bignum_t t ;
            memcpy(t,a.num,sizeof(bignum_t));
            sqrt(ret.num,t);
            ret.sgn=ret.num[0]!=1||ret.num[1];
            return ret ;
        }
        friend inline bignum sqrt(const bignum&a,bignum&b)
        {
            bignum ret ;
            memcpy(b.num,a.num,sizeof(bignum_t));
            sqrt(ret.num,b.num);
            ret.sgn=ret.num[0]!=1||ret.num[1];
            b.sgn=b.num[0]!=1||ret.num[1];
            return ret ;
        }
        inline int length()
        {
            return :: length(num);
        }
        inline int zeronum()
        {
            return :: zeronum(num);
        }
        inline bignum C(const int m,const int n)
        {
            combination(num,m,n);
            sgn=1 ;
            return*this ;
        }
        inline bignum P(const int m,const int n)
        {
            permutation(num,m,n);
            sgn=1 ;
            return*this ;
        }
    };
    bignum GCD(bignum a,bignum b)
    {     
    	if(b==0)         
    		return a;     
    	return GCD(b,a%b);   
    }
    bignum x_gcd,y_gcd;
    bignum Extend_GCD(bignum a,bignum b)
    {
    	bignum t,d;
    	cout<<a<<endl<<b<<endl;
    	if(b==0)
    	{
    		x_gcd=1;
    		y_gcd=0;
    		return a;
    	}
    	printf("jin
    ");
    	bignum mid;
    	mid=a%b;
    	cout<<mid<<endl<<endl<<endl<<endl;
    	d=Extend_GCD(b,mid);
    	printf("chu
    ");
    	t=x_gcd;
    	x_gcd=y_gcd;
    	y_gcd=t-(a/b)*(y_gcd); 
        return d;
    }
    bignum niyuan(bignum a,bignum mod)
    {
    	bignum d,k,gcd;//d就是a的逆元
    	gcd=Extend_GCD(a,mod);
    	bignum mid;
    	mid=1;
    	d=x_gcd*(mid/gcd); 
    	k=d/(mod/gcd);
    	d=d-k*(mod/gcd);
    	if(d<0) d=d+mod/gcd;
    	return d;
    }
    /*=====================================================
    * Fermat定理:
    * 如果n是素数,那么对于所有的a<>0(mod n)有
    * a^(n-1) mod n = 1
    *=====================================================
    */
    // 输出a^m(mod n)
    bignum f(bignum a,bignum k,bignum m)//求  a^k%m
    {
    	// long f(long a,long k,long m) 
    	bignum b; 
    	b=1;
    	while(k>=1) 
    	{ 
    		if(k%2==1) b=a*b%m; 
    		a=a*a%m; 
    		k=k/2; 
    	} 
    	return b; 
    }
    /*=====================================================
    * 输入:正奇数>=5
    * 输出:如果n是素数,则返回prime;否则返回composite
    * 出错概率:
    * 对于4~2000的所有合数,仅对341,561,645,1105,1387,1729
    * 返回素数,此外,小于100,000的数中,仅有78个测试错误
    * 最大的是93961 = 7*31*433
    *=====================================================
    */
    bool primeTest1(bignum n) {
    	bignum mid;
    	mid=2;
    	if (f(mid, n-1, n) == 1) 
    		return true;
    	else
    		return false;
    };
    /*=====================================================
    * Carmicheal数: 
    * 它对于相对于n互素的正整数a,满足Fermat定理
    * Carmicheal数相当少,对于10^8内仅有255个。
    * 当一个合数n对于底a满足Fermat定理时
    * n被称为底a的伪素数,于是primeTest1在n是素数或者
    * 是底2的伪素数时返回素数
    *=====================================================
    */
    /*=====================================================
    * 改进方法:
    * 在2~n-2之间随机地选择底,这产生了算法primeTest2
    *=====================================================
    */
    bignum Rand(bignum n)
    {
    	int  mid=rand();
    	bignum q;
    	q=mid*1;
    	bignum p;
    	p=99999;
        return  f(p,q,n);
    }
    bool primeTest2(bignum n) {
    	// a是2~n-2之间的随机数
    	bignum mid=n-1;
    	bignum a;
    	//  a=1;
    	a =Rand(n); 
    	if (f(a, mid, n) == 1)
    		return true;
    	else
    		return false;
    };
    /*=====================================================
    * 如果n不是Carmicheal数,则算法PTEST2将测出n是合数
    * 的概率至少是1/2,换句话说primeTest2出错的概率最多
    * 是1/2。于是,通过反复测试k次,出错的概率最多是2^(-k) 
    *=====================================================
    */
    /*=====================================================
    * 设n为大于5的奇数,写为n-1=(2^q)*m,则由费马定理,
    * 序列a^m(mod n), a^(2m)(mod n), a^(4m)(mod n)
    * ... a^((2^q)*m)(mod n) 必定以1结束,而且在1出现之前
    * 的值必定是n-1,这是因为当n是素数时,x^2=1(mod n)
    * 的唯一解是x=1或x=-1
    *=====================================================
    */
    // t为循环检测次数
    bool primalityTest(bignum n, bignum t) {
    	if (n == 2 || n == 3) 
    		return true;
    	
    	if (n%2 == 0)
    		return false;
    	
    	bignum q, m;
    	q=0; m=n-1;
    	while (m%2 == 0) {
    		++ q;
    		m /= 2;
    	}
        bignum i;
    	for (i = 0; i < t; ++ i) {
    		bignum a;
    		a= Rand(n);
    		bignum x = f(a, m, n);
    		bignum j;
    		
    		if (x == 1)
    			continue;
    		
    		for (j = 0; j < q && x != n-1; ++ j) {
    			x = (x*x)%n;
    		}
    		
    		if (j >= q)
    			return false;
    	}
    	
    	return true;
    };
    
    int fun(bignum x,bignum y)
    //公钥 e 与 t 的互素判断
    {
    	bignum t;
    	while(y!=0)
    	{
    		t=x;
    		x=y;
    		y=t%y;
    	}
    	if(x == 1)
    		return 0;
    	//x 与 y 互素时返回 0
    	else
    		return 1;
    	//x 与 y 不互素时返回 1
    }
    bignum cifang(bignum a,bignum k) 
    {
    	bignum b; 
    	b=1;
    	while(k>=1) 
    	{ 
    		if(k%2==1) b=a*b;
    		a=a*a;
    		k=k/2; 
    	} 
    	return b; 
    }
    
    bignum extended_euclidean(bignum n, bignum m, bignum &x, bignum &y)   //扩展的欧几里德非递归算法  
    {  
    	bignum x1, x2, x3;  
    	x1=1;x3=n;
    	
        bignum y1, y2, y3; 
    	y2=1;y3=m;
    	bignum zero;
    	zero=0;
        while(x3 % y3 != zero)  
        {  
    		bignum d = x3 / y3;  
    		bignum t1, t2, t3;  
            t1 = x1 - d * y1;  
            t2 = x2 - d * y2;  
            t3 = x3 - d * y3;  
            x1 = y1; x2 = y2; x3 = y3;  
            y1 = t1; y2 = t2; y3 = t3;  
        }  
        x = y1; y = y2;  
        return y3;  
    } 
    
    int main()
    {    
    	// cout<<"加法:"<<a+b<<endl;
    	// cout<<"减法:"<<a-b<<endl;
    	// cout<<"乘法:"<<a*b<<endl;
    	// cout<<"除法:"<<a/b<<endl; 
    	// cout<<"求余:"<<a%b<<endl;
    	//   cout<<"幂模:"<<f(a,b,mod)<<endl;
    	// cout<<"欧几里德:"<<GCD(a,b)<<endl;
    	/* cout<<"求a的逆元:"<<niyuan(a,mod)<<endl;//(a*c)%mod=1;
    	if(primalityTest(a,c))//判断a是否是一个素数  检查c次
    	printf("n is a prime
    ");
    	else 
    	printf("n is not a prime
    ");
    	*/
    	//freopen("haha.txt","w",stdout);
        system("color 2e");  
    	printf("			  *******RSA密码系统*******
    ");
    	bignum  p,q,e,d,m,n,t,c;
    	int r;
    	/* printf("请输入两个素数 p,q: ");//可以自行确定素数  
    	cin>>p>>q;
    	printf("请输入公钥 e: ");
    	cin>>e;   
    	*/
    	/////////////////////我们自己选取3个连续的梅森素数127 521 607 进行测试
    	bignum a,b;
    	a=2;b=127;
    	p=cifang(a,b)-1;
    	b=521;
    	q=cifang(a,b)-1;
    	b=607;   
    	e=cifang(a,b)-1;
    	/////////////////////////
    	//cout<<"e="<<e<<endl;
    	n=p*q;
    	t=(p-1)*(q-1); 
    	//求 n 的欧拉数
    	
    	if(e<1||e>t||fun(e,t))
    	{
    		printf("e 不合要求,请重新输入: ");
    		cin>>e;
    	} 
    	bignum gcd,k,mid;
    	// x_gcd,y_gcd
    	gcd=extended_euclidean(e,t,x_gcd,y_gcd);
    	mid=1;
    	d=x_gcd*(mid/gcd);  
    	k=d/(t/gcd);  
    	d=d-k*(t/gcd);  
    	if(d<0) d=d+t/gcd;  
    //	cout<<"d="<<d<<endl; d为逆元
    	while(1)
    	{
    		printf("加密请输入1  解密请输入2  退出请输入0
    ");
    		scanf("%d",&r);
    		switch(r)
    		{
    			system("cls");
    		case 1: printf("请输入明文 m: ");
    			cin>>m;
    			c=f(m,e,n);
    			printf("密文为
    ");
    			cout<<c<<endl;
    			break;
    		case 2: printf("请输入密文 c: ");
    			cin>>c;
    			m=f(c,d,n);
    			printf("明文为
    ");
                cout<<m<<endl;
    			break;
    		case 0: return 0; 
    		default:printf("输入无效请从新输入.
    ");break;
    		}
    	}
    	return 0 ;
    }
    
    ///http://blog.csdn.net/lishuhuakai/article/details/9104959
    ///扩展欧几里德 非递归


    参考了  上面的博客 发现了 扩展欧几里得的非递归用法   之前用递归的 总是出错 不知道原因

  • 相关阅读:
    C# 如何在PDF文档中创建表格
    C# 如何创建Excel多级分组
    C# 添加、修改以及删除Excel迷你图表的方法
    C# 创建EXCEL图表并保存为图片
    【BZOJ5287】[HNOI2018]毒瘤(动态规划,容斥)
    【BZOJ5250】[九省联考2018]秘密袭击(动态规划)
    【BZOJ5213】[ZJOI2018]迷宫(神仙题)
    CodeForces Global Round 1
    【BZOJ5212】[ZJOI2018]历史(Link-Cut Tree)
    【BZOJ5211】[ZJOI2018]线图(树哈希,动态规划)
  • 原文地址:https://www.cnblogs.com/snake-hand/p/3146947.html
Copyright © 2011-2022 走看看