HELP!!! Weird problem...

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Mar 2006
Posts: 2
Reputation: MAGiC333X is an unknown quantity at this point 
Solved Threads: 0
MAGiC333X MAGiC333X is offline Offline
Newbie Poster

HELP!!! Weird problem...

 
0
  #1
Mar 22nd, 2006
Hello,

I'm having a pretty weird problem with a program i created,
it's going to be a RSA encrypter...

I've made a BigNum class that is able to do multiplications, additions, decreament, divisions and modulo on very large numbers...

The size of these numbers is defined by a contant (named range),
everything works fine if the range <= 5104 bytes, but for some sort of reason when i go above this number it won't work correctly anymore, i have a system("PAUSE"); at the end of my code, but it will just go past it and past some other calculations in my function main()... Lets say range=6104, than it will only show input, not output of other calculations, but if i delete the multiplication and division from the main() it will show the output of the modulo correctly!

I guess it has to do with memory/buffer overflow of some kind, but if it where a normal overflow of some sort it should also give errors using a number other than 512 ('cause at first i didn't use a constant for the size of the numbers, but just 512)...

I haven't got a clue what is wrong...
Help is very much appreciated!

Here is the code (sorry it's a bit long):
BTW: I'm using Borland C++ 5.0, but should compile using other compilers

  1. #include <iostream>
  2. #include <stdlib>
  3. #include <stdio>
  4. #include <memory>
  5. #include <string>
  6.  
  7. const int range=5104;
  8. char strnum[range*2+1];
  9.  
  10. class BigNum{
  11. public:
  12. unsigned char num[range];
  13. int len;
  14. BigNum(){memset(num, 0, range); len=1;}
  15. BigNum(char*);
  16. BigNum operator + (BigNum);
  17. BigNum operator - (BigNum);
  18. BigNum operator * (BigNum);
  19. BigNum operator / (BigNum);
  20. BigNum operator % (BigNum);
  21. BigNum operator << (int);
  22. bool operator < (BigNum);
  23. char* print();
  24. };
  25.  
  26. BigNum::BigNum(char* param){ //Convert hex input to char number array
  27. memset(num, 0, range);
  28. int tmp=strlen(param);
  29. len=tmp/2;
  30. for(int i=0;i<tmp;i++){
  31. if((param[tmp-i-1]>=48)&&(param[tmp-i-1]<=57)){param[tmp-i-1]-=48;}
  32. if((param[tmp-i-1]>=65)&&(param[tmp-i-1]<=70)){param[tmp-i-1]-=55;}
  33. if((i%2)==1){num[(range-1)-i/2]|=param[tmp-i-1]<<4;}
  34. else{num[(range-1)-i/2]=param[tmp-i-1];}
  35. }
  36. }
  37.  
  38. BigNum BigNum::operator + (BigNum param){
  39. int tmpi;
  40. unsigned char tmpc;
  41. BigNum tmp;
  42. memcpy(tmp.num, num, range); tmp.len=len;
  43.  
  44. for(int i=0;i<param.len;i++){
  45. if(tmp.len<i+1){tmp.len++;}
  46. tmpc=tmp.num[(range-1)-i];
  47. tmp.num[(range-1)-i]+=param.num[(range-1)-i];
  48. if(tmpc>tmp.num[(range-1)-i]){
  49. tmpi=0;
  50. while(tmp.num[(range-1)-i-1-tmpi]==0xFF){tmp.num[(range-1)-i-1-tmpi]=0; tmpi++;}
  51. tmp.num[(range-1)-i-1-tmpi]++;
  52. if(tmp.len<i+2+tmpi){tmp.len++;}
  53. }
  54. }
  55.  
  56. return tmp;
  57. }
  58.  
  59. BigNum BigNum::operator - (BigNum param){
  60. int tmpi;
  61. unsigned char tmpc;
  62. BigNum tmp;
  63. memcpy(tmp.num, num, range); tmp.len=len;
  64.  
  65. for(int i=0;i<param.len;i++){
  66. tmpc=tmp.num[(range-1)-i];
  67. tmp.num[(range-1)-i]-=param.num[(range-1)-i];
  68. if(tmpc<tmp.num[(range-1)-i]){
  69. tmpi=0;
  70. while(tmp.num[(range-1)-i-1-tmpi]==0x00){tmp.num[(range-1)-i-1-tmpi]=0xFF; tmpi++;}
  71. tmp.num[(range-1)-i-1-tmpi]--;
  72. }
  73. for(tmpi=0;((tmpi<range)&&(tmp.num[tmpi]==0));tmpi++){}
  74. tmp.len=range-tmpi;
  75. }
  76.  
  77. return tmp;
  78. }
  79.  
  80. bool BigNum::operator < (BigNum param){
  81. int tmpi=0;
  82. if(len<param.len){return true;}
  83. if(len==param.len){
  84. while((tmpi<len)&&(num[range-len+tmpi]==param.num[range-param.len+tmpi])){tmpi++;}
  85. if(num[range-len+tmpi]<param.num[range-param.len+tmpi]){return true;}
  86. }
  87. return false;
  88. }
  89.  
  90. BigNum BigNum::operator << (int shl){
  91. BigNum tmp;
  92. int tmpi;
  93. unsigned char and, tmpc;
  94. memcpy(tmp.num+range-len-shl/8, num+range-len, len);
  95. tmp.len=len+shl/8;
  96. if((shl%8)>0){
  97. tmpi=shl%8;
  98. and=(2<<tmpi)-1;
  99. and<<=8-tmpi;
  100.  
  101. for(int i=0;i<tmp.len;i++){
  102. tmpc=(tmp.num[range-tmp.len+i]&and)>>8-tmpi;
  103. tmp.num[range-tmp.len+i]<<=tmpi;
  104. tmp.num[(range-1)-tmp.len+i]|=tmpc;
  105. }
  106. if(tmp.num[(range-1)-tmp.len]>0){tmp.len++;}
  107. }
  108. return tmp;
  109. }
  110.  
  111. BigNum BigNum::operator * (BigNum param){
  112. BigNum tmp, out;
  113. memcpy(tmp.num, num, range); tmp.len=len;
  114.  
  115. for(int i=0;i<param.len;i++){
  116. for(int ix=0;ix<8;ix++){
  117. if((param.num[(range-1)-i]&1)==1){
  118. out=out+(tmp<<(i*8+ix));
  119. }
  120. param.num[(range-1)-i]>>=1;
  121. }
  122. }
  123. for(int i=0;((i<range)&&(out.num[i]==0));i++){out.len=(range-1)-i;}
  124.  
  125. return out;
  126. }
  127.  
  128. BigNum BigNum::operator / (BigNum param){
  129. int tmpi;
  130. BigNum tmp, out, one=BigNum("01");
  131. memcpy(tmp.num, num, range); tmp.len=len;
  132. if(tmp<param){return BigNum("00");}
  133.  
  134. while(param<tmp){
  135. tmpi=0;
  136. while((param<<tmpi)<tmp){tmpi++;}
  137. if(tmpi==0){break;}
  138. tmpi--;
  139. tmp=tmp-(param<<tmpi);
  140. out=out+(one<<tmpi);
  141. }
  142. if(!(tmp<param)){
  143. out=out+one;
  144. tmp=tmp-param;
  145. }
  146. return out;
  147. }
  148.  
  149. BigNum BigNum::operator % (BigNum param){
  150. int tmpi;
  151. BigNum tmp, out, one=BigNum("01");
  152. memcpy(tmp.num, num, range); tmp.len=len;
  153. if(tmp<param){return BigNum("00");}
  154.  
  155. while(param<tmp){
  156. tmpi=0;
  157. while((param<<tmpi)<tmp){tmpi++;}
  158. if(tmpi==0){break;}
  159. tmpi--;
  160. tmp=tmp-(param<<tmpi);
  161. out=out+(one<<tmpi);
  162. }
  163. if(!(tmp<param)){
  164. out=out+one;
  165. tmp=tmp-param;
  166. }
  167. return tmp;
  168. }
  169.  
  170. char* BigNum::print(){
  171. int tmp=0;
  172. char *hex="0123456789ABCDEF";
  173.  
  174. for(int i=0;i<len;i++){
  175. strnum[tmp]=hex[(num[range-len+i]&0xF0)>>4];
  176. strnum[tmp+1]=hex[(num[range-len+i]&0x0F)];
  177. tmp+=2;
  178. }
  179. strnum[tmp]=0;
  180.  
  181. return strnum;
  182. }
  183.  
  184. int main(){
  185. BigNum tmp("123456789ABCDEF7");
  186. BigNum test("3F12");
  187. BigNum c;
  188.  
  189. cout << "----INPUT:----\n";
  190. printf("A:%s: %d Bytes\n", tmp.print(), tmp.len);
  191. printf("B:%s: %d Bytes\n--------------\n\n", test.print(), test.len);
  192. c=tmp*test;
  193. printf("--MULTIPLY(A*B)--\n%s\n%d Bytes\n\n", c.print(), c.len);
  194. c=tmp/test;
  195. printf("--DIVIDE(A/B)--\n%s\n%d Bytes\n\n", c.print(), c.len);
  196. c=tmp%test;
  197. printf("--MODULO(A MOD B)--\n%s\n%d Bytes\n", c.print(), c.len);
  198.  
  199. system("PAUSE");
  200. return 0;
  201. }
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 5,264
Reputation: iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold 
Solved Threads: 377
Featured Poster
iamthwee's Avatar
iamthwee iamthwee is offline Offline
Posting Expert

Re: HELP!!! Weird problem...

 
0
  #2
Mar 22nd, 2006
This may not be of help if this is homework...

http://www.swox.com/gmp/

why bother trying to reinvent the wheel?
*Voted best profile in the world*
Reply With Quote Quick reply to this message  
Join Date: Mar 2006
Posts: 2
Reputation: MAGiC333X is an unknown quantity at this point 
Solved Threads: 0
MAGiC333X MAGiC333X is offline Offline
Newbie Poster

Re: HELP!!! Weird problem...

 
0
  #3
Mar 22nd, 2006
It's sort of homework...
So that's no help for me, but thanks anyway...

BTW: reinventing the wheel rocks :p
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the C++ Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC