牛骨文教育服务平台(让学习变的简单)
博文笔记

四种加密算法之RSA源代码-C++

创建时间:2014-05-08 投稿人: 浏览次数:31207

RSA.h文件:

[cpp] view plaincopy
  1. //! RSA 动态链接库实现   H文件  
  2. /*! 
  3.  @author 朱孟斌 
  4.  @e-mail  zmb.tsubasa@gmail.com 
  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文件:

[cpp] view plaincopy
  1. /*! 
  2. * @ RSA 动态链接库实现   CPP文件 
  3. * @author 朱孟斌 
  4. * @e-mail  zmb.tsubasa@gmail.com 
  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])  
声明:该文观点仅代表作者本人,牛骨文系教育信息发布平台,牛骨文仅提供信息存储空间服务。