encryption - RSA Simple example with Javascript? -
i know may kind of basic having exam on next week , wanted test knowledge of rsa.
i have simple problem working on see mechanics of rsa, , want test if can encrypt , decrypt message in browser console.
the rsa variables are:
var p = 11 //the first prime number var q = 5 //the second prime number var m=72 // message var n=55 // rsa modulus, n = p*q var e=7 // (e,n) public key var d=23 //so (d,n) private key var fi_of_n=40 //the totient, fi_of_n = (p-1)*(q-1) = 40
to encrypt, doing:
var c = math.pow(m,e)%n // m^e mod(n); c=8
i c equals 8
then want decrypt, using d follows:
var m2 = math.pow(c,d)%n // c^d mod(n)
but result 17! not 72 expected. don't know doing wrong , think simple example.
the values of public , private key ones obtained in video: https://www.youtube.com/watch?v=kyasb426yjk
rsa has limitation messages strictly smaller modulus can encrypted. message of 72 big modulus of 55. can see encryption , decryption work, because 17 = 72 (mod 55).
in javascript numbers floating point numbers. there maximum number can safely represented integer: number.max_safe_integer = 9007199254740991
. if result of power operation exceeds value, nature of floating point operation cannot ensure correct value back. during encryption , decryption such cutoff can happen twice.
let's take m = 5
example. pow(5, 7) = 78125
smaller 9007199254740991
. there no cutoff here. now, c = 25
math.pow(25, 23) = 1.4210854715202004e+32
far bigger safe integer. when apply modulo operator, won't give correct result, because floating point resolution not 1 anymore.
the message 8
example produces small enough pow(m,e)
value smaller maximum safe integer value and resulting ciphertext produces small enough pow(c,d)
value isn't cut off.
you should either use big number (multiple precision) library operations or use python can represent integer long have enough memory. if want keep doing in javascript, there jsbn implements rsa in addition big number library.
Comments
Post a Comment