RSA Question
-
im trying to implement the algorithm of RSA for fun...but im having a problem with the mod function in .net returning the wrong values. the algorithm im using is. Encrypt: value^e mod PQ Decrypt: (x^e mod PQ)^d mod PQ= value; A implementation i use is hard coded...and i have varifyed the values with the windows calculator and found that the awnser is returned correctly. Here is where is the code with the problem
double E = 17 ; double D = 2753; double PQ = 3233; double P = 61 ; //<- first prime number (destroy this after computing E and D) double Q = 53 ; //<- second prime number (destroy this after computing E and D) //to encrypt i do this. the message/int to encrypt will be 123 //(123^E) mod PQ = (123^17) mod 3233 = 855 //the above code will return 855. try it in the windows calc and it will return it int i = Math.Pow(123,E)%PQ; //this above code..in .Net using the same logic as above , yet it returns me 922 ?? What am i doing wrong ? i have varifyed that Math.Pow(123,E) is raising the number to the correct value. but i think the problem lies in the mod function.
May it be that my chosen key pairs and primes are in correct ? Thanks for your time peoples Jesse M The Code Project Is Your Friend... -
im trying to implement the algorithm of RSA for fun...but im having a problem with the mod function in .net returning the wrong values. the algorithm im using is. Encrypt: value^e mod PQ Decrypt: (x^e mod PQ)^d mod PQ= value; A implementation i use is hard coded...and i have varifyed the values with the windows calculator and found that the awnser is returned correctly. Here is where is the code with the problem
double E = 17 ; double D = 2753; double PQ = 3233; double P = 61 ; //<- first prime number (destroy this after computing E and D) double Q = 53 ; //<- second prime number (destroy this after computing E and D) //to encrypt i do this. the message/int to encrypt will be 123 //(123^E) mod PQ = (123^17) mod 3233 = 855 //the above code will return 855. try it in the windows calc and it will return it int i = Math.Pow(123,E)%PQ; //this above code..in .Net using the same logic as above , yet it returns me 922 ?? What am i doing wrong ? i have varifyed that Math.Pow(123,E) is raising the number to the correct value. but i think the problem lies in the mod function.
May it be that my chosen key pairs and primes are in correct ? Thanks for your time peoples Jesse M The Code Project Is Your Friend...I think the problem is arising because you are using doubles to store your values. The number 123^17 probably can't be stored exactly as a double. You should use a BigInteger class that can hold as many bits as you want for your encryption algorithm. Check out this article for some good info on implementing a BigInteger class: http://www.codeproject.com/csharp/BigInteger.asp[^] Using that class you will get the correct ciphertext (855) with this code:
BigInteger e = new BigInteger(17);
BigInteger d = new BigInteger(2753);
BigInteger p = new BigInteger(61);
BigInteger q = new BigInteger(53);
BigInteger n = p * q;BigInteger P = new BigInteger(123);
BigInteger C = P.modPow(e, n);