gpt4 book ai didi

javascript - 如何从数字数组输出字母数组

转载 作者:行者123 更新时间:2023-11-29 18:40:44 24 4
gpt4 key购买 nike

我正在尝试使用公钥 + 私钥加密一些文本数据,但我所看到的只是 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

Infinity % anyNumberNaN,这就是为什么您会得到很多 NaN

要解决此问题,您需要使用任意精度数字库,例如:

如果您使用的是像 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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com