Hey Everyone,
I have been working on encrypting strings with JS and a public key. Utilizing the JSBN code has been helpful, but I am encountering an issue with creating a BigInt.
Here is what RSA.js does:
// Copyright (c) 2005 Tom Wu
var m = pkcs1pad2(text,(this.n.bitLength()+7)>>3); // obtaining the BigInt
if(m == null) return null; // checking for null
var c = this.doPublic(m); // encrypting the BigInt
The problem lies within the "pkcs1pad2" function. It includes a condition to verify if the length of the text exceeds the BitLength of the key. If it does, it returns; otherwise, it creates a BigInt.
// Copyright (c) 2005 Tom Wu
if(n < s.length + 11) { // TODO: fixing for utf-8
alert("Message too long for RSA");
return null;
}
var ba = new Array();
var i = s.length - 1;
while(i >= 0 && n > 0) {
var c = s.charCodeAt(i--);
if(c < 128) { // encoding using utf-8
ba[--n] = c;
} else if((c > 127) && (c < 2048)) {
ba[--n] = (c & 63) | 128;
ba[--n] = (c >> 6) | 192;
} else {
ba[--n] = (c & 63) | 128;
ba[--n] = ((c >> 6) & 63) | 128;
ba[--n] = (c >> 12) | 224;
}
}
ba[--n] = 0;
var rng = new SecureRandom();
var x = new Array();
while(n > 2) { // random non-zero pad
x[0] = 0;
while(x[0] == 0) rng.nextBytes(x);
ba[--n] = x[0];
}
ba[--n] = 2;
ba[--n] = 0;
return new BigInteger(ba);
I am unsure about the author's note "//TODO: fix for utf-8". Can someone provide an explanation and a solution?