返回列表 发帖

[原创] RSA加密 源代码

[这个贴子最后由x86在 2005/03/23 01:09pm 第 1 次编辑]


为了方便大家,我已经打包上传了,在visual c++6.0编译通过!!!!


#ifndef STACK_H
#define STACK_H
class node{
public:
unsigned char c;
node* next;
node(char cc,node* n=NULL){c=cc;next=n;}
node(node* n=NULL){next=n;}
};
class stack:public node{
private:
node* head;
int length;
public:
stack(){head=NULL;length = 0;}
~stack()
{
node* temp;
while(head!=NULL)
{
temp=head;
head=head->next;
delete head;
}
}
void push(const unsigned char& cc)
{
node* temp;
temp=head;
head=new node(cc,temp);
++length;
}
bool pop(unsigned char& cc)
{
if(head==NULL)
return false;
node* temp;
temp=head;
head=head->next;
cc=temp->c;
delete temp;
--length;
return true;
}
int getLength()
{
return length;
}
};
#endif

[ 本帖最后由 chinanic 于 2007-8-25 13:43 编辑 ]

RSA加密 源代码

  1. #ifndef INTNODE_H
  2. #define INTNODE_H
  3. class intNode{
  4. public:
  5. unsigned int num;
  6. intNode *next;
  7. intNode();
  8. intNode(unsigned int para);
  9. intNode(unsigned int para, intNode* p);
  10. intNode(intNode* p);
  11. ~intNode();
  12. void* operator new(size_t);
  13. void operator delete(void*);
  14. void clear();
  15. private:
  16. static intNode* freelist;
  17. };
  18. intNode* intNode::freelist = NULL;
  19. //constructor implement
  20. intNode::intNode()
  21. {
  22. num = 0;
  23. next = NULL;
  24. }
  25. intNode::intNode(unsigned int para)
  26. {
  27. num = para;
  28. next = NULL;
  29. }
  30. intNode::intNode(unsigned int para, intNode* p)
  31. {
  32. num = para;
  33. next = p;
  34. }
  35. intNode::intNode(intNode* p)
  36. {
  37. num = 0;
  38. next = p;
  39. }
  40. //disconstruct
  41. intNode::~intNode()
  42. {}
  43. //overload for new operator
  44. void* intNode::operator new(size_t)
  45. {
  46. if(freelist == NULL)
  47. return ::new intNode;//Create space
  48. intNode* temp;//can take from freelist
  49. temp = freelist;
  50. freelist = freelist->next;
  51. return temp;//return the link
  52. }
  53. //over load for delete operator
  54. void intNode::operator delete(void* ptr)
  55. {
  56. ((intNode*) ptr)->next = freelist;//put on freelist
  57. freelist = (intNode*) ptr;
  58. }
  59. //delete the freelist
  60. void intNode::clear()
  61. {
  62. intNode* temp;
  63. temp = freelist;
  64. while(freelist != NULL)
  65. {
  66. temp = freelist;
  67. freelist = freelist->next;
  68. ::delete temp;
  69. }
  70. }
  71. #endif
复制代码

[ 本帖最后由 chinanic 于 2007-8-25 13:43 编辑 ]

TOP

RSA加密 源代码

  1. #if !defined DESPROCESS_H
  2. #define DESPROCESS_H
  3. #if _MSC_VER > 1000
  4. #pragma once
  5. #endif//_MSC > 1000
  6. #include<iostream>
  7. #include<string>
  8. #include<iomanip>
  9. #include<cstdio>
  10. using namespace std;
  11. const static int maxlen = 60000;
  12. //IP
  13. const static int ip[64] = {
  14. 58, 50, 42, 34, 26, 18, 10, 2,
  15. 60, 52, 44, 36, 28, 20, 12, 4,
  16. 62, 54, 46, 38, 30, 22, 14, 6,
  17. 64, 56, 48, 40, 32, 24, 16, 8,
  18. 57, 49, 41, 33, 25, 17, 9, 1,
  19. 59, 51, 43, 35, 27, 19, 11, 3,
  20. 61, 53, 45, 37, 29, 21, 13, 5,
  21. 63, 55, 47, 39, 31, 23, 15, 7};
  22. //the reverse of IP
  23. const static int fp[64] = {
  24. 40, 8, 48, 16, 56, 24, 64, 32,
  25. 39, 7, 47, 15, 55, 23, 63, 31,
  26. 38, 6, 46, 14, 54, 22, 62, 30,
  27. 37, 5, 45, 13, 53, 21, 61, 29,
  28. 36, 4, 44, 12, 52, 20, 60, 28,
  29. 35, 3, 43, 11, 51, 19, 59, 27,
  30. 34, 2, 42, 10, 50, 18, 58, 26,
  31. 33, 1, 41,  9, 49, 17, 57, 25};
  32. //E matrix
  33. const static int e[48] = {
  34. 32,  1,  2,  3,  4,  5,
  35. 4,   5,  6,  7,  8,  9,
  36. 8,   9, 10, 11, 12, 13,
  37. 12, 13, 14, 15, 16, 17,
  38. 16, 17, 18, 19, 20, 21,
  39. 20, 21, 22, 23, 24, 25,
  40. 24, 25, 26, 27, 28, 29,
  41. 28, 29, 30, 31, 32,  1};
  42. //S box
  43. const static int sbox[8][64] = {
  44. //S1
  45. 14,  4, 13,  1,  2, 15, 11,  8,
  46. 3, 10,  6, 12,  5,  9,  0,  7,
  47. 0, 15,  7,  4, 14,  2, 13,  1,
  48. 10,  6, 12, 11,  9,  5,  3,  8,
  49. 4,  1, 14,  8, 13,  6,  2, 11,
  50. 15, 12,  9,  7,  3, 10,  5,  0,
  51. 15, 12,  8,  2,  4,  9,  1,  7,
  52. 5, 11,  3, 14, 10,  0,  6, 13,
  53. //S2
  54. 15,  1,  8, 14,  6, 11,  3,  4,
  55. 9,  7,  2, 13, 12,  0,  5, 10,
  56. 3, 13,  4,  7, 15,  2,  8, 14,
  57. 12,  0,  1, 10,  6,  9, 11,  5,
  58. 0, 14,  7, 11, 10,  4, 13,  1,
  59. 5,  8, 12,  6,  9,  3,  2, 15,
  60. 13,  8, 10,  1,  3, 15,  4,  2,
  61. 11,  6,  7, 12,  0,  5, 14,  9,
  62. //S3
  63. 10,  0,  9, 14,  6,  3, 15,  5,
  64. 1, 13, 12,  7, 11,  4,  2,  8,
  65. 13,  7,  0,  9,  3,  4,  6, 10,
  66. 2,  8,  5, 14, 12, 11, 15,  1,
  67. 13,  6,  4,  9,  8, 15,  3,  0,
  68. 11,  1,  2, 12,  5, 10, 14,  7,
  69. 1, 10, 13,  0,  6,  9,  8,  7,
  70. 4, 15, 14,  3, 11,  5,  2, 12,
  71. //S4
  72. 7, 13, 14,  3,  0,  6,  9, 10,
  73. 1,  2,  8,  5, 11, 12,  4, 15,
  74. 13,  8, 11,  5,  6, 15,  0,  3,
  75. 4,  7,  2, 12,  1, 10, 14,  9,
  76. 10,  6,  9,  0, 12, 11,  7, 13,
  77. 15,  1,  3, 14,  5,  2,  8,  4,
  78. 3, 15,  0,  6, 10,  1, 13,  8,
  79. 9,  4,  5, 11, 12,  7,  2, 14,
  80. //S5
  81. 2, 12,  4,  1,  7, 10, 11,  6,
  82. 8,  5,  3, 15, 13,  0, 14,  9,
  83. 14, 11,  2, 12,  4,  7, 13,  1,
  84. 5,  0, 15, 10,  3,  9,  8,  6,
  85. 4,  2,  1, 11, 10, 13,  7,  8,
  86. 15,  9, 12,  5,  6,  3,  0, 14,
  87. 11,  8, 12,  7,  1, 14,  2, 13,
  88. 6, 15,  0,  9, 10,  4,  5,  3,
  89. //S6
  90. 12,  1, 10, 15,  9,  2,  6,  8,
  91. 0, 13,  3,  4, 14,  7,  5, 11,
  92. 10, 15,  4,  2,  7, 12,  9,  5,
  93. 6,  1, 13, 14,  0, 11,  3,  8,
  94. 9, 14, 15,  5,  2,  8, 12,  3,
  95. 7,  0,  4, 10,  1, 13, 11,  6,
  96. 4,  3,  2, 12,  9,  5, 15, 10,
  97. 11, 14,  1,  7,  6,  0,  8, 13,
  98. //S7
  99. 4, 11,  2, 14, 15,  0,  8, 13,
  100. 3, 12,  9,  7,  5, 10,  6,  1,
  101. 13,  0, 11,  7,  4,  9,  1, 10,
  102. 14,  3,  5, 12,  2, 15,  8,  6,
  103. 1,  4, 11, 13, 12,  3,  7, 14,
  104. 10, 15,  6,  8,  0,  5,  9,  2,
  105. 6, 11, 13,  8,  1,  4, 10,  7,
  106. 9,  5,  0, 15, 14,  2,  3, 12,
  107. //S8
  108. 13,  2,  8,  4,  6, 15, 11,  1,
  109. 10,  9,  3, 14,  5,  0, 12,  7,
  110. 1, 15, 13,  8, 10,  3,  7,  4,
  111. 12,  5,  6, 11,  0, 14,  9,  2,
  112. 7, 11,  4,  1,  9, 12, 14,  2,
  113. 0,  6, 10, 13, 15,  3,  5,  8,
  114. 2,  1, 14,  7,  4, 10,  8, 13,
  115. 15, 12,  9,  0,  3,  5,  6, 11
  116. };
  117. //P
  118. const static int p[32] = {
  119. 16,  7, 20, 21,
  120. 29, 12, 28, 17,
  121. 1, 15, 23, 26,
  122. 5, 18, 31, 10,
  123. 2,  8, 24, 14,
  124. 32, 27,  3,  9,
  125. 19, 13, 30,  6,
  126. 22, 11,  4, 25,
  127. };
  128. //PC--1
  129. const static int pc1[56] = {
  130. 57, 49, 41, 33, 25, 17,  9,
  131. 1, 58, 50, 42, 34, 26, 18,
  132. 10,  2, 59, 51, 43, 35, 27,
  133. 19, 11,  3, 60, 52, 44, 36,
  134. 63, 55, 47, 39, 31, 23, 15,
  135. 7, 62, 54, 46, 38, 30, 22,
  136. 14,  6, 61, 53, 45, 37, 29,
  137. 21, 13,  5, 28, 20, 12,  4
  138. };
  139. //the digit every circle should left move
  140. const static int ls[16] = {
  141. 1, 1, 2, 2, 2, 2, 2, 2, 1, 2, 2, 2, 2, 2, 2, 1};
  142. //PC--2
  143. const static int pc2[48] = {
  144. 14, 17, 11, 24,  1,  5,
  145. 3, 28, 15,  6, 21, 10,
  146. 23, 19, 12,  4, 26,  8,
  147. 16,  7, 27, 20, 13,  2,
  148. 41, 52, 31, 37, 47, 55,
  149. 30, 40, 51, 45, 33, 48,
  150. 44, 49, 39, 56, 34, 53,
  151. 46, 42, 50, 36, 29, 32
  152. };
  153. class DESProcess
  154. {
  155. public:
  156. DESProcess();
  157. ~DESProcess();
  158. void setBits(const unsigned int& r, const unsigned int& l);
  159. void setKey(const unsigned int& r, const unsigned int& l);
  160. //the desEnCode() function read prlaintext in the form
  161. //of charactors from the M file and write the cryphcoraphy in the form of
  162. //integer to the C File.   
  163. //the desDeCode() is just reverse.
  164. void desEnCode();
  165. void desDeCode();
  166. void outputBits(unsigned int& r, unsigned int& l);
  167. private:
  168. char mch[8];
  169. bool bits[64];
  170. bool constkey[64];
  171. bool key[64];
  172. bool tkey[48];
  173. unsigned int right, left;
  174. void desCoding(bool enCoding);
  175. void intToBit();
  176. void bitToInt();
  177. void ipBits();
  178. void reverseBits();
  179. void DiffBits();
  180. void fpBits();
  181. void pc_1Key();
  182. void lsKey(int length);
  183. void BitsToKey();
  184. void pc_2Key();
  185. void sBoxKey();
  186. void pKey();
  187. };
  188. #include"DESProcess.cpp"
  189. #endif
复制代码

[ 本帖最后由 chinanic 于 2007-8-25 13:42 编辑 ]

TOP

RSA加密 源代码

  1. //the implementation of the DESProcess class
  2. //construct and desconstruct of the DESProcess
  3. DESProcess:ESProcess()
  4. {
  5. //give defalute value to every member
  6. for(int i = 0; i < 8; i++)
  7. mch = ';#';;
  8. for(i = 0; i < 64; i++)
  9. {
  10. bits = false;
  11. key = false;
  12. constkey = false;
  13. if(i < 48)
  14. tkey = false;
  15. }
  16. right = left = 0;
  17. }
  18. DESProcess::~DESProcess()
  19. {
  20. }
  21. void DESProcess::setBits(const unsigned int& r, const unsigned int& l)
  22. {
  23. right = r;
  24. left = l;
  25. }
  26. void DESProcess::setKey(const unsigned int& r, const unsigned int& l)
  27. {
  28. right = r;
  29. left = l;
  30. intToBit();
  31. for(int i = 0; i < 64; ++i)
  32. constkey = bits;
  33. }
  34. void DESProcess::desEnCode()
  35. {
  36. intToBit();
  37. desCoding(true);
  38. bitToInt();
  39. }
  40. void DESProcess::desDeCode()
  41. {
  42. intToBit();
  43. desCoding(false);
  44. bitToInt();
  45. }
  46. void DESProcess:utputBits(unsigned int& r, unsigned int& l)
  47. {
  48. bitToInt();
  49. r = right;
  50. l = left;
  51. }
  52. void DESProcess::intToBit()
  53. {
  54. unsigned int con = 0x80000000; //the power of 2;
  55. int i; //circle variable;
  56. for(i = 0; i < 32; ++i)
  57. {
  58. if((con & right) != 0)
  59. bits = true;
  60. else
  61. bits = false;
  62. con >>= 1;
  63. }
  64. con = 0x80000000;
  65. for(; i < 64; ++i)
  66. {
  67. if((con & left) != 0)
  68. bits = true;
  69. else
  70. bits = false;
  71. con >>= 1;
  72. }
  73. }
  74. void DESProcess::bitToInt()
  75. {
  76. int i;
  77. right = 0;
  78. for(i = 0; i < 32; ++i)
  79. {
  80. right <<= 1;
  81. if(bits)
  82. right += 1;
  83. }
  84. left = 0;
  85. for(; i < 64; ++i)
  86. {
  87. left <<= 1;
  88. if(bits)
  89. left += 1;
  90. }
  91. }
  92. //descoding process
  93. void DESProcess::desCoding(bool enCoding)
  94. {
  95. ipBits();
  96. int i;//cicle variable
  97. for(i = 0; i < 64; ++i)
  98. key = constkey;
  99. pc_1Key();
  100. int sum = 0;
  101. for(i = 0; i < 16; ++i)
  102. sum += ls;
  103. if(! enCoding)
  104. lsKey(sum);
  105. else
  106. lsKey(ls[0]);
  107. for(i = 0; i < 16; ++i)
  108. {
  109. BitsToKey();
  110. pc_2Key();
  111. sBoxKey();
  112. pKey();
  113. DiffBits();
  114. if(i < 15)
  115. {
  116. if(enCoding)
  117. lsKey(ls[i + 1]);
  118. else
  119. lsKey(0 - ls[15 - i]);
  120. reverseBits();
  121. }
  122. }
  123. fpBits();
  124. }
  125. //IP Permute
  126. void DESProcess::ipBits()
  127. {
  128. bool temp[64];
  129. for(int i = 0; i < 64; ++i)
  130. temp = bits;
  131. for(i = 0; i < 64; ++i)
  132. bits = temp[ip - 1];
  133. }
  134. //exchange the front and tail
  135. void DESProcess::reverseBits()
  136. {
  137. bool temp[32];
  138. for(int i = 0; i < 32; ++i)
  139. {
  140. temp = bits;
  141. bits = bits[32 + i];
  142. bits[32 + i] = temp;
  143. }
  144. }
  145. //Bits and tkey different or
  146. void DESProcess:iffBits()
  147. {
  148. for(int i = 0; i < 32; ++i)
  149. bits = ( (! bits) && tkey) || ( (! tkey) && bits);
  150. }
  151. //ip';s contradictory permulate
  152. void DESProcess::fpBits()
  153. {
  154. bool temp[64];
  155. for(int i = 0; i < 64; ++i)
  156. temp = bits;
  157. for(i = 0; i < 64; ++i)
  158. bits = temp[fp - 1];
  159. }
  160. //pc-1 permulate
  161. void DESProcess::pc_1Key()
  162. {
  163. bool temp[64];
  164. for(int i = 0; i < 64; ++i)
  165. temp = key;
  166. for(i = 0; i < 56; ++i)
  167. key = temp[pc1 - 1];
  168. }
  169. //left shift the key with parameter length
  170. void DESProcess::lsKey(int length)
  171. {
  172. bool temp[28];
  173. int i;//circle variable
  174. length = length + 28;
  175. for(i = 0; i < 28; ++i)
  176. temp = key;
  177. for(i = 0; i < 28; ++i)
  178. key = temp[(i + length) % 28];
  179. for(i = 0; i < 28; ++i)
  180. temp = key[28 + i];
  181. for(i = 0; i < 28; ++i)
  182. key[28 + i] = temp[(i + length) % 28];
  183. }
  184. //expand the right part to the tkey
  185. void DESProcess::BitsToKey()
  186. {
  187. for(int i = 0; i < 48; ++i)
  188. tkey = bits[31 + e];
  189. }
  190. //pc_2 permulate and different or
  191. void DESProcess::pc_2Key()
  192. {
  193. for(int i = 0; i < 48; ++i)
  194. tkey = ((! tkey) && key[pc2 - 1]) || ((! key[pc2 - 1]) && tkey);
  195. }
  196. //sBox permulate the key
  197. void DESProcess::sBoxKey()
  198. {
  199. const int pow_2[4] = {1, 2, 4, 8};
  200. int i,j;
  201. bool temp[48];
  202. for(i = 0; i < 48; ++i)
  203. temp = tkey;
  204. int tempint;
  205. for(i = 0; i < 8; ++i)
  206. {
  207. tempint = 0;
  208. for(j = 0; j < 4; ++j)
  209. if(temp[i * 6 + 4 - j])
  210. tempint += pow_2[j];
  211. if(temp[i * 6 + 5])
  212. tempint += 16;
  213. if(temp[i*6])
  214. tempint += 32;
  215. tempint = sbox[tempint];
  216. for(j = 0; j < 4; ++j)  //number change to tkey
  217. tkey[i * 4 + j] = ((tempint & pow_2[3 - j]) != 0);
  218. } //end of for{}
  219. }
  220. //p permulate
  221. void DESProcess::pKey()
  222. {
  223. bool temp[32];
  224. for(int i = 0; i < 32; ++i)
  225. temp = tkey;
  226. for(i = 0; i < 32; ++i)
  227. tkey = temp[ p - 1];
  228. }
复制代码

[ 本帖最后由 chinanic 于 2007-8-25 13:42 编辑 ]

TOP

RSA加密 源代码

  1. #ifndef PREDEFINE_H
  2. #define PREDEFINE_H
  3. #include<cmath>
  4. #include"stack.h"
  5. #include"intNode.h"
  6. const unsigned int FULL = 0xFFFFFFFF;
  7. const unsigned int MAXDEC = 1000000000;
  8. const static unsigned char HArray[] = {';0';, ';1';, ';2';, ';3';, ';4';, ';5';, ';6';, ';7';, ';8';, ';9';,
  9. ';A';, ';B';, ';C';, ';D';, ';E';, ';F';};
  10. const static unsigned int sta_p[] = {2, 7, 3, 5, 97, 39, 3, 37, 1109, 1877};
  11. const static unsigned int r_laimda = 0x48c27395;
  12. const static unsigned int r_c = 23;
  13. const static double r_M = 100000000.;
  14. const static double r_MM = 30000000.;
  15. const static double r_cc = 113.;
  16. const static double r_l = 8.;
  17. unsigned int hdigit(unsigned char c)
  18. {
  19. unsigned char hi,lo;
  20. hi = c >> 4;
  21. lo = c & 0x0f;
  22. if(hi == 0x04)
  23. return (int)(lo + 0x09);
  24. if(hi == 0x06)
  25. return (int)(lo + 0x09);
  26. return (unsigned int)lo;
  27. }
  28. void multi(const unsigned int& p1,const unsigned int& p2, unsigned int& high, unsigned int& low)
  29. {
  30. unsigned int h1;
  31. unsigned int h2;
  32. unsigned int l1;
  33. unsigned int l2;
  34. h1 = (p1 >> 16);
  35. h2 = (p2 >> 16);
  36. l1 = (p1 & 0xFFFF);
  37. l2 = (p2 & 0xFFFF);
  38. high = h1 * h2;
  39. low = l1 * l2;
  40. unsigned int temp;
  41. unsigned int re;
  42. temp = h1 * l2;
  43. re = ((temp & 0xFFFF) << 16);
  44. high += (temp >> 16);
  45. temp = h2 * l1;
  46. re += ((temp & 0xFFFF) << 16);
  47. if(re < ((temp & 0xFFFF) << 16))
  48. ++high;
  49. high += temp >> 16;
  50. low += re;
  51. if(low < re)
  52. ++high;
  53. }
  54. intNode* backToFront(intNode* p)
  55. {
  56. intNode* t1 = NULL;
  57. intNode* t2;
  58. while(p != NULL)
  59. {
  60. t2 = p->next;
  61. p->next = t1;
  62. t1 = p;
  63. p = t2;
  64. }
  65. return t1;
  66. }
  67. int FindZeroBit(unsigned int num)
  68. {
  69. int cc = FULL >> 4;
  70. int Fbit = 3;
  71. while(num < cc)
  72. {
  73. cc >>= 4;
  74. Fbit += 4;
  75. }
  76. cc = FULL >> Fbit;
  77. while((cc & num) != num)
  78. {
  79. --Fbit;
  80. cc = FULL >> Fbit;
  81. }
  82. return Fbit;
  83. }
  84. bool TheLastNode(intNode*& t)
  85. {
  86. if(t == NULL)
  87. return false;
  88. while(t->next != NULL)
  89. t = t->next;
  90. return true;
  91. }
  92. #endif
复制代码

[ 本帖最后由 chinanic 于 2007-8-25 13:42 编辑 ]

TOP

RSA加密 源代码

  1. #ifndef BIGINT_H
  2. #define BIGINT_H
  3. #include"predefine.h"
  4. class BigInt{
  5. public:
  6. BigInt();
  7. ~BigInt();
  8. void setZero();
  9. void loadDEC(const char* str, const int length);//
  10. void loadHEX(const char* str, const int length);//
  11. void loadSTR(const char* str, const int length);
  12. char* outputDEC(int& length);
  13. char* outputHEX(int& length);
  14. char* outputSTR(int& length);
  15. BigInt operator + (const BigInt& p) const;
  16. BigInt operator + (const unsigned int& p) const;
  17. BigInt operator - (const BigInt& p) const;
  18. BigInt operator - (const unsigned int& p) const;
  19. BigInt operator * (const BigInt& p) const;
  20. BigInt operator * (const unsigned int& p) const;
  21. BigInt operator / (const BigInt& p);
  22. BigInt operator / (const unsigned int& p);
  23. BigInt operator % (const BigInt& p) const;
  24. unsigned int operator % (const unsigned int& p) const;
  25. bool operator > (const BigInt& p) const;
  26. bool operator > (const unsigned int p) const;
  27. bool operator == (const BigInt& p) const;
  28. bool operator != (const BigInt& p) const;
  29. BigInt& operator <<= (int iBit);
  30. BigInt& operator >>= (int iBit);
  31. BigInt& operator += (const unsigned int& p);
  32. BigInt& operator = (const BigInt& p);
  33. BigInt& operator = (const unsigned int& p);
  34. BigInt& operator ++ ();
  35. BigInt& operator -- ();
  36. bool BIlisZero() const;
  37. int BICompareABS(const BigInt& p) const;
  38. int BICompareABS(const unsigned int& p) const;
  39. BigInt BIdivision(const BigInt& p, BigInt& pr);
  40. unsigned int getLast32Bit();
  41. void copy(const BigInt& p);
  42. void add(const BigInt& p, BigInt& temp) const;
  43. void minuse(const BigInt& p, BigInt& temp) const;
  44. void add(const unsigned int& p,BigInt& temp) const;
  45. void minuse(const unsigned int& p,BigInt& temp) const;
  46. void clear();
  47. void MakeClose(BigInt& p);
  48. BigInt BIinverse(const BigInt& p) const;
  49. int nodeNumber() const;
  50. intNode* getHead() const;
  51. void print(int choose);
  52. //void MakeModMultTable(const BigInt& pp);
  53. //BigInt MontMult(const BigInt& p1, const BigInt& p2, const BigInt pp);
  54. //BigInt MontExpo(const BigInt& p1, const BigInt& p2, const BigInt pp);
  55. private:
  56. intNode* head;
  57. bool positive;
  58. friend BigInt BIgcd(const BigInt& p1, const BigInt& p2);
  59. friend void BIswap(BigInt& a, BigInt& b);
  60. friend BigInt BImodmul(const BigInt& p1, const BigInt& p2, const BigInt pp);
  61. friend BigInt BImodmul2(const BigInt& p1, const BigInt& p2, const BigInt pp);
  62. friend void MakeModBaseTable(const BigInt& pp);
  63. friend BigInt BImodexp2(const BigInt& p1, const BigInt& p2, const BigInt pp);
  64. friend BigInt BImodexp(const BigInt& p1, const BigInt& p2, const BigInt pp);
  65. friend BigInt BImodpow2(const BigInt& p1, const BigInt& pp, const int& d);
  66. friend void MakePreTable(const BigInt& p, const BigInt& pp);
  67. };
  68. #include"BigInt.cpp"
  69. #endif
复制代码

[ 本帖最后由 chinanic 于 2007-8-25 13:42 编辑 ]

TOP

RSA加密 源代码

  1. BigInt TableModBase[25];//for modlue multie
  2. BigInt PreTable[16];
  3. //constructor
  4. BigInt::BigInt()
  5. {
  6. head = new intNode();
  7. positive = true;
  8. }
  9. //disconstuctor
  10. BigInt::~BigInt()
  11. {
  12. }
  13. //set the number to 0
  14. void BigInt::setZero()
  15. {
  16. intNode* temp;
  17. while(head->next != NULL)
  18. {
  19. temp = head;
  20. head = head->next;
  21. delete temp;
  22. }
  23. positive = true;
  24. head -> num = 0;
  25. }
  26. //load the number at the base of ten
  27. void BigInt::loadDEC(const char* str, const int length)
  28. {
  29. unsigned int temp = 0;
  30. unsigned int base = MAXDEC;
  31. setZero();
  32. int i = 0;
  33. if(str[i] == ';-';)
  34. {
  35. positive = false;
  36. ++i;
  37. }
  38. for(; i < length; ++i)
  39. {
  40. temp *= 10;
  41. temp += ((unsigned int) (str[i] - ';0';));
  42. if((i + 1) % 9 == 0)
  43. {
  44. *this = *this * base;
  45. *this += temp;
  46. temp = 0;
  47. }
  48. }
  49. base = (unsigned int)pow(10, (length % 9));
  50. *this = * this * base;
  51. *this += temp;
  52. }
  53. //load the number at the base of 16
  54. void BigInt::loadHEX(const char* str, const int length)
  55. {
  56. unsigned int temp = 0;
  57. unsigned char t;
  58. setZero();
  59. int i = 0;
  60. if(str[i] == ';-';)
  61. {
  62. positive = false;
  63. ++i;
  64. }
  65. for(; i < length; ++i)
  66. {
  67. temp <<= 4;
  68. t = (unsigned char) str[i];
  69. temp |= hdigit(t);
  70. if((i + 1) % 8 == 0)
  71. {
  72. *this <<= 32;
  73. *this += temp;
  74. temp = 0;
  75. }
  76. }
  77. *this <<= (length % 8) * 4;
  78. *this += temp;
  79. }
  80. //convert the string to big number
  81. void BigInt::loadSTR(const char* str, const int length)
  82. {
  83. unsigned int temp = 0;
  84. setZero();
  85. int i = 0;
  86. int mi = 1;
  87. if(str[0] == ';-';)
  88. {
  89. positive = false;
  90. ++i;
  91. mi = 0;
  92. }
  93. for(; i < length ; ++i)
  94. {
  95. temp <<= 8;
  96. temp |= (unsigned int)str[i];
  97. if( (i +mi) % 4 == 0)
  98. {
  99. *this <<= 32;
  100. *this += temp;
  101. temp = 0;
  102. }
  103. }
  104. * this <<= (length % 4) * 8;
  105. *this += temp;
  106. }
  107. //output the number at the base ten
  108. char* BigInt::outputDEC(int& length)
  109. {
  110. unsigned int temp;
  111. unsigned int base = MAXDEC;
  112. int i;
  113. unsigned char c = ';0';;
  114. stack s;//the stack to store the charactors
  115. BigInt a;
  116. a.copy(*this);
  117. unsigned char* ch;
  118. if(a.BIlisZero())
  119. {
  120. ch = new unsigned char[1];
  121. ch[0] = ';0';;
  122. length = 1;
  123. return (char*)ch;
  124. }
  125. while(! a.BIlisZero())
  126. {
  127. temp = a % base;
  128. a = a / base;
  129. for(i = 0; i < 9; ++i)
  130. {
  131. c = (unsigned char)((temp % 10) + ((unsigned int)';0';));
  132. s.push(c);
  133. temp /= 10;
  134. }
  135. }//convent the number at the base of ten to charactors and store the in the stack
  136. c = ';0';;
  137. while(c == ';0';)
  138. s.pop(c);
  139. length = s.getLength() + 1;
  140. i = 0;
  141. if(!positive)
  142. ++length;
  143. ch = new unsigned char[length];
  144. if(!positive)
  145. {
  146. ++i;
  147. ch[0] = ';-';;
  148. }
  149. for(; i < length; ++i)
  150. {
  151. ch[i] = c;
  152. s.pop(c);
  153. }//pop the characors and put the in the characotors
  154. return (char*)ch;
  155. }
  156. //output the number at the base of 16
  157. char* BigInt::outputHEX(int& length)
  158. {
  159. unsigned int temp;
  160. BigInt a;
  161. a.copy(*this);
  162. stack s;
  163. unsigned char c = ';0';;
  164. int i;
  165. unsigned char* ch;
  166. if(a.BIlisZero())
  167. {
  168. ch = new unsigned char[1];
  169. ch[0] = ';0';;
  170. length = 1;
  171. return (char*)ch;
  172. }
  173. while(!a.BIlisZero())
  174. {
  175. temp = a.getLast32Bit();
  176. a >>= 32;
  177. for(i = 0; i < 8; ++i)
  178. {
  179. c = HArray[temp & 0xF];
  180. s.push(c);
  181. temp >>= 4;
  182. }
  183. }//convent the number at the base of 16 to charactors and store the in the stack
  184. c = ';0';;
  185. while(c == ';0';)
  186. s.pop(c);
  187. length = s.getLength() + 1;
  188. i = 0;
  189. if(!positive)
  190. ++length;
  191. ch = new unsigned char[length];
  192. if(!positive)
  193. {
  194. ++i;
  195. ch[0] = ';-';;
  196. }
  197. for(; i < length; ++i)
  198. {
  199. ch[i] = c;
  200. s.pop(c);
  201. }//pop the characors and put the in the characotors
  202. return (char*)ch;
  203. }
  204. //output the number at form of ASCII
  205. char* BigInt::outputSTR(int& length)
  206. {
  207. unsigned int temp;
  208. BigInt a;
  209. a.copy(*this);
  210. stack s;
  211. unsigned char c;
  212. int i;
  213. unsigned char* ch;
  214. if(a.BIlisZero())
  215. {
  216. ch = new unsigned char [1];
  217. ch[0] = 0;
  218. length = 1;
  219. return (char*)ch;
  220. }
  221. while(! a.BIlisZero())
  222. {
  223. temp = a.getLast32Bit();
  224. a >>= 32;
  225. for(i = 0; i < 4; ++i)
  226. {
  227. c = (unsigned char)(temp & 0xFF);
  228. s.push(c);
  229. temp >>= 8;
  230. }
  231. }//convent the number at the base of 16 to charactors and store the in the stack
  232. c = 0;
  233. while(c == 0)
  234. s.pop(c);
  235. length = s.getLength() + 1;
  236. ch = new unsigned char[length];
  237. for(i = 0; i < length; ++i)
  238. {
  239. ch[i] = c;
  240. s.pop(c);
  241. }//pop the characors and put the in the characotors
  242. return (char*)ch;
  243. }
  244. //reload the operator +
  245. BigInt BigInt::operator + (const BigInt& p) const
  246. {
  247. BigInt temp;
  248. if((positive && p.positive) || ((!positive) && (!p.positive)))
  249. {
  250. add(p, temp);
  251. temp.positive = positive;
  252. }
  253. else
  254. {
  255. if( BICompareABS(p) > 0)
  256. {
  257. minuse(p,temp);
  258. temp.positive = positive;
  259. }
  260. else
  261. {
  262. if( BICompareABS(p) == 0)
  263. temp.setZero();
  264. else
  265. {
  266. p.minuse(*this,temp);
  267. temp.positive = ! positive;
  268. }
  269. }//end of second else
  270. }//end of first else
  271. return temp;
  272. }
  273. //reload the operator +
  274. BigInt BigInt::operator + (const unsigned int& p) const
  275. {
  276. BigInt temp;
  277. if(positive)
  278. {
  279. add(p, temp);
  280. temp.positive = true;
  281. }
  282. else
  283. {
  284. temp.setZero();
  285. if( BICompareABS(p) > 0)
  286. {
  287. minuse(p, temp);
  288. temp.positive = false;
  289. }
  290. if(BICompareABS(p) < 0)
  291. {
  292. temp.head -> num = p - head->num;
  293. temp.positive = true;
  294. }
  295. }
  296. return temp;
  297. }
  298. BigInt BigInt::operator - (const BigInt& p) const
  299. {
  300. BigInt temp;
  301. if((positive && (!p.positive)) || ((!positive) && p.positive))
  302. {
  303. add(p, temp);
  304. temp.positive = positive;
  305. }
  306. else
  307. {
  308. if( BICompareABS(p) > 0)
  309. {
  310. minuse(p,temp);
  311. temp.positive = positive;
  312. }
  313. else
  314. {
  315. if( BICompareABS(p) == 0)
  316. temp.setZero();
  317. else
  318. {
  319. p.minuse(*this,temp);
  320. temp.positive = ! positive;
  321. }
  322. }//end of second else
  323. }//end of first else
  324. return temp;
  325. }
  326. BigInt BigInt::operator - (const unsigned int& p) const
  327. {
  328. BigInt temp;
  329. if(!positive)
  330. {
  331. add(p, temp);
  332. temp.positive = false;
  333. }
  334. else
  335. {
  336. temp.setZero();
  337. if( BICompareABS(p) > 0)
  338. {
  339. minuse(p, temp);
  340. temp.positive = true;
  341. }
  342. if(BICompareABS(p) < 0)
  343. {
  344. temp.head->num = p - head->num;
  345. temp.positive = false;
  346. }
  347. }
  348. return temp;
  349. }
  350. BigInt BigInt::operator * (const unsigned int& p) const
  351. {
  352. BigInt bin;
  353. unsigned int high = 0;
  354. unsigned int low = 0;
  355. intNode* temp = head;
  356. intNode* t1 = bin.head;
  357. intNode* t2 = bin.head;
  358. while(temp != NULL)
  359. {
  360. multi(p, temp->num, high, low);
  361. t1->num += low;
  362. if(t1->num < low)
  363. ++high;
  364. t1->next = new intNode;
  365. t2 = t1;
  366. t1 = t1->next;
  367. t1->num = high;
  368. temp = temp->next;
  369. }
  370. if(t1->num == 0)
  371. {
  372. delete t1;
  373. t2->next = NULL;
  374. }
  375. bin.positive = positive;
  376. return bin;
  377. }
  378. BigInt BigInt::operator * (const BigInt& p) const
  379. {
  380. BigInt bin;
  381. unsigned int high = 0;
  382. unsigned int low = 0;
  383. unsigned int carry = 0;
  384. intNode* temp1 = head;
  385. intNode* temp2 = p.head;
  386. intNode* t1 = bin.head;
  387. intNode* t2 = bin.head;
  388. intNode* t3 = bin.head;
  389. bin.positive = (positive && p.positive) || ((! positive) && (! p.positive));
  390. while(temp1 != NULL)
  391. {
  392. t2 = t1;
  393. temp2 = p.head;
  394. while(temp2 != NULL)
  395. {
  396. multi(temp1->num, temp2->num, high, low);
  397. t2->num += carry;
  398. if(t2->num < carry)
  399. carry = 1;
  400. else
  401. carry = 0;
  402. t2->num += low;
  403. if(t2->num < low)
  404. ++carry;
  405. carry += high;
  406. if(t2->next == NULL)
  407. {
  408. t3 = t2;
  409. t2->next = new intNode;
  410. }
  411. t2 = t2->next;
  412. temp2 = temp2->next;
  413. }
  414. t2->num = carry;
  415. carry = 0;
  416. temp1 = temp1 -> next;
  417. t1 = t1->next;
  418. }
  419. if(t2->num == 0)
  420. {
  421. delete t2;
  422. t3->next = NULL;
  423. }
  424. return bin;
  425. }
  426. BigInt BigInt::operator / (const BigInt& p)
  427. {
  428. BigInt d;
  429. BigInt r;
  430. r = BIdivision(p, d);
  431. r.positive = (positive && p.positive) || ((!positive) && (!p.positive));
  432. d.clear();
  433. return r;
  434. }
  435. BigInt BigInt::operator / (const unsigned int& p)
  436. {
  437. BigInt d;
  438. BigInt r;
  439. BigInt pp;
  440. pp = p;
  441. r.positive = positive;
  442. r = BIdivision(pp, d);
  443. d.clear();
  444. pp.clear();
  445. return r;
  446. }
  447. BigInt BigInt::operator % (const BigInt& p) const
  448. {
  449. BigInt a, mb;
  450. BigInt temp;
  451. a.copy(*this);
  452. a.positive = (p.positive && positive) || ((!p.positive) && (!positive));
  453. if(a.BICompareABS(p) > 0)
  454. {
  455. mb.copy(p);
  456. a.MakeClose(mb);
  457. while(a.BICompareABS(mb) > 0)
  458. mb <<= 1;
  459. while(a.BICompareABS(p) > 0)
  460. {
  461. while(a.BICompareABS(mb) < 0)
  462. mb >>= 1;
  463. temp = a - mb;
  464. a.copy(temp);
  465. }
  466. }
  467. if(a.BICompareABS(p) == 0)
  468. a.setZero();
  469. mb.clear();
  470. temp.clear();
  471. return a;
  472. }
  473. unsigned int BigInt::operator % (const unsigned int& p) const
  474. {
  475. BigInt mb;
  476. mb = p;
  477. BigInt a;
  478. a = (*this) % mb;
  479. unsigned int temp;
  480. temp = a.head->num;
  481. mb.clear();
  482. a.clear();
  483. return temp;
  484. }
  485. bool BigInt::operator > (const BigInt& p) const
  486. {
  487. if(positive && (!p.positive))
  488. return true;
  489. if((!positive) && p.positive)
  490. return false;
  491. if(positive && (BICompareABS(p) > 0))
  492. return true;
  493. if(!positive && (BICompareABS(p) < 0))
  494. return true;
  495. return false;
  496. }
  497. bool BigInt::operator > (const unsigned int p) const
  498. {
  499. if(! positive)
  500. return false;
  501. if(BICompareABS(p) > 0)
  502. return true;
  503. return false;
  504. }
  505. bool BigInt::operator == (const BigInt& p) const
  506. {
  507. if(positive &&(! p.positive))
  508. return false;
  509. return (BICompareABS(p) == 0);
  510. }
  511. bool BigInt::operator != (const BigInt& p) const
  512. {
  513. if(positive && (! p.positive))
  514. return true;
  515. return (BICompareABS(p) != 0);
  516. }
  517. BigInt& BigInt::operator <<= (int iBit)
  518. {
  519. if(BIlisZero())
  520. return *this;
  521. int block = 0;
  522. intNode* t1 = head;
  523. intNode* t2 = head;
  524. block = iBit / 32;
  525. for(int i = 0; i < block; ++i)
  526. {
  527. t2 = new intNode(head);
  528. head = t2;
  529. }
  530. iBit %= 32;
  531. if(iBit == 0)
  532. return *this;
  533. unsigned int carry = 0;
  534. unsigned int highF =((FULL >> (32 - iBit)) << (32 - iBit));
  535. unsigned int high;
  536. for(t2 = t1; t2->next != NULL; t2 = t2->next)
  537. {
  538. high = (t2->num & highF) >> (32 - iBit);
  539. t2->num <<= iBit;
  540. t2->num |= carry;
  541. carry = high;
  542. }
  543. high = (t2->num & highF) >> (32 - iBit);
  544. t2->num <<= iBit;
  545. t2->num |= carry;
  546. carry = high;
  547. if(carry != 0)
  548. t2->next = new intNode(carry);
  549. return *this;
  550. }
  551. BigInt& BigInt::operator >>= (int iBit)
  552. {
  553. if(BIlisZero())
  554. return *this;
  555. int block = iBit / 32;
  556. intNode* t = head;
  557. for(int i = 0; (i < block) && (head != NULL); ++i)
  558. {
  559. t = head;
  560. head = head->next;
  561. delete t;
  562. }
  563. if(head == NULL)
  564. {
  565. head = new intNode;
  566. return *this;
  567. }
  568. iBit %= 32;
  569. if(iBit == 0)
  570. return *this;
  571. t = head;
  572. if(t->next == NULL)
  573. {
  574. t->num >>= iBit;
  575. return *this;
  576. }
  577. unsigned int low;
  578. unsigned int lowF = ((FULL << (32 - iBit)) >> (32 - iBit));
  579. unsigned int carry = 0;
  580. for(t = head; t->next->next != NULL; t = t->next)
  581. {
  582. low = ((t->next->num & lowF) << (32-iBit));
  583. t->num = ((t->num >> iBit) | low);
  584. }
  585. low = ((t->next->num & lowF) << (32-iBit));
  586. t->num = ((t->num >> iBit) | low);
  587. t->next->num >>= iBit;
  588. if(t->next->num == 0)
  589. {
  590. delete t->next;
  591. t->next = NULL;
  592. }
  593. return *this;
  594. }
  595. BigInt& BigInt::operator = (const BigInt& p)
  596. {
  597. positive = p.positive;
  598. clear();
  599. head = p.head;
  600. return *this;
  601. }
  602. BigInt& BigInt::operator = (const unsigned int& p)
  603. {
  604. positive = true;
  605. clear();
  606. head = new intNode(p);
  607. return *this;
  608. }
  609. BigInt& BigInt::operator ++ ()
  610. {
  611. intNode *t = head;
  612. while(t->next != NULL)
  613. {
  614. ++(t->num);
  615. if(t->num != 0)
  616. return *this;
  617. t = t->next;
  618. }
  619. ++(t->num);
  620. if(t->num == 0)
  621. t->next = new intNode(1);
  622. return *this;
  623. }
  624. BigInt& BigInt::operator -- ()
  625. {
  626. intNode *t = head;
  627. intNode *tt = head;
  628. if(head->next == NULL)
  629. {
  630. --(t->num);
  631. return *this;
  632. }
  633. while(t->next != NULL)
  634. {
  635. --(t->num);
  636. if(t->num != FULL)
  637. return *this;
  638. tt = t;
  639. t = t->next;
  640. }
  641. --(t->num);
  642. if(t->num == 0)
  643. {
  644. delete t;
  645. tt->next = NULL;
  646. }
  647. return *this;
  648. }
  649. bool BigInt::BIlisZero() const
  650. {
  651. return ((head->next == NULL) && (head->num == 0));
  652. }
  653. int BigInt::BICompareABS(const BigInt& p) const
  654. {
  655. intNode* t1 = head;
  656. intNode* t2 = p.head;
  657. int comp = 0;
  658. while((t1 != NULL) && (t2 != NULL))
  659. {
  660. if(t1->num > t2->num)
  661. comp = 1;
  662. if(t1->num < t2->num)
  663. comp = -1;
  664. t1 = t1->next;
  665. t2 = t2->next;
  666. }
  667. if(t1 != NULL)
  668. comp = 1;
  669. if(t2 != NULL)
  670. comp = -1;
  671. return comp;
  672. }
  673. int BigInt::BICompareABS(const unsigned int& p) const
  674. {
  675. if((head->next != NULL) || (head->num > p))
  676. return 1;
  677. if(head->num < p)
  678. return -1;
  679. return 0;
  680. }
  681. void BigInt::copy(const BigInt& p)
  682. {
  683. if(head == NULL)
  684. head = new intNode;
  685. intNode* t1 = head;
  686. intNode* t2 = p.head;
  687. positive = p.positive;
  688. while(t2->next != NULL)
  689. {
  690. if(t1->next == NULL)
  691. t1->next = new intNode;
  692. t1->num = t2->num;
  693. t1 = t1->next;
  694. t2 = t2->next;
  695. }
  696. t1->num = t2->num;
  697. t2 = t1->next;
  698. t1->next = NULL;
  699. while(t2 != NULL)
  700. {
  701. t1 = t2;
  702. t2 = t2->next;
  703. delete t1;
  704. }
  705. }
  706. unsigned int BigInt::getLast32Bit()
  707. {
  708. if(head != NULL)
  709. return head->num;
  710. else
  711. return 0;
  712. }
  713. void BigInt::add(const BigInt& p, BigInt& temp) const
  714. {
  715. if(temp.head == NULL)
  716. temp.head = new intNode;
  717. intNode* t1 = temp.head;
  718. intNode* t2 = p.head;
  719. intNode* t3 = head;
  720. intNode* t4 = temp.head;
  721. unsigned int carry = 0;
  722. while((t2 != NULL) && (t3 != NULL))
  723. {
  724. if(t1->next == NULL)
  725. t1->next = new intNode;
  726. t1->num = carry;
  727. carry = 0;
  728. t1->num += t2->num;
  729. if(t1->num < t2->num)
  730. carry = 1;
  731. t1->num += t3->num;
  732. if(t1->num < t3->num)
  733. carry = 1;
  734. t4 = t1;
  735. t1 = t1->next;
  736. t2 = t2->next;
  737. t3 = t3->next;
  738. }
  739. if(t2 == NULL)
  740. t2 = t3;
  741. while(t2 != NULL)
  742. {
  743. if(t1->next == NULL)
  744. t1->next = new intNode;
  745. t1->num = t2->num + carry;
  746. if(t1->num < carry)
  747. carry = 1;
  748. else
  749. carry = 0;
  750. t4 = t1;
  751. t2 = t2->next;
  752. t1 = t1->next;
  753. }
  754. t1->num = carry;
  755. if(t1->num == 0)
  756. {
  757. delete t1;
  758. t4->next = NULL;
  759. }
  760. }
  761. void BigInt::minuse(const BigInt& p, BigInt& temp) const
  762. {
  763. temp.copy(*this);
  764. intNode* t1 = p.head;
  765. intNode* t2 = temp.head;
  766. unsigned int carry = 0;
  767. while(t1 != NULL)
  768. {
  769. t2->num -= carry;
  770. if((carry == 1) && (t2->num == FULL))
  771. carry = 1;
  772. else
  773. carry = 0;
  774. t2->num -= t1->num;
  775. if((t2->num + t1->num) < t2->num)
  776. carry = 1;
  777. t2 = t2->next;
  778. t1 = t1->next;
  779. }
  780. if(carry == 1)
  781. {
  782. while(t2 != NULL)
  783. {
  784. --(t2->num);
  785. if(t2->num != FULL)
  786. break;
  787. t2 = t2->next;
  788. }
  789. }
  790. for(t1 = temp.head; t1 != NULL; t1 = t1->next)
  791. {
  792. if(t1->num != 0)
  793. t2 = t1;
  794. }
  795. t1 = t2->next;
  796. t2->next = NULL;
  797. while(t1 != NULL)
  798. {
  799. t2 = t1;
  800. t1 = t1->next;
  801. delete t2;
  802. }
  803. }
  804. void BigInt::add(const unsigned int& p,BigInt& temp) const
  805. {
  806. temp.copy(*this);
  807. intNode* t = temp.head;
  808. t->num += p;
  809. if(t->num >= p)
  810. return;
  811. if(t->next == NULL)
  812. {
  813. t->next->next = new intNode;
  814. t->next->num = 1;
  815. return;
  816. }
  817. t = t->next;
  818. while(t->next != NULL)
  819. {
  820. ++(t->num);
  821. if(t->num != 0)
  822. return;
  823. t = t->next;
  824. }
  825. t->next = new intNode;
  826. t->next->num = 1;
  827. }
  828. void BigInt::minuse(const unsigned int& p,BigInt& temp) const
  829. {
  830. temp.copy(*this);
  831. intNode* t = temp.head;
  832. t->num -= p;
  833. if((t->num + p) > t->num)
  834. return;
  835. t = t->next;
  836. while(t->next->next != NULL)
  837. {
  838. --(t->num);
  839. if(t->num != FULL)
  840. return;
  841. t = t->next;
  842. }
  843. --(t->num);
  844. if(t->num != FULL)
  845. return;
  846. --(t->next->num);
  847. if(t->next->num == 0)
  848. {
  849. delete t->next;
  850. t->next = NULL;
  851. }
  852. }
  853. void BigInt::clear()
  854. {
  855. if(head == NULL)
  856. return;
  857. intNode* t;
  858. while(head != NULL)
  859. {
  860. t = head;
  861. head = head->next;
  862. delete t;
  863. }
  864. }
  865. void BigInt::MakeClose(BigInt& p)
  866. {
  867. intNode* t1 = head;
  868. intNode* t2 = p.head;
  869. while(t2 != NULL)
  870. {
  871. t1 = t1->next;
  872. t2 = t2->next;
  873. }
  874. while(t1 != NULL)
  875. {
  876. t1 = t1->next;
  877. t2 = new intNode(p.head);
  878. p.head = t2;
  879. }
  880. }
  881. BigInt BigInt::BIdivision(const BigInt& p, BigInt& pr)
  882. {
  883. int test = BICompareABS(p);
  884. BigInt re;
  885. if(test == 0)
  886. {
  887. pr.setZero();
  888. ++re;
  889. return re;
  890. }
  891. if(test < 0)
  892. {
  893. pr.copy(*this);
  894. return re;
  895. }
  896. BigInt t1;
  897. BigInt t2;
  898. t2.copy(p);
  899. t1.copy(*this);
  900. MakeClose(t2);
  901. while(BICompareABS(t2) > 0)
  902. t2 <<= 1;
  903. while(BICompareABS(t2) < 0)
  904. t2 >>= 1;
  905. while((p.BICompareABS(t1) <= 0) || (p.BICompareABS(t2) <= 0))
  906. {
  907. if(t1.BICompareABS(t2) >= 0)
  908. {
  909. t1 = t1 - t2;
  910. re <<= 1;
  911. ++re;
  912. }
  913. else
  914. re <<= 1;
  915. t2 >>= 1;
  916. }
  917. pr.copy(t1);
  918. return re;
  919. }
  920. BigInt& BigInt::operator += (const unsigned int& p)
  921. {
  922. intNode* temp = head;
  923. temp->num += p;
  924. if(temp->num >= p)
  925. return *this;
  926. if(temp->next == NULL)
  927. {
  928. temp->next = new intNode(1);
  929. return *this;
  930. }
  931. temp = temp->next;
  932. while(temp->next != NULL)
  933. {
  934. ++(temp->num);
  935. if(temp->num != 0)
  936. return *this;
  937. temp = temp->next;
  938. }
  939. ++(temp->num);
  940. if(temp->num == 0)
  941. temp->next = new intNode(1);
  942. return *this;
  943. }
  944. BigInt BigInt::BIinverse(const BigInt& p) const
  945. {
  946. BigInt y, g0, g1, g2, v0, v1, v2;
  947. g0.copy(p);
  948. g1.copy(*this);
  949. ++v1;
  950. while(! g1.BIlisZero())
  951. {
  952. y = g0.BIdivision(g1, g2);
  953. g0.copy(g1);
  954. g1.copy(g2);
  955. v2 = y * v1;
  956. while(v0.BICompareABS(v2) < 0)
  957. v0 = v0 + p;
  958. v0 = v0 - v2;
  959. BIswap(v0,v1);
  960. }
  961. y.clear();
  962. g0.clear();
  963. g1.clear();
  964. g2.clear();
  965. v1.clear();
  966. v2.clear();
  967. return v0;
  968. }
  969. int BigInt::nodeNumber() const
  970. {
  971. intNode* t;
  972. t = head;
  973. int i = 0;
  974. while(t != NULL)
  975. {
  976. t =  t->next;
  977. ++i;
  978. }
  979. return i;
  980. }
  981. BigInt BIgcd(const BigInt& p1, const BigInt& p2)
  982. {
  983. BigInt a;
  984. a.copy(p1);
  985. BigInt b;
  986. b.copy(p2);
  987. while(! b.BIlisZero())
  988. {
  989. a = a % b;
  990. BIswap(a, b);
  991. }
  992. b.clear();
  993. return a;
  994. }
  995. void BIswap(BigInt& a, BigInt& b)
  996. {
  997. bool bo;
  998. bo = a.positive;
  999. a.positive = b.positive;
  1000. b.positive = bo;
  1001. intNode* temp;
  1002. temp = a.head;
  1003. a.head = b.head;
  1004. b.head = temp;
  1005. }
  1006. void MakeModBaseTable(const BigInt& pp)
  1007. {
  1008. TableModBase[0].copy(pp);
  1009. for(int i = 1; i < 25; ++i)
  1010. {
  1011. TableModBase[i].copy(TableModBase[i - 1]);
  1012. TableModBase[i] <<= 2;
  1013. }
  1014. }
  1015. BigInt BImodmul(const BigInt& p1, const BigInt& p2, const BigInt pp)
  1016. {
  1017. BigInt a, b, c, result;
  1018. int i;
  1019. intNode* t;
  1020. if(p1.BIlisZero())
  1021. return result;
  1022. if(p2.BIlisZero())
  1023. return result;
  1024. if(TableModBase[0] != pp)
  1025. MakeModBaseTable(pp);
  1026. a = p1 % pp;
  1027. b = p2 % pp;
  1028. b.head = t = backToFront(b.head);
  1029. while( t != NULL)
  1030. {
  1031. result <<= 32;
  1032. if(t->num != 0)
  1033. {
  1034. c = a * t->num;
  1035. result = result + c;
  1036. }
  1037. if(result.BICompareABS(pp) >= 0)
  1038. {
  1039. for(i = 24; i >= 0; --i)
  1040. {
  1041. while(result.BICompareABS(TableModBase[i]) >= 0)
  1042. result = result - TableModBase[i];
  1043. }
  1044. }
  1045. t = t->next;
  1046. }
  1047. a.clear();
  1048. b.clear();
  1049. c.clear();
  1050. return result;
  1051. }
  1052. BigInt BImodmul2(const BigInt& p1, const BigInt& p2, const BigInt pp)
  1053. {
  1054. BigInt pre_a[16], a, b, pp2, pp4, pp8, result;
  1055. unsigned int ma1, ma2;
  1056. int i, j, test;
  1057. intNode* t;
  1058. a.copy(p1);
  1059. b.copy(p2);
  1060. test = a.BICompareABS(pp);
  1061. if(test > 0)
  1062. a = a % pp;
  1063. if(test == 0)
  1064. return result;
  1065. if(a.BIlisZero())
  1066. return result;
  1067. test = b.BICompareABS(pp);
  1068. if(test > 0)
  1069. b = b % pp;
  1070. if(test == 0)
  1071. return result;
  1072. if(b.BIlisZero())
  1073. return result;
  1074. pre_a[0].setZero();
  1075. pre_a[1].copy(a);
  1076. for(i = 2; i < 16; ++i)
  1077. {
  1078. pre_a[i] = pre_a[i - 1] + a;
  1079. if(pre_a[i].BICompareABS(pp) > 0)
  1080. pre_a[i] = pre_a[i] - pp;
  1081. }
  1082. pp2.copy(pp);
  1083. pp2 <<= 1;
  1084. pp4.copy(pp2);
  1085. pp4 <<= 1;
  1086. pp8.copy(pp4);
  1087. pp8 <<= 1;
  1088. b.head = t = backToFront(b.head);
  1089. while(t != NULL)
  1090. {
  1091. ma1 = 0xF0000000;
  1092. for(i = 7; i >= 0; --i)
  1093. {
  1094. result <<= 4;
  1095. ma2 = t->num & ma1;
  1096. if(ma2 != 0)
  1097. {
  1098. j = (i << 2);
  1099. ma2 >>= j;
  1100. result = result + pre_a[ma2 & 0xf];
  1101. }
  1102. while(result.BICompareABS(pp8) > 0)
  1103. result = result - pp8;
  1104. while(result.BICompareABS(pp4) > 0)
  1105. result = result - pp4;
  1106. while(result.BICompareABS(pp2) > 0)
  1107. result = result - pp2;
  1108. while(result.BICompareABS(pp) > 0)
  1109. result = result - pp;
  1110. ma1 >>= 4;
  1111. }
  1112. t = t->next;
  1113. }
  1114. for(i = 0; i < 16; ++i)
  1115. pre_a[i].clear();
  1116. a.clear();
  1117. b.clear();
  1118. pp2.clear();
  1119. pp4.clear();
  1120. pp8.clear();
  1121. return result;
  1122. }
  1123. BigInt BImodexp(const BigInt& p1, const BigInt& p2, const BigInt pp)
  1124. {
  1125. BigInt a, b, result;
  1126. a = p1 % pp;
  1127. b.copy(p2);
  1128. if(a.BIlisZero())
  1129. return result;
  1130. if(b.BIlisZero())
  1131. {
  1132. ++result;
  1133. return result;
  1134. }
  1135. ++result;
  1136. do{
  1137. if((b.head->num & 1) == 1)
  1138. result = BImodmul(a, result, pp);
  1139. b >>= 1;
  1140. if(! b.BIlisZero())
  1141. a = BImodmul(a, a, pp);
  1142. }while(! b.BIlisZero());
  1143. a.clear();
  1144. b.clear();
  1145. return result;
  1146. }
  1147. BigInt BImodpow2(const BigInt& p1, const BigInt& pp, const int& d)
  1148. {
  1149. BigInt result;
  1150. int i;
  1151. result.copy(p1);
  1152. for(i = d; i > 0; --i)
  1153. result = BImodmul(result, result, pp);
  1154. return result;
  1155. }
  1156. void MakePreTable(const BigInt& p, const BigInt& pp)
  1157. {
  1158. int i;
  1159. PreTable[0].setZero();
  1160. ++PreTable[0];
  1161. PreTable[1].copy(p);
  1162. for(i = 2; i < 16; ++i)
  1163. PreTable[i] = BImodmul2(PreTable[i - 1], p, pp);
  1164. }
  1165. BigInt BImodexp2(const BigInt& p1, const BigInt& p2, const BigInt pp)
  1166. {
  1167. BigInt a, b, result;
  1168. unsigned int tmp;
  1169. int iz, Lt, count, kZero;
  1170. if(p1.BIlisZero())
  1171. return result;
  1172. ++result;
  1173. if(p2.BIlisZero())
  1174. return result;
  1175. a = p1 % pp;
  1176. b.copy(p2);
  1177. if(a.BICompareABS(PreTable[1]) != 0)
  1178. MakePreTable(a, pp);
  1179. Lt = b.nodeNumber();
  1180. count = 32 * Lt;
  1181. intNode* t = b.head;
  1182. if(! TheLastNode(t))
  1183. {
  1184. result.positive = false;
  1185. return result;
  1186. }
  1187. kZero = FindZeroBit(t->num);
  1188. b <<= kZero;
  1189. count -= kZero;
  1190. do{
  1191. tmp = (t->num & 0xF0000000);
  1192. if(count > 3)
  1193. {
  1194. tmp >>= 28;
  1195. b <<= 4;
  1196. count -= 4;
  1197. }
  1198. else
  1199. {
  1200. tmp >>= (32 - count);
  1201. b <<= count;
  1202. count = 0;
  1203. }
  1204. result = BImodmul2(result, PreTable[tmp & 0x0f], pp);
  1205. iz = 0;
  1206. if(count != 0)
  1207. {
  1208. while(((t->num & 0x80000000) != 0x80000000) && (count > 0))
  1209. {
  1210. b <<= 1;
  1211. --count;
  1212. ++iz;
  1213. }
  1214. if(count > 3)
  1215. iz += 4;
  1216. else
  1217. iz += count;
  1218. result = BImodpow2(result, pp, iz);
  1219. }
  1220. }while(count != 0);
  1221. a.clear();
  1222. b.clear();
  1223. return result;
  1224. }
  1225. intNode* BigInt::getHead() const
  1226. {
  1227. return head;
  1228. }
  1229. void BigInt::print(int choose)
  1230. {
  1231. char* ch;
  1232. int lent;
  1233. switch(choose)
  1234. {
  1235. case 1:
  1236. ch = outputDEC(lent);
  1237. break;
  1238. case 2:
  1239. ch = outputHEX(lent);
  1240. break;
  1241. case 3:
  1242. ch = outputSTR(lent);
  1243. break;
  1244. default:
  1245. break;
  1246. }
  1247. for(int i = 0; i < lent; ++i)
  1248. cout << ch[i];
  1249. cout << endl;
  1250. delete[] ch;
  1251. }[/i][/i][/i][/i][/i][/i][/i][/i][/i][/i][/i][/i][/i][/i][/i][/i][/i][/i][/i]
复制代码

[ 本帖最后由 chinanic 于 2007-8-25 13:41 编辑 ]

TOP

RSA加密 源代码

  1. #ifndef RSA_H
  2. #define RSA_H
  3. #include<fstream>
  4. #include<cstdio>
  5. using namespace std;
  6. #include"BigInt.h"
  7. class rsa{
  8. public:
  9. rsa();
  10. ~rsa();
  11. void setkey(const BigInt& prp, const BigInt& prq, const BigInt& pubk);
  12. void setpubkey(const BigInt& pubk, const BigInt& n);
  13. void setMFileName(char* rsa_m);
  14. void setCFileName(char* rsa_c);
  15. void DeCode(int choose, int base);
  16. void EnCode(int choose, int base);
  17. private:
  18. void init();
  19. void DeCodingProcess(int choose, int base);
  20. void DeCodingSpecial(int choose, int base);
  21. void EnCodingProcess(int base);
  22. void EnCodingSpecial(int base);
  23. BigInt primeP;
  24. BigInt primeQ;
  25. BigInt modn;
  26. BigInt pubkey;
  27. BigInt prikey;
  28. BigInt fp;
  29. BigInt fq;
  30. char* rsach;
  31. char rsacFile[30];
  32. char rsamFile[30];
  33. int rsal;
  34. };
  35. rsa::rsa()
  36. {
  37. primeP = 952559;
  38. primeQ = 999983;
  39. pubkey = 11;
  40. init();
  41. rsach = NULL;
  42. strcpy(rsacFile, "c.txt");
  43. strcpy(rsamFile, "m.txt");
  44. }
  45. rsa::~rsa()
  46. {
  47. primeP.clear();
  48. primeQ.clear();
  49. modn.clear();
  50. pubkey.clear();
  51. prikey.clear();
  52. fp.clear();
  53. fq.clear();
  54. }
  55. void rsa::init()
  56. {
  57. modn = primeP * primeQ;
  58. --primeP;
  59. --primeQ;
  60. prikey = primeP * primeQ;
  61. prikey = pubkey.BIinverse(prikey);
  62. ++primeP;
  63. ++primeQ;
  64. rsal = modn.nodeNumber();
  65. rsal *= 32;
  66. intNode* t = modn.getHead();
  67. if(TheLastNode(t))
  68. rsal -= FindZeroBit(t->num);
  69. fp = primeP.BIinverse(primeQ);
  70. fq = primeQ.BIinverse(primeP);
  71. }
  72. void rsa::setkey(const BigInt& prp, const BigInt& prq, const BigInt& pubk)
  73. {
  74. primeP.copy(prp);
  75. primeQ.copy(prq);
  76. pubkey.copy(pubk);
  77. init();
  78. }
  79. void rsa::setpubkey(const BigInt& pubk, const BigInt& n)
  80. {
  81. pubkey.copy(pubk);
  82. modn.copy(n);
  83. rsal = modn.nodeNumber();
  84. rsal *= 32;
  85. intNode* t = modn.getHead();
  86. if(TheLastNode(t))
  87. rsal -= FindZeroBit(t->num);
  88. }
  89. void rsa::setMFileName(char* rsa_m)
  90. {
  91. strcpy(rsamFile, rsa_m);
  92. }
  93. void rsa::setCFileName(char* rsa_c)
  94. {
  95. strcpy(rsacFile, rsa_c);
  96. }
  97. //1. represent number based on decimal.
  98. //2. represent number based on hex.
  99. //3. represent with a = 01, b = 02........... to decimal
  100. void rsa::EnCode(int choose,int base)
  101. {
  102. switch(choose)
  103. {
  104. case 1:
  105. EnCodingProcess(base);
  106. break;
  107. case 2:
  108. EnCodingSpecial(base);
  109. break;
  110. default:
  111. break;
  112. }
  113. }
  114. void rsa:eCode(int choose, int base)
  115. {
  116. switch(choose)
  117. {
  118. case 1:
  119. DeCodingProcess(choose, base);
  120. break;
  121. case 2:
  122. DeCodingProcess(choose, base);
  123. break;
  124. case 3:
  125. DeCodingSpecial(choose, base);
  126. break;
  127. case 4:
  128. DeCodingSpecial(choose, base);
  129. break;
  130. default:
  131. break;
  132. }
  133. }
  134. void rsa::EnCodingProcess(int base)
  135. {
  136. FILE* fp;
  137. if( (fp = fopen(rsamFile, "r") ) == NULL)
  138. return;
  139. fstream out;
  140. out.open(rsacFile, ios:ut);
  141. int r_length;
  142. r_length = rsal  / 8;
  143. rsach = new char[r_length];
  144. BigInt temp;
  145. char* rsa_re;
  146. int rsa_leng;
  147. int count = 0;
  148. int i;
  149. int times = 0;
  150. while(! feof(fp))
  151. {
  152. rsach[count++] = fgetc(fp);
  153. if(count == r_length)
  154. {
  155. ++times;
  156. cout << "第 " << times << " 轮加密!";
  157. count = 0;
  158. temp.loadSTR(rsach, r_length);
  159. temp = BImodexp2(temp, pubkey, modn);
  160. if(base == 10)
  161. rsa_re = temp.outputDEC(rsa_leng);
  162. else
  163. rsa_re = temp.outputHEX(rsa_leng);
  164. for(i = 0; i < rsa_leng; ++i)
  165. out << rsa_re;
  166. delete[] rsa_re;
  167. out << endl;
  168. }
  169. }
  170. ++times;
  171. cout << "第 " << times << " 轮加密!";
  172. temp.loadSTR(rsach, count);
  173. temp = BImodexp2(temp, pubkey, modn);
  174. if(base == 10)
  175. rsa_re = temp.outputDEC(rsa_leng);
  176. else
  177. rsa_re = temp.outputHEX(rsa_leng);
  178. for(i = 0; i < rsa_leng; ++i)
  179. out << rsa_re;
  180. delete[] rsa_re;
  181. delete[] rsach;
  182. temp.clear();
  183. out.close();
  184. fclose(fp);
  185. }
  186. void rsa:eCodingProcess(int choose, int base)
  187. {
  188. fstream out;
  189. fstream in;
  190. in.open(rsacFile, ios::in);
  191. out.open(rsamFile, ios:ut);
  192. BigInt temp;
  193. BigInt temp1;
  194. BigInt temp2;
  195. int r_length;
  196. char* rr;
  197. r_length = rsal * 3  / 10;
  198. rsach = new char[r_length + 5];
  199. int times = 0;
  200. while(in >> rsach)
  201. {
  202. ++times;
  203. cout << "第 " << times << " 轮解密!";
  204. if(base == 10)
  205. temp.loadDEC(rsach, strlen(rsach));
  206. else
  207. temp.loadHEX(rsach, strlen(rsach));
  208. if(choose == 1)
  209. temp = BImodexp2(temp, prikey, modn);
  210. else
  211. {
  212. temp1 = BImodexp2(temp, prikey, primeP);
  213. temp2 = BImodexp2(temp, prikey, primeQ);
  214. temp1 = temp1 * fq;
  215. temp1 = temp1 * primeQ;
  216. temp2 = temp2 * fp;
  217. temp2 = temp2 * primeP;
  218. temp = temp1 + temp2;
  219. temp = temp % modn;
  220. }
  221. rr = temp.outputSTR(r_length);
  222. for(int i = 0; i < r_length; ++i)
  223. out << rr;
  224. out << endl;
  225. delete[] rr;
  226. }
  227. delete[] rsach;
  228. temp.clear();
  229. temp1.clear();
  230. temp2.clear();
  231. out.close();
  232. in.close();
  233. }
  234. void rsa::EnCodingSpecial(int base)
  235. {
  236. FILE* ffp;
  237. if( (ffp = fopen(rsamFile, "r") ) == NULL)
  238. return;
  239. fstream out;
  240. out.open(rsacFile, ios:ut);
  241. int r_length;
  242. r_length = rsal * 3  / 20;
  243. BigInt temp;
  244. int rsa_leng;
  245. int count = 0;
  246. unsigned int rsa_int;
  247. char rsa_c;
  248. int i;
  249. int times = 0;
  250. while(! feof(ffp))
  251. {
  252. rsa_c = fgetc(ffp);
  253. if((rsa_c <= ';z'; && rsa_c >= ';a';) || (rsa_c <= ';Z'; && rsa_c >= ';A';))
  254. {
  255. ++count;
  256. if(rsa_c <= ';Z'; && rsa_c >= ';A';)
  257. rsa_int = ((unsigned int) ( rsa_c - ';A';)) + 26;
  258. else
  259. rsa_int = ((unsigned int) ( rsa_c - ';a';));
  260. temp = temp * 100;
  261. temp += rsa_int;
  262. if(count == r_length)
  263. {
  264. ++times;
  265. cout << "第 " << times << " 轮加密!";
  266. count = 0;
  267. temp = BImodexp2(temp, pubkey, modn);
  268. if(base == 10)
  269. rsach = temp.outputDEC(rsa_leng);
  270. else
  271. rsach = temp.outputHEX(rsa_leng);
  272. temp.setZero();
  273. for(i = 0; i < rsa_leng; ++i)
  274. out << rsach;
  275. delete[] rsach;
  276. out << endl;
  277. }
  278. }
  279. }
  280. temp = BImodexp2(temp, pubkey, modn);
  281. if(base == 10)
  282. rsach = temp.outputDEC(rsa_leng);
  283. else
  284. rsach = temp.outputHEX(rsa_leng);
  285. for(i = 0; i < rsa_leng; ++i)
  286. out << rsach;
  287. delete[] rsach;
  288. temp.clear();
  289. out.close();
  290. fclose(ffp);
  291. }
  292. void rsa:eCodingSpecial(int choose, int base)
  293. {
  294. fstream out;
  295. fstream in;
  296. in.open(rsacFile, ios::in);
  297. out.open(rsamFile, ios:ut);
  298. BigInt temp;
  299. BigInt temp1;
  300. BigInt temp2;
  301. int r_length;
  302. char* rr;
  303. r_length = rsal * 3  / 10;
  304. rsach = new char[r_length + 5];
  305. rr = new char[r_length + 5];
  306. unsigned int rsa_int;
  307. int count;
  308. int times = 0;
  309. while(in >> rsach)
  310. {
  311. ++times;
  312. cout << "第 " << times << " 轮解密!";
  313. if(base == 10)
  314. temp.loadDEC(rsach, strlen(rsach));
  315. else
  316. temp.loadHEX(rsach, strlen(rsach));
  317. if(choose == 3)
  318. temp = BImodexp2(temp, prikey, modn);
  319. else
  320. {
  321. temp1 = BImodexp2(temp, prikey, primeP);
  322. temp2 = BImodexp2(temp, prikey, primeQ);
  323. temp1 = temp1 * fq;
  324. temp1 = temp1 * primeQ;
  325. temp2 = temp2 * fp;
  326. temp2 = temp2 * primeP;
  327. temp = temp1 + temp2;
  328. temp = (temp % modn);
  329. }   
  330. count = 0;
  331. while(! temp.BIlisZero())
  332. {
  333. rsa_int = temp % 100;
  334. temp = temp / 100;
  335. if(rsa_int >= 0 && rsa_int <= 25)
  336. rr[count ++] = (char)(rsa_int + ((unsigned int)';a';));
  337. else if(rsa_int >= 26 && rsa_int <= 51)
  338. rr[count ++] = (char)(rsa_int + ((unsigned int)';A';) - 26);
  339. }
  340. for(int i = 0; i < count; ++i)
  341. out << rr[count - i - 1];
  342. }
  343. delete[] rr;
  344. delete[] rsach;
  345. temp.clear();
  346. temp1.clear();
  347. temp2.clear();
  348. out.close();
  349. in.close();
  350. }
  351. #endif
复制代码

[ 本帖最后由 chinanic 于 2007-8-25 13:40 编辑 ]

TOP

RSA加密 源代码

  1. #ifndef RAND_H
  2. #define RAND_H
  3. #include"DESProcess.h"
  4. #include"BigInt.h"
  5. #include<fstream>
  6. #include<iostream>
  7. using namespace std;
  8. class Rand{
  9. public:
  10. Rand()
  11. {
  12. num = 2;
  13. length = 1000.;
  14. for(int i = 0; i < 2; ++i)
  15. k1[i] = k2[i] = x[i] = y[i] = z[i] = w[i] = v[i] = 0;
  16. ev = true;
  17. }
  18. ~Rand(){}
  19. void setKeyFile(char* c)
  20. {
  21. fstream in;
  22. in.open(c, ios::in);
  23. in >> setbase(16) >> k1[0] >> k1[1] >> k2[0] >> k2[1];
  24. in.close();
  25. }
  26. void setZVFile(char* c)
  27. {
  28. fstream in;
  29. in.open(c, ios::in);
  30. in >> setbase(16) >> z[0] >> z[1] >> v[0] >> v[1];
  31. in.close();
  32. }
  33. BigInt randStrPrime()
  34. {
  35. BigInt p;
  36. BigInt k;
  37. BigInt temp;
  38. cout << "生成第一个素因子.\n";
  39. temp = randPrime();
  40. while(k.BIlisZero())
  41. {
  42. k = randBigInt();
  43. k >>= 16;
  44. k <<= 1;
  45. }
  46. p = temp * k;
  47. temp = temp * 2;
  48. ++p;
  49. cout << "生成第二个素因子.\n";
  50. while(!isPrime(p))
  51. {
  52. cout << "新的一轮!" << endl;
  53. p = p + temp;
  54. }
  55. temp.copy(p);
  56. k = randBigInt();
  57. k <<= 96;
  58. if(ev)
  59. k <<= 4;
  60. ev = !ev;
  61. p = temp * k;
  62. temp = temp * 2;
  63. ++p;
  64. cout << "生成强素数.\n";
  65. while(!isPrime(p))
  66. {
  67. cout << "新的一轮!" << endl;
  68. p = p + temp;
  69. }
  70. cout << "强素数生成成功!"<< endl;
  71. k.clear();
  72. temp.clear();
  73. return p;
  74. }
  75. private:
  76. int randlength()
  77. {
  78. int randlength_temp;
  79. length = length * r_l + r_cc;
  80. length = fmod(length, r_M);
  81. randlength_temp =(int) (length / r_MM);
  82. if(randlength_temp == 0)
  83. ++randlength_temp;
  84. return  randlength_temp;
  85. }
  86. unsigned int randnumber()
  87. {
  88. num = r_laimda * num + r_c;
  89. return num;
  90. }
  91. BigInt randBigInt()
  92. {
  93. BigInt a;
  94. int ran;
  95. ran = randlength();
  96. for(int j = 0; j < ran; ++j)
  97. {
  98. a <<= 32;
  99. a += randnumber();
  100. }
  101. return a;
  102. }
  103. void randkey(unsigned int& r, unsigned& l)
  104. {//1
  105. d.setKey(k1[0], k1[1]);
  106. d.setBits(v[0], v[1]);
  107. d.desEnCode();
  108. d.outputBits(x[0], x[1]);
  109. //2
  110. d.setKey(x[0], x[1]);
  111. d.setBits(v[0], v[1]);
  112. d.desDeCode();
  113. d.outputBits(w[0], w[1]);
  114. //3
  115. d.setKey(z[0], z[1]);
  116. d.setBits(x[0], x[1]);
  117. d.desDeCode();
  118. d.outputBits(y[0], y[1]);
  119. //4
  120. d.setKey(y[0], y[1]);
  121. d.setBits(k2[0], k2[1]);
  122. d.desEnCode();
  123. d.outputBits(v[0], v[1]);
  124. //5
  125. d.setKey(k2[0], v[1]);
  126. d.setBits(w[0], w[1]);
  127. d.desEnCode();
  128. d.outputBits(r, l);
  129. //6
  130. d.setKey(k1[0], k1[1]);
  131. d.setBits(z[0], z[1]);
  132. d.desDeCode();
  133. d.outputBits(z[0], z[1]);
  134. }
  135. bool isPrime(const BigInt& p)
  136. {
  137. BigInt t;
  138. t.copy(p);
  139. --t;
  140. unsigned int last32 = t.getLast32Bit();
  141. int count = 0;
  142. while((last32 & 1) == 0)
  143. {
  144. t >>= 1;
  145. last32 = t.getLast32Bit();
  146. ++count;
  147. }
  148. BigInt a;
  149. BigInt re;
  150. int i, j;
  151. bool flag;
  152. unsigned int con = 1;
  153. char chd;
  154. for(i = 0; i < 100; ++i)
  155. {
  156. flag = false;
  157. if(i < 10)
  158. a = sta_p[i];
  159. else
  160. {
  161. if(i == 10)
  162. {
  163. cout << "前10次检测已经通过,要进行100次的检测吗(这可能很耗时间)(y/n)?" << endl;
  164. cin >> chd;
  165. if(chd == ';n'; || chd == ';N';)
  166. break;
  167. }
  168. a = randBigInt();
  169. }
  170. a.print(1);
  171. cout << "第 " << setbase(10) << i + 1 << " 次检查!"<< endl;
  172. re = BImodexp(a, t, p);
  173. if(count > 1)
  174. {
  175. if(re.BICompareABS(con) == 0)
  176. continue;
  177. ++re;
  178. if(re.BICompareABS(p) == 0)
  179. continue;
  180. --re;
  181. for(j = 2; j < count; ++j)
  182. {
  183. re = BImodmul2(re, re, p);
  184. if(re.BICompareABS(con) == 0)
  185. {
  186. t.clear();
  187. a.clear();
  188. re.clear();
  189. return false;
  190. }
  191. ++re;
  192. if(re.BICompareABS(p) == 0)
  193. {
  194. flag = true;
  195. break;
  196. }
  197. --re;
  198. }
  199. re = BImodmul2(re, re, p);
  200. }
  201. ++re;
  202. if(flag)
  203. continue;
  204. if(re.BICompareABS(p) != 0)
  205. {
  206. t.clear();
  207. a.clear();
  208. re.clear();
  209. return false;
  210. }
  211. }
  212. t.clear();
  213. a.clear();
  214. re.clear();
  215. return true;
  216. }
  217. BigInt randPrime()
  218. {
  219. int i;
  220. BigInt p;
  221. unsigned int right, left;
  222. unsigned int temp;
  223. for(i = 0; i < 2; ++i)
  224. {
  225. randkey(right, left);
  226. p <<= 32;
  227. p += right;
  228. p <<= 32;
  229. p += left;
  230. }
  231. temp = p % 30;
  232. p += (29 - temp);
  233. while(true)
  234. {//30 as a circle
  235. cout << "新的一轮!" << endl;
  236. p += 2;
  237. if(isPrime(p))
  238. return p;
  239. cout << "新的一轮!" << endl;
  240. p += 6;
  241. if(isPrime(p))
  242. return p;
  243. cout << "新的一轮!" << endl;
  244. p += 4;
  245. if(isPrime(p))
  246. return p;
  247. cout << "新的一轮!" << endl;
  248. p += 2;
  249. if(isPrime(p))
  250. return p;
  251. cout << "新的一轮!" << endl;
  252. p += 4;
  253. if(isPrime(p))
  254. return p;
  255. cout << "新的一轮!" << endl;
  256. p += 2;
  257. if(isPrime(p))
  258. return p;
  259. cout << "新的一轮!" << endl;
  260. p += 4;
  261. if(isPrime(p))
  262. return p;
  263. cout << "新的一轮!" << endl;
  264. p += 6;
  265. if(isPrime(p))
  266. return p;
  267. }
  268. }
  269. unsigned int num;
  270. double length;
  271. unsigned int k1[2];
  272. unsigned int k2[2];
  273. unsigned int x[2];
  274. unsigned int y[2];
  275. unsigned int z[2];
  276. unsigned int w[2];
  277. unsigned int v[2];
  278. bool ev;
  279. DESProcess d;
  280. };
  281. #endif
复制代码

[ 本帖最后由 chinanic 于 2007-8-25 13:41 编辑 ]

TOP

RSA加密 源代码

  1. #include<iostream>
  2. #include<string>
  3. #include<iomanip>
  4. using namespace std;
  5. #include"predefine.h"
  6. #include"rand.h"
  7. #include"rsa.h"
  8. void clearintNode()
  9. {
  10. intNode i;
  11. i.clear();
  12. }
  13. void printMenu()
  14. {
  15. cout << "__________________________\n";
  16. cout << "请选择:\n";
  17. cout << "1.产生两个随机大素数并写入文件(16进制)!\n";
  18. cout << "2.产生两个随机大素数并写入文件(10进制)!\n";
  19. cout << "3.从文件读入素数和公钥并解密(私钥自动生成)(16进制)!\n";
  20. cout << "4.从文件读入素数和公钥并解密(私钥自动生成)(10进制)!\n";
  21. cout << "5.从文件读入模数和公钥并加密(16进制)!\n";
  22. cout << "6.从文件读入模数和公钥并加密(10进制)!\n";
  23. cout << "0.退出!\n";
  24. }
  25. void creatPrime(int base)
  26. {
  27. Rand r;
  28. char c[30];
  29. cout << "请输入用DES产生随机素数所需密钥所在的文件名!\n";
  30. cin >> c;
  31. r.setKeyFile(c);
  32. cout <<"请输用产生随机数的种子所在的文件名!\n";
  33. cin >> c;
  34. r.setZVFile(c);
  35. cout << "请输入用来保存素数的文件名!\n";
  36. cin >> c;
  37. fstream out;
  38. out.open(c, ios:ut);
  39. char* ch;
  40. int ch_l;
  41. BigInt p, q, n;
  42. p = r.randStrPrime();
  43. if(base == 10)
  44. ch = p.outputDEC(ch_l);
  45. else
  46. ch = p.outputHEX(ch_l);
  47. for(int i = 0; i < ch_l; ++i)
  48. out << ch;
  49. out << endl;
  50. delete[] ch;
  51. q = r.randStrPrime();
  52. if(base == 10)
  53. ch = q.outputDEC(ch_l);
  54. else
  55. ch = q.outputHEX(ch_l);
  56. for(i = 0; i < ch_l; ++i)
  57. out << ch;
  58. out << endl;
  59. cout << "要在文件后面加入加密时所要的公钥吗?\n";
  60. char judge;
  61. cin >> judge;
  62. if(judge == ';y'; || judge == ';Y';)
  63. {
  64. cout << "请输入公钥( " << base <<" 进制)!\n";
  65. cin >> ch;
  66. out << ch << endl;
  67. out.close();
  68. cout << "要新建一个文件并保存公钥和模数吗?";
  69. cin >> judge;
  70. if(judge == ';y'; || judge == ';Y';)
  71. {
  72. cout << "请输入要保存公钥的文件名:";
  73. cin >> c;
  74. out.open(c, ios:ut);
  75. out << ch << endl;
  76. delete[] ch;
  77. n = p * q;
  78. if(base == 10)
  79. ch = n.outputDEC(ch_l);
  80. else
  81. ch = n.outputHEX(ch_l);
  82. for(i = 0; i < ch_l; ++i)
  83. out << ch;
  84. out << endl;
  85. cout << "写入成功!" << endl;
  86. out.close();
  87. delete[] ch;
  88. }
  89. }
  90. q.clear();
  91. p.clear();
  92. n.clear();
  93. }
  94. void decodingFile(int base)
  95. {
  96. rsa r;
  97. char name[30];
  98. cout << "请输入素数和公钥所在的文件名:\n";
  99. cin >> name;
  100. fstream in;
  101. in.open(name, ios::in);
  102. BigInt p, q, pub;
  103. char num[150];
  104. in >> num;
  105. if(base == 10)
  106. p.loadDEC(num, strlen(num));
  107. else
  108. p.loadHEX(num, strlen(num));
  109. in >> num;
  110. if(base == 10)
  111. q.loadDEC(num, strlen(num));
  112. else
  113. q.loadHEX(num, strlen(num));
  114. in >> num;
  115. if(base == 10)
  116. pub.loadDEC(num, strlen(num));
  117. else
  118. pub.loadHEX(num, strlen(num));
  119. r.setkey(p, q, pub);
  120. in.close();
  121. cout << "请输入要解密的文件名:\n";
  122. cin >> name;
  123. r.setCFileName(name);
  124. cout << "请输入要保存明文的文件名:\n";
  125. cin >> name;
  126. r.setMFileName(name);
  127. cout << "请选择:\n"
  128. << "1.读入文件的ASCII码解密!\n"
  129. << "2.读入文件的ASCII码用中国剩余定理解密!\n"
  130. << "3.以a = 00, b = 01, .........A = 26, B = 27..........解密!\n"
  131. << "4.以a = 00, b = 01, .........A = 26, B = 27..........用中国剩余定理解密!\n";
  132. cout << "0.返回!" << endl;
  133. int choose;
  134. cin >> choose;
  135. if(choose != 0)
  136. {
  137. r.DeCode(choose, base);
  138. cout << "解密成功!" << endl;
  139. }
  140. p.clear();
  141. q.clear();
  142. pub.clear();
  143. }
  144. void encodingFile(int base)
  145. {
  146. rsa r;
  147. char name[30];
  148. cout << "请输入公钥和模数所在的文件名:\n";
  149. cin >> name;
  150. fstream in;
  151. in.open(name, ios::in);
  152. BigInt pub, n;
  153. char num[300];
  154. in >> num;
  155. if(base == 10)
  156. pub.loadDEC(num, strlen(num));
  157. else
  158. pub.loadHEX(num, strlen(num));
  159. in >> num;
  160. if(base == 10)
  161. n.loadDEC(num, strlen(num));
  162. else
  163. n.loadHEX(num, strlen(num));
  164. r.setpubkey(pub, n);
  165. in.close();
  166. cout << "请输入要加密的文件名:\n";
  167. cin >> name;
  168. r.setMFileName(name);
  169. cout << "请输入要保存密文的文件名:\n";
  170. cin >> name;
  171. r.setCFileName(name);
  172. cout << "请选择:\n"
  173. << "1.读入文件的ASCII码加密!\n"
  174. << "2.以a = 00, b = 01, .........A = 26, B = 27..........加密!\n";
  175. cout << "0.返回!" << endl;
  176. int choose;
  177. cin >> choose;
  178. if(choose != 0)
  179. {
  180. r.EnCode(choose, base);
  181. cout << "加密成功!" << endl;
  182. }
  183. n.clear();
  184. pub.clear();
  185. n.clear();
  186. }
  187. int main()
  188. {
  189. int choose;
  190. while(true)
  191. {
  192. printMenu();
  193. cin >> choose;
  194. switch(choose)
  195. {
  196. case 1:
  197. creatPrime(16);
  198. break;
  199. case 2:
  200. creatPrime(10);
  201. break;
  202. case 3:
  203. decodingFile(16);
  204. break;
  205. case 4:
  206. decodingFile(10);
  207. break;
  208. case 5:
  209. encodingFile(16);
  210. break;
  211. case 6:
  212. encodingFile(10);
  213. break;
  214. default:
  215. break;
  216. }
  217. if(choose == 0)
  218. break;
  219. }
  220. clearintNode();
  221. return 0;
  222. }
复制代码

[ 本帖最后由 chinanic 于 2007-8-25 13:40 编辑 ]

TOP

怎么看不到附件

TOP

经典····

TOP

怎没有附件啊

看不见上传的东东啊

TOP

返回列表 回复 发帖