Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. RSA Question

RSA Question

Scheduled Pinned Locked Moved C#
questioncsharpalgorithmshelp
2 Posts 2 Posters 1 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • J Offline
    J Offline
    jtmtv18
    wrote on last edited by
    #1

    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...

    K 1 Reply Last reply
    0
    • J jtmtv18

      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...

      K Offline
      K Offline
      Kastro
      wrote on last edited by
      #2

      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);

      1 Reply Last reply
      0
      Reply
      • Reply as topic
      Log in to reply
      • Oldest to Newest
      • Newest to Oldest
      • Most Votes


      • Login

      • Don't have an account? Register

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • World
      • Users
      • Groups