四種加密算法之RSA源代碼-C++

RSA.h文件:

  1. //! RSA 動態鏈接庫實現   H文件  
  2. /*! 
  3.  @author 朱孟斌 
  4.  @e-mail  [email protected] 
  5.  @version 1.0 
  6.  @date 2011-03 
  7.  @{ 
  8. */  
  9. #ifndef RSA_H  
  10. #define RSA_H  
  11.   
  12. #include <stdio.h>  
  13. #include <string>  
  14. #include <stdlib.h>     
  15. #include <time.h>    
  16. #include <math.h>  
  17. #include <malloc.h>  
  18. #include <iostream>  
  19. #include <ppl.h>  
  20.   
  21. using namespace std;  
  22. using namespace Concurrency;  
  23. //! MAX是數組的最大個數,LEN爲結構體slink的佔用內存空間大小 */  
  24. #define MAX 100  
  25. #define LEN sizeof(struct slink)  
  26. //!   #能夠進行動態鏈接庫編譯的RSA類  
  27.  /*!   
  28.        @see class _declspec(dllexport) RSA 
  29.        將RSA算法寫成動態鏈接庫的形式方便調用,其中有公鑰,私鑰和明文 
  30.        就可以進行明文的加密和解密 
  31.  */  
  32. class _declspec(dllexport) RSA  
  33. {  
  34. public:  
  35.   
  36.     //! # 新定義的數據結構slink  
  37.     /*!   
  38.      @see struct slink 
  39.      數據結構中,bignum存儲的是隨機生成的大數,next是指向後面的指針 
  40.      @see int bignum[MAX] 
  41.      */  
  42.     typedef struct slink  
  43.     {  
  44.         int  bignum[MAX];/*!< bignum[98]用來標記正負號,1正,0負bignum[99]來標記實際長度*/  
  45.         struct slink *next;  
  46.     }slink;  
  47.   
  48. public:  
  49.     //! #RSA 的構造函數  
  50.     /*! 
  51.      @see RSA() 
  52.       其中應該對RSA類中的一些變量進行相應的初始化 
  53.     */  
  54.     RSA();  
  55.     //! #RSA的析構函數  
  56.     /*! 
  57.     @see ~RSA() 
  58.       釋放內存 
  59.     */  
  60.     ~RSA();  
  61.   
  62. public:  
  63.     //! #RSA的大數運算函數庫  
  64.   
  65.     /** @大數比較函數 
  66.     @see int cmp(int, int) 
  67.     */  
  68.     int cmp(int a1[MAX],int a2[MAX]);  
  69.     /** @大數類型轉換函數 
  70.     @see void mov(int a[MAX],int *b); 
  71.     */  
  72.     void mov(int a[MAX],int *b);  
  73.      /** @大數乘積函數 
  74.      @see void mul(int a1[MAX],int a2[MAX],int *c); 
  75.      */  
  76.     void mul(int a1[MAX],int a2[MAX],int *c);  
  77.      /** @大數相加函數 
  78.      @see void add(int a1[MAX],int a2[MAX],int *c); 
  79.      */  
  80.     void add(int a1[MAX],int a2[MAX],int *c);  
  81.      /** @大數大數相減函數 
  82.      @see  void sub(int a1[MAX],int a2[MAX],int *c); 
  83.      */  
  84.     void sub(int a1[MAX],int a2[MAX],int *c);  
  85.      /*! @大數取模函數 
  86.      @see void mod(int a[MAX],int b[MAX],int  *c); 
  87.      @attention /c=a mod b//注意:經檢驗知道此處A和C的數組都改變了。 
  88.      */  
  89.     void mod(int a[MAX],int b[MAX],int  *c);  
  90.     /*! @大數相除函數 
  91.     @see void divt(int t[MAX],int b[MAX],int  *c ,int *w); 
  92.     @attention //試商法//調用以後w爲a mod b, C爲a  div b; 
  93.     */  
  94.     void divt(int t[MAX],int b[MAX],int  *c ,int *w);  
  95.     /*! @解決 了 m=a*b mod n; 
  96.     /*! 
  97.     @see void mulmod(int a[MAX] ,int b[MAX] ,int n[MAX],int *m); 
  98.     */  
  99.     void mulmod(int a[MAX] ,int b[MAX] ,int n[MAX],int *m);  
  100.     /*! @解決 m=a^p  mod n的函數問題 
  101.     /*! 
  102.     @see void expmod(int a[MAX] ,int p[MAX] ,int n[MAX],int *m); 
  103.     */  
  104.     void expmod(int a[MAX] ,int p[MAX] ,int n[MAX],int *m);  
  105.     /*!  @判斷是否爲素數 
  106.     @see int   is_prime_san(int p[MAX] ); 
  107.     */  
  108.     int   is_prime_san(int p[MAX] );  
  109.     /*! @判斷兩個大數之間是否互質 
  110.     @see int coprime(int e[MAX],int s[MAX]); 
  111.     */  
  112.     int coprime(int e[MAX],int s[MAX]);  
  113.     /*!  @隨機產生素數 
  114.     @see void prime_random(int *p,int *q); 
  115.     */  
  116.     void prime_random(int *p,int *q);  
  117.     /*! @產生素數e 
  118.     @see void erand(int e[MAX],int m[MAX]); 
  119.     */  
  120.     void erand(int e[MAX],int m[MAX]);  
  121.     /*! @根據規則產生其他的數 
  122.     @see void rsad(int e[MAX],int g[MAX],int *d); 
  123.     */  
  124.     void rsad(int e[MAX],int g[MAX],int *d);  
  125.     /*! @求解密密鑰d的函數(根據Euclid算法) 
  126.     @see unsigned long  rsa(unsigned long p,unsigned long q,unsigned long e); 
  127.     */  
  128.     unsigned long  rsa(unsigned long p,unsigned long q,unsigned long e);  
  129.       
  130.    //! #RSA的產生大數的公鑰和私鑰的函數  
  131.     /*! 
  132.       @see bool RSAKey(); 
  133.       @param 沒有任何輸入, 
  134.       @param 隨機產生e,d,n的函數,其運行時間比較長,需要等待 
  135.       @return[bool] 成功返回true,失敗返回false 
  136.     */  
  137.     bool RSAKey();  
  138.   
  139.     //!  #RSA的進行文件加密的函數  
  140.     /*! 
  141.       @see string tencrypto(int e[MAX], int n[MAX], char* text); 
  142.       @param[int[] e,n爲隨機產生的公鑰,利用公鑰進行加密 
  143.       @param[char* text爲明文,明文以char*的格式存儲 
  144.       @return[string] 返回值爲加密成功之後的密文,採用string類型進行存儲 
  145.     */  
  146.     string tencrypto(int e[MAX], int n[MAX], char* text);  
  147.   
  148.     //! #RSA的進行文件解密的函數  
  149.     /*! 
  150.       @see string tdecrypto(int d[MAX], int n[MAX], string text); 
  151.       @param[int[] d,n爲私鑰,由函數RSAKey()產生 
  152.       @param[string text爲密文,對應加密函數,密文的格式爲string 
  153.       @return[string] 解密之後的明文采用string進行存儲 
  154.     */  
  155.     string tdecrypto(int d[MAX], int n[MAX], string text);  
  156.   
  157. public:  
  158.     /** @brief 定義的全局變量,存儲密鑰 */  
  159.     int  p[MAX],q[MAX],n[MAX],d[MAX],e[MAX],m[MAX],p1[MAX],q1[MAX];  
  160.   
  161. private:  
  162.     int  i;  
  163.     char  c;  
  164.     //struct slink *head,*h1,*h2;  
  165. };  
  166.   
  167. #endif // RSA_H  
RSA.c文件:

  1. /*! 
  2. * @ RSA 動態鏈接庫實現   CPP文件 
  3. * @author 朱孟斌 
  4. * @e-mail  [email protected] 
  5. * @version 1.0 
  6. * @date 2011-03 
  7. * @{ 
  8. */  
  9. #include "RSA.h"  
  10.   
  11. RSA::RSA()  
  12. {  
  13.   
  14. }  
  15.   
  16. RSA::~RSA()  
  17. {  
  18.   
  19. }  
  20.   
  21. /*----------------------------創建自己的大數運算庫---------------------------------*/  
  22. int RSA::cmp(int a1[MAX],int a2[MAX])  
  23. {  
  24.     int l1, l2;  
  25.     int i;  
  26.     l1=a1[99];  
  27.     l2=a2[99];  
  28.     if (l1>l2)  
  29.         return 1;  
  30.     if (l1<l2)  
  31.         return -1;  
  32.     for(i=(l1-1);i>=0;i--)  
  33.     {  
  34.         if (a1[i]>a2[i])  
  35.             return 1 ;  
  36.         if (a1[i]<a2[i])  
  37.             return -1;  
  38.     }  
  39.     return 0;  
  40. }  
  41.   
  42. void RSA::mov(int a[MAX],int *b)  
  43. {  
  44.     int j;  
  45.     for(j=0;j<MAX;j++)  
  46.         b[j]=a[j];  
  47.     return ;  
  48. }  
  49.   
  50. void RSA::mul(int a1[MAX],int a2[MAX],int *c)  
  51. {  
  52.     int i,j;  
  53.     int y;  
  54.     int x;  
  55.     int z;  
  56.     int w;  
  57.     int l1, l2;  
  58.     l1=a1[MAX-1];  
  59.     l2=a2[MAX-1];  
  60.     if (a1[MAX-2]=='-'&& a2[MAX-2]=='-')  
  61.         c[MAX-2]=0;  
  62.     else if (a1[MAX-2]=='-')  
  63.         c[MAX-2]='-';  
  64.     else if (a2[MAX-2]=='-')  
  65.         c[MAX-2]='-';  
  66.     for(i=0;i<l1;i++)  
  67.     {  
  68.         for(j=0;j<l2;j++)  
  69.         {  
  70.             x=a1[i]*a2[j];  
  71.             y=x/10;  
  72.             z=x%10;  
  73.             w=i+j;  
  74.             c[w]=c[w]+z;  
  75.             c[w+1]=c[w+1]+y+c[w]/10;  
  76.             c[w]=c[w]%10;  
  77.         }  
  78.     }  
  79.     w=l1+l2;  
  80.     if(c[w-1]==0)w=w-1;  
  81.     c[MAX-1]=w;  
  82.     return;  
  83. }   
  84.   
  85. void RSA::add(int a1[MAX],int a2[MAX],int *c)  
  86. {  
  87.     int i,l1,l2;  
  88.     int len,temp[MAX];  
  89.     int k=0;  
  90.     l1=a1[MAX-1];  
  91.     l2=a2[MAX-1];  
  92.     if((a1[MAX-2]=='-')&&(a2[MAX-2]=='-'))  
  93.     {  
  94.         c[MAX-2]='-';  
  95.     }  
  96.     else if (a1[MAX-2]=='-')  
  97.     {  
  98.         mov(a1,temp);  
  99.         temp[MAX-2]=0;  
  100.         sub(a2,temp,c);  
  101.         return;  
  102.     }  
  103.     else if (a2[MAX-2]=='-')  
  104.     {  
  105.         mov(a2,temp);  
  106.         temp[98]=0;  
  107.         sub(a1,temp,c);  
  108.         return;  
  109.     }  
  110.   
  111.     if(l1<l2)len=l1;  
  112.     else len=l2;  
  113.     for(i=0;i<len;i++)  
  114.     {  
  115.         c[i]=(a1[i]+a2[i]+k)%10;  
  116.         k=(a1[i]+a2[i]+k)/10;  
  117.     }  
  118.     if(l1>len)  
  119.     {  
  120.         for(i=len;i<l1;i++)  
  121.         {  
  122.             c[i]=(a1[i]+k)%10;  
  123.             k=(a1[i]+k)/10;     
  124.         }  
  125.         if(k!=0)  
  126.         {  
  127.             c[l1]=k;  
  128.             len=l1+1;  
  129.         }  
  130.         else len=l1;  
  131.     }  
  132.     else  
  133.     {  
  134.         for(i=len;i<l2;i++)  
  135.         {  
  136.             c[i]=(a2[i]+k)%10;  
  137.             k=(a2[i]+k)/10;     
  138.         }  
  139.         if(k!=0)  
  140.         {  
  141.             c[l2]=k;  
  142.             len=l2+1;  
  143.         }  
  144.         else len=l2;  
  145.     }  
  146.     c[99]=len;  
  147.     return;  
  148. }   
  149.   
  150. void RSA::sub(int a1[MAX],int a2[MAX],int *c)  
  151. {  
  152.     int i,l1,l2;  
  153.     int len,t1[MAX],t2[MAX];  
  154.     int k=0;  
  155.     l1=a1[MAX-1];  
  156.     l2=a2[MAX-1];  
  157.     if ((a1[MAX-2]=='-') && (a2[MAX-2]=='-'))  
  158.     {  
  159.         mov(a1,t1);  
  160.         mov(a2,t2);  
  161.         t1[MAX-2]=0;  
  162.         t2[MAX-2]=0;  
  163.         sub(t2,t1,c);  
  164.         return;  
  165.     }  
  166.     else if( a2[MAX-2]=='-')  
  167.     {  
  168.         mov(a2,t2);  
  169.         t2[MAX-2]=0;  
  170.         add(a1,t2,c);  
  171.         return;  
  172.     }  
  173.     else if (a1[MAX-2]=='-')  
  174.     {  
  175.         mov(a2,t2);  
  176.         t2[MAX-2]='-';  
  177.         add(a1,t2,c);  
  178.         return;  
  179.     }  
  180.     if(cmp(a1,a2)==1)  
  181.     {  
  182.         len=l2;  
  183.         for(i=0;i<len;i++)  
  184.         {  
  185.             if ((a1[i]-k-a2[i])<0)  
  186.             {  
  187.                 c[i]=(a1[i]-a2[i]-k+10)%10;  
  188.                 k=1;  
  189.             }  
  190.             else   
  191.             {  
  192.                 c[i]=(a1[i]-a2[i]-k)%10;  
  193.                 k=0;  
  194.             }  
  195.         }  
  196.         for(i=len;i<l1;i++)  
  197.         {  
  198.             if ((a1[i]-k)<0)  
  199.             {  
  200.                 c[i]=(a1[i]-k+10)%10;  
  201.                 k=1;  
  202.             }  
  203.             else   
  204.             {  
  205.                 c[i]=(a1[i]-k)%10;  
  206.                 k=0;  
  207.             }   
  208.         }  
  209.         if(c[l1-1]==0)/*使得數組C中的前面所以0字符不顯示了,如1000-20=0980--->顯示爲980了*/  
  210.         {  
  211.             len=l1-1;  
  212.             i=2;  
  213.             while (c[l1-i]==0)/*111456-111450=00006,消除0後變成了6;*/  
  214.             {  
  215.                 len=l1-i;  
  216.                 i++;  
  217.             }  
  218.         }  
  219.         else   
  220.         {  
  221.             len=l1;  
  222.         }  
  223.     }  
  224.     else  
  225.         if(cmp(a1,a2)==(-1))  
  226.         {  
  227.             c[MAX-2]='-';  
  228.             len=l1;  
  229.             for(i=0;i<len;i++)  
  230.             {  
  231.                 if ((a2[i]-k-a1[i])<0)  
  232.                 {  
  233.                     c[i]=(a2[i]-a1[i]-k+10)%10;  
  234.                     k=1;  
  235.                 }  
  236.                 else   
  237.                 {  
  238.                     c[i]=(a2[i]-a1[i]-k)%10;  
  239.                     k=0;  
  240.                 }  
  241.             }  
  242.             for(i=len;i<l2;i++)  
  243.             {  
  244.                 if ((a2[i]-k)<0)  
  245.                 {  
  246.                     c[i]=(a2[i]-k+10)%10;  
  247.                     k=1;  
  248.                 }  
  249.                 else   
  250.                 {  
  251.                     c[i]=(a2[i]-k)%10;  
  252.                     k=0;  
  253.                 }     
  254.             }  
  255.             if(c[l2-1]==0)  
  256.             {    
  257.                 len=l2-1;  
  258.                 i=2;  
  259.                 while (c[l1-i]==0)  
  260.                 {  
  261.                     len=l1-i;  
  262.                     i++;  
  263.                 }  
  264.             }  
  265.             else len=l2;  
  266.         }  
  267.         else if(cmp(a1,a2)==0)  
  268.         {  
  269.             len=1;  
  270.             c[len-1]=0;  
  271.         }  
  272.         c[MAX-1]=len;  
  273.         return;  
  274. }  
  275.   
  276. void  RSA::mod(int a[MAX],int b[MAX],int  *c)/*/c=a mod b//注意:經檢驗知道此處A和C的數組都改變了。*/  
  277. {     
  278.     int d[MAX];  
  279.     mov (a,d);  
  280.     while (cmp(d,b)!=(-1))/*/c=a-b-b-b-b-b.......until(c<b)*/  
  281.     {  
  282.         sub(d,b,c);  
  283.         mov(c,d);/*/c複製給a*/  
  284.     }     
  285.     return ;  
  286. }  
  287.   
  288. void  RSA::divt(int t[MAX],int b[MAX],int  *c ,int *w)/*//試商法//調用以後w爲a mod b, C爲a  div b;*/  
  289. {  
  290.   
  291.     int a1,b1,i,j,m;/*w用於暫時保存數據*/  
  292.     int d[MAX],e[MAX],f[MAX],g[MAX],a[MAX];  
  293.     mov(t,a);  
  294.     for(i=0;i<MAX;i++)  
  295.         e[i]=0;  
  296.     for(i=0;i<MAX;i++)  
  297.         d[i]=0;  
  298.     for(i=0;i<MAX;i++) g[i]=0;  
  299.     a1=a[MAX-1];  
  300.     b1=b[MAX-1];  
  301.     if (cmp(a,b)==(-1))  
  302.     {  
  303.         c[0]=0;  
  304.         c[MAX-1]=1;  
  305.         mov(t,w);  
  306.         return;  
  307.     }  
  308.     else if (cmp(a,b)==0)  
  309.     {  
  310.         c[0]=1;  
  311.         c[MAX-1]=1;  
  312.         w[0]=0;  
  313.         w[MAX-1]=1;  
  314.         return;  
  315.     }  
  316.     m=(a1-b1);  
  317.     for(i=m;i>=0;i--)/*341245/3=341245-300000*1--->41245-30000*1--->11245-3000*3--->2245-300*7--->145-30*4=25--->25-3*8=1*/  
  318.     {  
  319.         for(j=0;j<MAX;j++)  
  320.             d[j]=0;  
  321.         d[i]=1;  
  322.         d[MAX-1]=i+1;  
  323.         mov(b,g);  
  324.         mul(g,d,e);  
  325.         while (cmp(a,e)!=(-1))  
  326.         {  
  327.             c[i]++;  
  328.             sub(a,e,f);  
  329.             mov(f,a);/*f複製給g*/  
  330.         }  
  331.         for(j=i;j<MAX;j++)/*高位清零*/  
  332.             e[j]=0;  
  333.     }  
  334.     mov(a,w);  
  335.     if (c[m]==0) c[MAX-1]=m;  
  336.     else c[MAX-1]=m+1;  
  337.     return;  
  338. }  
  339.   
  340. void RSA::mulmod(int a[MAX] ,int b[MAX] ,int n[MAX],int *m)/*解決 了 m=a*b mod n;*/  
  341. {  
  342.     int c[MAX],d[MAX];  
  343.     int i;  
  344.     for(i=0;i<MAX;i++)  
  345.         d[i]=c[i]=0;  
  346.     mul(a,b,c);  
  347.     divt(c,n, d,m);  
  348.     //for(i=0;i<m[MAX-1];i++)  
  349.     //  printf("%d",m[m[MAX-1]-i-1]);  
  350.     //printf("\nm  length is :  %d \n",m[MAX-1]);  
  351. }  
  352. /*-------------接下來的重點任務是要着手解決 m=a^p  mod n的函數問題------------*/  
  353. void RSA::expmod(int a[MAX] ,int p[MAX] ,int n[MAX],int *m)  
  354. {  
  355.     int t[MAX],l[MAX],temp[MAX]; /*/t放入2,l放入1;*/  
  356.     int w[MAX],s[MAX],c[MAX],b[MAX],i;  
  357.     for(i=0;i<MAX-1;i++)  
  358.         b[i]=l[i]=t[i]=w[i]=0;  
  359.     t[0]=2;t[MAX-1]=1;  
  360.     l[0]=1;l[MAX-1]=1;  
  361.     mov(l,temp);  
  362.     mov(a,m);  
  363.     mov(p,b);  
  364.     while(cmp(b,l)!=0)  
  365.     {  
  366.         for(i=0;i<MAX;i++)  
  367.             w[i]=c[i]=0;  
  368.         divt(b,t,w,c);/*// c=p mod 2  w= p /2*/  
  369.         mov(w,b);/*//p=p/2*/  
  370.         if(cmp(c,l)==0) /*/餘數c==1*/  
  371.         {  
  372.             for(i=0;i<MAX;i++)  
  373.                 w[i]=0;  
  374.             mul(temp,m,w);  
  375.             mov(w,temp);  
  376.             for(i=0;i<MAX;i++)  
  377.                 w[i]=c[i]=0;  
  378.             divt(temp,n,w,c);/* /c爲餘c=temp % n,w爲商w=temp/n */  
  379.             mov(c,temp);  
  380.         }  
  381.         for(i=0;i<MAX;i++)  
  382.             s[i]=0;  
  383.         mul(m,m,s);//s=a*a  
  384.         for(i=0;i<MAX;i++)  
  385.             c[i]=0;  
  386.         divt(s,n,w,c);/*/w=s/n;c=s mod n*/  
  387.         mov (c,m);  
  388.     }  
  389.     for(i=0;i<MAX;i++)  
  390.         s[i]=0;  
  391.     mul(m,temp,s);  
  392.     for(i=0;i<MAX;i++)  
  393.         c[i]=0;  
  394.     divt(s,n,w,c);  
  395.     mov (c,m);/*餘數s給m*/  
  396.     m[MAX-2]=a[MAX-2];/*爲後面的漢字顯示需要,用第99位做爲標記*/  
  397.     return;/*/k=temp*k%n;*/  
  398. }  
  399.   
  400. int   RSA::is_prime_san(int p[MAX] )  
  401. {  
  402.     int i,a[MAX],t[MAX],s[MAX],o[MAX];   
  403.     for(i=0;i<MAX;i++)  
  404.         s[i]=o[i]=a[i]=t[i]=0;  
  405.     t[0]=1;  
  406.     t[MAX-1]=1;  
  407.     a[0]=2;// { 2,3,5,7 }  
  408.     a[MAX-1]=1;  
  409.     sub(p,t,s);  
  410.     expmod ( a, s, p ,o);  
  411.     if ( cmp(o,t) != 0 )   
  412.     {  
  413.         return 0;  
  414.     }  
  415.     a[0]=3;  
  416.     for(i=0;i<MAX;i++)  o[i]=0;  
  417.     expmod ( a, s, p ,o);  
  418.     if ( cmp(o,t) != 0 )         
  419.     {  
  420.         return 0;  
  421.     }  
  422.     a[0]=5;  
  423.     for(i=0;i<MAX;i++)  o[i]=0;  
  424.     expmod ( a, s, p ,o);  
  425.     if ( cmp(o,t) != 0 )   
  426.     {        
  427.         return 0;  
  428.     }  
  429.     a[0]=7;  
  430.     for(i=0;i<MAX;i++)  o[i]=0;  
  431.     expmod ( a, s, p ,o);  
  432.     if ( cmp(o,t) != 0 )   
  433.     {  
  434.         return 0;  
  435.     }  
  436.     return 1;  
  437. }  
  438.   
  439. int RSA::coprime(int e[MAX],int s[MAX]) /*//// 判斷兩個大數之間是否互質////*/  
  440. {  
  441.     int a[MAX],b[MAX],c[MAX],d[MAX],o[MAX],l[MAX];  
  442.     int i;  
  443.     for(i=0;i<MAX;i++)  
  444.         l[i]=o[i]=c[i]=d[i]=0;  
  445.     o[0]=0;o[MAX-1]=1;  
  446.     l[0]=1;l[MAX-1]=1;  
  447.     mov(e,b);  
  448.     mov(s,a);  
  449.     do  
  450.     {  
  451.         if(cmp(b,l)==0)  
  452.         {  
  453.             return 1;  
  454.         }  
  455.         for(i=0;i<MAX;i++)  
  456.             c[i]=0;  
  457.         divt(a,b,d,c);  
  458.         mov(b,a);/*b--->a*/  
  459.         mov(c,b);/*c--->b*/  
  460.     }while(cmp(c,o)!=0);  
  461.     /*  printf("Ihey are  not coprime!\n");*/  
  462.     return 0;  
  463. }  
  464.   
  465. void RSA::prime_random(int *p,int *q)  
  466. {  
  467.     int i,k;  
  468.     time_t t;   
  469.     p[0]=1;  
  470.     q[0]=3;  
  471.     p[MAX-1]=10;  
  472.     q[MAX-1]=11;  
  473.     do  
  474.     {  
  475.         t=time(NULL);  
  476.         srand((unsigned long)t);  
  477.         for(i=1;i<p[MAX-1]-1;i++)  
  478.         {  
  479.             k=rand()%10;  
  480.             p[i]=k;  
  481.         }  
  482.         k=rand()%10;  
  483.         while (k==0)  
  484.         {  
  485.             k=rand()%10;  
  486.         }  
  487.         p[p[MAX-1]-1]=k;  
  488.     }while((is_prime_san(p))!=1);  
  489.     do  
  490.     {  
  491.         t=time(NULL);  
  492.         srand((unsigned long)t);  
  493.         for(i=1;i<q[MAX-1];i++)  
  494.         {  
  495.             k=rand()%10;  
  496.             q[i]=k;  
  497.         }  
  498.     }while((is_prime_san(q))!=1);  
  499.     return;  
  500. }  
  501.   
  502. void  RSA::erand(int e[MAX],int m[MAX])  
  503. {  
  504.     int i,k;  
  505.     time_t t;  
  506.     e[MAX-1]=5;  
  507.     do  
  508.     {  
  509.         t=time(NULL);  
  510.         srand((unsigned long)t);  
  511.         for(i=0;i<e[MAX-1]-1;i++)  
  512.         {  
  513.             k=rand()%10;  
  514.             e[i]=k;  
  515.         }  
  516.         while((k=rand()%10)==0)  
  517.             k=rand()%10;  
  518.         e[e[MAX-1]-1]=k;  
  519.     }while(coprime( e, m)!=1);  
  520.     return ;  
  521. }  
  522.   
  523. void RSA::rsad(int e[MAX],int g[MAX],int *d)  
  524. {  
  525.     int r[MAX],n1[MAX],n2[MAX],k[MAX],w[MAX];  
  526.     int i,t[MAX],b1[MAX],b2[MAX],temp[MAX];  
  527.     mov(g,n1);  
  528.     mov(e,n2);  
  529.     for(i=0;i<MAX;i++)  
  530.         k[i]=w[i]=r[i]=temp[i]=b1[i]=b2[i]=t[i]=0;  
  531.     b1[MAX-1]=0;b1[0]=0;/*/b1=0;*/  
  532.     b2[MAX-1]=1;b2[0]=1;/*/b2=1;*/  
  533.     while(1)  
  534.     {  
  535.         for(i=0;i<MAX;i++)  
  536.             k[i]=w[i]=0;  
  537.         divt(n1,n2,k,w);/*/k=n1/n2;*/  
  538.         for(i=0;i<MAX;i++)  
  539.             temp[i]=0;  
  540.         mul(k,n2,temp);/*/temp=k*n2;*/  
  541.         for(i=0;i<MAX;i++)  
  542.             r[i]=0;  
  543.         sub(n1,temp,r);  
  544.         if((r[MAX-1]==1) && (r[0]==0))/*/r=0*/  
  545.         {  
  546.             break;  
  547.         }  
  548.         else  
  549.         {  
  550.             mov(n2,n1);/*/n1=n2;*/  
  551.             mov( r,n2);/*/n2=r;*/  
  552.             mov(b2, t);/*/t=b2;*/  
  553.             for(i=0;i<MAX;i++)  
  554.                 temp[i]=0;  
  555.             mul(k,b2,temp);/*/b2=b1-k*b2;*/  
  556.             for(i=0;i<MAX;i++)  
  557.                 b2[i]=0;  
  558.             sub(b1,temp,b2);  
  559.             mov(t,b1);  
  560.         }  
  561.     }  
  562.     for(i=0;i<MAX;i++)  
  563.         t[i]=0;  
  564.     add(b2,g,t);  
  565.     for(i=0;i<MAX;i++)  
  566.         temp[i]=d[i]=0;  
  567.     divt(t,g,temp,d);  
  568. }  
  569.   
  570. /*/求解密密鑰d的函數(根據Euclid算法)96403770511368768000*/  
  571. unsigned long  RSA::rsa(unsigned long p,unsigned long q,unsigned long e)  /*/求解密密鑰d的函數(根據Euclid算法)*/  
  572. {  
  573.     unsigned long g,k,r,n1,n2,t;  
  574.     unsigned long b1=0,b2=1;  
  575.     g=(p-1)*(q-1);  
  576.     n1=g;  
  577.     n2=e;  
  578.     while(1)  
  579.     {  
  580.         k=n1/n2;  
  581.         r=n1-k*n2;  
  582.         if(r!=0)  
  583.         {  
  584.             n1=n2;  
  585.             n2=r;  
  586.             t=b2;  
  587.             b2=b1-k*b2;  
  588.             b1=t;  
  589.         }  
  590.         else  
  591.         {  
  592.             break;  
  593.         }  
  594.     }  
  595.     return (g+b2)%g;  
  596. }  
  597.   
  598. /////////////////////////////////////////////////////////////////////////////////////////  
  599. /////////////////////////////////////////////////////////////////////////////////////////  
  600. //! RSA的進行文件加密的函數  
  601. /*! 
  602.    * @param[in] e,n爲隨機產生的公鑰,利用公鑰進行加密 
  603.    * @param[in] text爲明文,明文以char*的格式存儲 
  604.    * @return[string] 返回值爲加密成功之後的密文,採用string類型進行存儲 
  605.    * @pre \e 進行加密的過程中進行了數據類型的轉換 
  606.    * @see e,d,n & text 
  607. */  
  608. string  RSA::tencrypto(int e[MAX], int n[MAX], char* text)/*//對有需要的文件進行加密*/  
  609. {  
  610.     int i,k,count,temp,c;  
  611.     char ch;  
  612.     struct slink  *p,*p1,*p2;  
  613.     struct slink  *h;  
  614.     h=p=p1=p2=(struct slink * )malloc(LEN);  
  615.     h=NULL;  
  616.     if (text == NULL)  
  617.     {  
  618.         return NULL;  
  619.     }  
  620.     count=0;  
  621.     int j;  
  622.     for (j = 0 ; j < strlen(text); j++)  
  623.     {    
  624.         ch = text[j];  
  625.         c=ch;  
  626.         k=0;  
  627.         if(c<0)  
  628.         {  
  629.             c=abs(c);/*/把負數取正並且做一個標記*/  
  630.             p1->bignum[MAX-2]='0';  
  631.         }  
  632.         else  
  633.         {  
  634.             p1->bignum[MAX-2]='1';  
  635.         }  
  636.   
  637.         while(c/10!=0)  
  638.         {  
  639.             temp=c%10;  
  640.             c=c/10;  
  641.             p1->bignum[k]=temp;  
  642.             k++;  
  643.         }  
  644.         p1->bignum[k]=c;  
  645.         p1->bignum[MAX-1]=k+1;  
  646.         count=count+1;  
  647.         if(count==1)  
  648.             h=p1;  
  649.         else p2->next=p1;  
  650.         p2=p1;  
  651.         p1=(struct slink * )malloc(LEN);  
  652.     }  
  653.     p2->next=NULL;   
  654.   
  655.     string res;  
  656.     p=p1=(struct slink * )malloc(LEN);  
  657.     p=h;  
  658.     if(h!=NULL)  
  659.         do   
  660.         {   
  661.             expmod( p->bignum , e ,n ,p1->bignum);  
  662.             ch=p1->bignum[MAX-2];  
  663.             res += ch;  
  664.             if ((p1->bignum[MAX-1]/10) ==0)/*/判斷p1->bignum[99]的是否大於十;*/  
  665.             {  
  666.                 ch=0+48;  
  667.                 res += ch;  
  668.                 ch=p1->bignum[MAX-1]+48;  
  669.                 res += ch;  
  670.             }  
  671.             else  
  672.             {  
  673.                 ch=p1->bignum[MAX-1]/10+48;  
  674.                 res += ch;  
  675.                 ch=p1->bignum[MAX-1]%10+48;  
  676.                 res += ch;  
  677.             }  
  678.   
  679.             for(i=0;i<p1->bignum[MAX-1];i++)  
  680.             {  
  681.                 ch=p1->bignum[i]+48;  
  682.                 res += ch;  
  683.             }  
  684.             p=p->next;  
  685.             p1=(struct slink * )malloc(LEN);  
  686.         }while(p!=NULL);  
  687.         return res;  
  688. }  
  689.   
  690. //! RSA的進行文件解密的函數  
  691. /*! 
  692.    * @param[in] d,n爲私鑰,由函數RSAKey()產生 
  693.    * @param[in] text爲密文,對應加密函數,密文的格式爲string 
  694.    * @return[string] 解密之後的明文采用string進行存儲 
  695.    * @pre \e 進行解密的過程中進行了數據類型的轉換 
  696.    * @see e,d,n & text 
  697. */  
  698. string RSA::tdecrypto(int d[MAX], int n[MAX], string text)  
  699. {  
  700.     struct slink *h,*p1,*p2;  
  701.     char ch;  
  702.     int i,j,k,c,count,temp;  
  703.     i=0;  
  704.     j=3;  
  705.     count=0;  
  706.     h=p1=p2=(struct slink * )malloc(LEN);  
  707.   
  708.     int kk;  
  709.     for (kk = 0; kk < text.length(); kk++)  
  710.     {    
  711.         ch = text.at(kk);  
  712.         c=ch;        
  713.         if(j==3)  
  714.         {  
  715.             p1->bignum[MAX-2]=c;  
  716.             j--;  
  717.         }  
  718.         else if(j==2)  
  719.         {  
  720.             temp=c-48;  
  721.             j--;  
  722.         }  
  723.         else if(j==1)  
  724.         {  
  725.             p1->bignum[MAX-1]=temp*10+c-48;  
  726.             j--;  
  727.         }  
  728.         else if (j==0)  
  729.         {  
  730.             p1->bignum[i]=c-48;  
  731.             i++;  
  732.             if(i==p1->bignum[MAX-1])  
  733.             {   
  734.                 i=0;  
  735.                 j=3;  
  736.                 count++;  
  737.                 if (count==1)  
  738.                     h=p1;  
  739.                 else p2->next=p1;  
  740.                 p2=p1;  
  741.                 p1=(struct slink * )malloc(LEN);  
  742.             }  
  743.         }  
  744.     }  
  745.     p2->next=NULL;   
  746.   
  747.     p2=(struct slink * )malloc(LEN);  
  748.     p1=h;  
  749.     k=0;  
  750.     string res;  
  751.     if(h!=NULL)/*/temp爲暫存ASIIC碼的int值*/  
  752.         do  
  753.         {  
  754.             for(i=0;i<MAX;i++)  
  755.                 p2->bignum[i]=0;  
  756.             expmod( p1->bignum , d ,n ,p2->bignum);           
  757.             temp=p2->bignum[0]+p2->bignum[1]*10+p2->bignum[2]*100;  
  758.             if (( p2->bignum[MAX-2])=='0')  
  759.             {  
  760.                 temp=0-temp;  
  761.             }/*/轉化爲正確的ASIIC碼,如-78-96形成漢字    */     
  762.             ch=temp;/*  str[k]--->ch */  
  763.             res += ch;  
  764.             k++;  
  765.             p1=p1->next;  
  766.             p2=(struct slink * )malloc(LEN);  
  767.         }while (p1!=NULL);  
  768.         return res;  
  769. }  
  770. //////////////////////////////////////////////////////////////////////////////////  
  771. //////////////////////////////////////////////////////////////////////////////////  
  772. /* 
  773. * 產生密鑰,公鑰和私鑰 
  774. * @param[in] 隨機產生大數密鑰 
  775. * @param[in] 利用自定義的大數運算規則進行計算 
  776. * @return 返回產生成功與否 
  777. * – false 表示產生密鑰失敗 
  778. * @pre \e 產生的密鑰存儲在定義的類中變量中 
  779. * @see e,d,n 
  780. */  
  781. bool RSA::RSAKey()  
  782. {  
  783.     for(i=0;i<MAX;i++)  
  784.         m[i]=p[i]=q[i]=n[i]=d[i]=e[i]=0;/*/簡單初始化一下*/  
  785.     prime_random(p,q);/*/隨機產生兩個大素數*/  
  786.     mul(p,q,n);  
  787.     mov(p,p1);  
  788.     p1[0]--;        
  789.     mov(q,q1);  
  790.     q1[0]--;      /*/q-1;*/  
  791.     mul(p1,q1,m);//m=(p-1)*(q-1)  
  792.     erand(e,m);  
  793.     rsad(e,m,d);  
  794.     return true;  
  795. }  
  796. //////////////////////////  
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章