鱼C论坛

 找回密码
 立即注册
查看: 8400|回复: 37

[技术交流] 一个RSA加密算法。

[复制链接]
发表于 2014-3-11 13:12:26 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
这个算法参考了多个资源。还有自己的改变。RSA.cpp
  1. #include <iostream>
  2. #include <ctime>
  3. #include "HugeInt.h"
  4. #include "Random.h"

  5. // Compute x^n ( mod p ).
  6. // HugeInt: must have copy constructor, operator=,
  7. //     conversion from int, *, /, %, ==, and !=
  8. //     Assumes that p is not zero and power( 0, 0, p ) is 1
  9. HugeInt power( const HugeInt & x, const HugeInt & n , const HugeInt & p )
  10. {

  11.     if( n == 0 )
  12.         return 1;

  13.     HugeInt tmp = power( ( x * x ) % p, n / 2, p );
  14.    
  15.     if( n % 2 != 0 )
  16.         tmp = ( tmp * x ) % p;

  17.     return tmp;
  18. }

  19. // Probabilistic primality testing routine.
  20. // If witness does not return 1, n is definitely composite.
  21. // Do this by computing a^i ( mod n ) and looking for
  22. // non-trivial square roots of 1 along the way.
  23. // HugeInt: must have copy constructor, operator=,
  24. //     conversion from int, *, /, -, %, ==, and !=
  25. HugeInt witness( const HugeInt & a, const HugeInt & i, const HugeInt & n )
  26. {
  27.     if( i == 0 )
  28.         return 1;

  29.     HugeInt x = witness( a, i / 2, n );
  30.     if( x == 0 )    // If n is recursively composite, stop
  31.         return 0;

  32.       // n is not prime if we find a nontrivial square root of 1
  33.     HugeInt y = ( x * x ) % n;
  34.     if( y == 1 && x != 1 && x != n - 1 )
  35.         return 0;

  36.     if( i % 2 == 1 )
  37.         y = ( a * y ) % n;

  38.     return y;
  39. }

  40. // Make NUM_TRIALS calls to witness to check if n is prime.
  41. bool isPrime( const HugeInt & n )
  42. {
  43.     const int NUM_TRIALS = 5;
  44.         Random rnd;

  45.     for( int counter = 0; counter < NUM_TRIALS; counter++ )
  46.         if( witness( rnd.randomInt( 2, n ) , n - 1, n ) != 1 )
  47.             return false;

  48.     return true;
  49. }

  50. // Returns the greatest common divisor of a and b.
  51. HugeInt gcd( const HugeInt & a, const HugeInt & b )
  52. {
  53.     if( b == 0 )
  54.         return a;
  55.     else
  56.         return gcd( b, a % b );
  57. }

  58. // Given a and b, assume gcd( a, b ) = 1.
  59. // Find x and y such that  a x + b y = 1.
  60. // HugeInt: must have copy constructor,
  61. //     zero-parameter constructor, operator=,
  62. //     conversion from int, *, /, +, -, %, ==, and >
  63. void fullGcd( const HugeInt & a, const HugeInt & b,
  64.               HugeInt & x, HugeInt & y )
  65. {
  66.     HugeInt x1, y1;

  67.     if( b == 0 )
  68.     {
  69.         x = 1;       // If a != 1, there is no inverse
  70.         y = 0;       // We omit this check
  71.     }
  72.     else
  73.     {
  74.         fullGcd( b, a % b, x1, y1 );
  75.         x = y1;
  76.         y = x1 - ( a / b ) * y1;
  77.     }
  78. }

  79. // GetPrime randomly picks a large, odd number until it gets
  80. // one that is prime. It uses is_prime to check for primality.
  81. // The more failed primes that occur, the larger the N becomes.

  82. HugeInt GetPrime( )
  83. {
  84.         Random rnd(time(0));
  85.         HugeInt N = rnd.randomInt( 1000000000, INT_MAX );
  86.         HugeInt NN = rnd.randomInt( 1000000000, INT_MAX );
  87.         HugeInt multiplier(1314521);
  88.         N = N * NN * N * multiplier;
  89.         HugeInt Odd = 2;

  90.         if ( N % 2 == 0 )
  91.                 N = N+1;

  92.         while ( !isPrime( N )){
  93.                 N = N + Odd;
  94.         }
  95.         return N;
  96. }

  97. // Solve a x == 1 ( mod n ).
  98. // Assume that gcd( a, n ) = 1.
  99. HugeInt inverse( const HugeInt & a, const HugeInt & n )
  100. {
  101.     HugeInt x, y;

  102.     fullGcd( a, n, x, y );
  103.     return x > 0 ? x : x + n;
  104. }

  105. // Illustrate an RSA computation
  106. int main( )
  107. {
  108.       // Sample of how RSA works.
  109.     HugeInt p = GetPrime();
  110.     HugeInt q = GetPrime();
  111.     HugeInt message, code, decode;
  112.     HugeInt n, nPrime, e, d;
  113.         std::string myletter = "For my friends!";
  114.         message.rsa(myletter);

  115.     std::cout <<  "p: " << p << std::endl;
  116.     std::cout <<  "q: " << q << std::endl;

  117.     n = p * q;
  118.     std::cout <<  "n: " << n << std::endl;

  119.     nPrime = ( p - 1 ) * ( q - 1 );
  120.     std::cout <<  "nprime: " << nPrime << std::endl;

  121.     for( e = nPrime/521; gcd( e, nPrime ) != 1; e++ );
  122.     std::cout <<  "e: " << e << std::endl;

  123.     d = inverse( e, nPrime );
  124.     std::cout <<  "d: " << d << std::endl;

  125.     std::cout << "Verify inverse: " << ( e * d % ( nPrime ) ) << std::endl;

  126.     std::cout << "message: " << myletter << std::endl;

  127.     code = power( message, e, n );
  128.     decode = power( code, d, n );

  129.     std::cout << "Code: " << code << std::endl;
  130.     std::cout << "Decode: " << decode.asr() << std::endl;

  131.     if( message != decode )
  132.         std::cout << "OOPS: " << std::endl;
  133.     else
  134.         std::cout << "Success!!!!" << std::endl;

  135.     return 0;
  136. }
复制代码
游客,如果您要查看本帖隐藏内容请回复

RSA方法源于Mark Allen Weiss的实现。

想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2014-3-11 13:26:10 From FishC Mobile | 显示全部楼层
看看,呃呃呃
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2014-3-11 22:15:06 | 显示全部楼层
由于发帖字数限制,完全的源代码只有回复下载了才能看到。这里可以提供运行过后的一个截图。很成功的~ RSA.jpg
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2014-4-4 15:13:37 | 显示全部楼层
支持楼主  这个挺好 研究研究
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2014-4-14 13:00:01 | 显示全部楼层
hhhhhhhhh呵呵呵
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2014-8-15 12:22:51 | 显示全部楼层
回帖是一种美德
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2014-8-16 11:30:11 From FishC Mobile | 显示全部楼层
回帖是一种美德
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2014-8-20 20:38:50 | 显示全部楼层
这东西不错呀,非常感谢楼主分享。。。!
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2014-8-21 10:06:55 | 显示全部楼层
谢谢分享
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2014-8-25 11:53:45 | 显示全部楼层
感谢楼主无私分享!!!!!
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2014-8-25 14:30:54 | 显示全部楼层
谢谢分享~
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2014-8-31 10:29:42 | 显示全部楼层
顶顶顶顶顶顶顶
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2014-9-1 10:27:36 | 显示全部楼层
一句话,支持你,支持开源,支持原创。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2014-9-10 18:43:21 | 显示全部楼层
很好噶
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2014-9-11 09:41:58 | 显示全部楼层
感谢楼主无私分享!!
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2014-9-21 12:50:28 | 显示全部楼层
看看。。。。。。。。。。。。。。。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2014-10-4 18:01:22 | 显示全部楼层
学习一下。。。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2014-11-2 00:42:41 | 显示全部楼层
四处找资料,,恶补知识
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2015-3-22 22:33:58 | 显示全部楼层
谢谢分享,辛苦了
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2015-4-14 15:42:23 | 显示全部楼层
谢谢分享!学习它。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-4-24 02:04

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表