我正在尝试使用公钥+私钥加密一些文本数据,但我看到的只是 HTML 方面的 NaN 。
我尝试过更改某些变量的性质,但没有成功。谢谢
function letterValue(str){
var anum={
a: 1, b: 2, c: 3, d: 4, e: 5, f: 6, g: 7, h: 8, i: 9, j: 10, k: 11,
l: 12, m: 13, n: 14,o: 15, p: 16, q: 17, r: 18, s: 19, t: 20,
u: 21, v: 22, w: 23, x: 24, y: 25, z: 26
}
if(str.length== 1) return anum[str] || ' ';
return str.split('').map(letterValue);
}
function encryptText() {
var p = document.getElementById("num1").value;
var q = document.getElementById("num2").value;
var n = p*q;
const e = Math.pow(2, 16) + 1;
var plaintext = document.getElementById('plaintext');
var c = letterValue(String(plaintext));
var ciphertext = c.map((el) => Math.pow(el, e) % n);
document.getElementById("output3").innerHTML = "Encrypted text = " + ciphertext;
}
我期望收到字母数组,但这不是我收到的。
最佳答案
Javascript 数字的精度有限。
Math.pow(x, Math.pow(2, 16) + 1)
将超过最大可表示值 (1.7976931348623157e+308
, Number.MAX_VALUE
) 很快。
例如x >= 2
将始终导致无穷大
。
并且 Infinity % anyNumber
是 NaN
,这就是为什么你会得到很多 NaN
。
要解决此问题,您需要使用任意精度数字库, 例如:
- big.js
- decimal.js
- crunch.js
- 等等...
如果您使用像 babel 这样的转译器,或者不介意您的代码在某些浏览器上无法运行,您可以尝试新的 BigInt
类型。
使用 BigInt
的示例:
function letterValue(str){
if(str.length== 1) {
if(str >= "a" && str <= "z")
return str.charCodeAt(0) - 'a'.charCodeAt(0) + 1;
else
return 0;
}
return str.split('').map(letterValue);
}
function encryptText(text) {
// TODO: choose better values for p and q
var p = 173n;
var q = 149n;
var n = p*q;
// beware: you should avoid Math functions with BigInts, because
// those operate on numbers, so you will loose precision with them.
// you can use ** instead of Math.pow though to work around that.
const e = 2n ** 16n + 1n;
var c = letterValue(String(text));
var ciphertext = c.map(el => (BigInt(el) ** e) % n);
return ciphertext;
}
// the .map(e => e.toString()) is only for stackoverflow to correctly display the result.
console.log(encryptText("abc").map(e => e.toString()));
编辑:完整的 RSA 工作示例:
// choose two distinct primes: p, q
const p = 173n;
const q = 149n;
const n = p * q;
const phi = (p - 1n) * (q - 1n);
// choose two integer numbers e,d so that
// gcd(e, phi) == 1
// e != d
// (e*d) % phi == 1
// In a normal RSA implementation e would be picked randomly from this set of values: 3,5,17,257,65537
// and then calculate d = modinv(e, phi)
const e = 3n;
const d = 16971n;
// then this will be your private / public keys:
const privateKey = {d, n};
const publicKey = {e, n};
function encrypt(message) {
let chars = message.split('');
return chars.map(c => {
let charValue = BigInt(c.charCodeAt(0));
return (charValue ** publicKey.e) % publicKey.n;
});
}
function decrypt(message) {
return message.map(c => {
let charCode = (c ** privateKey.d) % privateKey.n;
return String.fromCharCode(Number(charCode));
}).join('');
}
let message = "i love cookies";
let crypt = encrypt(message);
console.log("Encrypted: ", crypt.map(e => e.toString()));
console.log("Decrypted: ", decrypt(crypt));
关于javascript - 如何从数字数组输出字母数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57255905/