I'm trying to perform a calculation in Javascript (n^e mod n) for each element e in my array, then output that new array subsequently created. How would I do this? This is what I've figured out so far, but the code doesn't work.
This is what I've figured out so far, but the code doesn't work.
function encryptText() {
var plaintext = document.getElementById('plaintext').value;
var n = letterValue(String(plaintext));
ciphertext = array()
foreach(addon_array as key => col) {
ciphertext[key] = Math.pow(col, e) % n;
}
document.getElementById("output3").innerHTML = "Encrypted text = " + ciphertext;
}
I hope to get an array of modified integers (the ciphertext) as a result. Thank you
I'm trying to perform a calculation in Javascript (n^e mod n) for each element e in my array, then output that new array subsequently created. How would I do this? This is what I've figured out so far, but the code doesn't work.
This is what I've figured out so far, but the code doesn't work.
function encryptText() {
var plaintext = document.getElementById('plaintext').value;
var n = letterValue(String(plaintext));
ciphertext = array()
foreach(addon_array as key => col) {
ciphertext[key] = Math.pow(col, e) % n;
}
document.getElementById("output3").innerHTML = "Encrypted text = " + ciphertext;
}
I hope to get an array of modified integers (the ciphertext) as a result. Thank you
Share Improve this question edited Apr 28, 2019 at 21:15 Daniel Cottone 4,48029 silver badges39 bronze badges asked Apr 28, 2019 at 20:22 Olaf WillnerOlaf Willner 855 bronze badges3 Answers
Reset to default 6You can use Javascript's map() function on an array.
const arr = [1, 2, 3];
const newArr = arr.map(i => i * 2);
// should be [2, 4, 6]
console.log(newArr);
Use Javascript map function, like so:
function encryptText() {
var plaintext = document.getElementById('plaintext').value;
var n = letterValue(String(plaintext));
ciphertext = addon_array.map((el) => Math.pow(el, e) % n);
document.getElementById("output3").innerHTML = "Encrypted text = " + ciphertext;
}
You are using php syntax in javascript :)
in js it should look like this
function encryptText() {
var plaintext = document.getElementById('plaintext').value;
var n = letterValue(String(plaintext));
var ciphertext = []
for(var key in addon_array) {
let col = addon_array[key]
ciphertext[key] = Math.pow(col, e) % n;
}
document.getElementById("output3").innerHTML = "Encrypted text = " + ciphertext;
}
But as mentioned before, in js better approach is to use Array.map function
function encryptText() {
var plaintext = document.getElementById('plaintext').value;
var n = letterValue(String(plaintext));
var ciphertext = addon_array.map((el) => Math.pow(el, e) % n);
document.getElementById("output3").innerHTML = "Encrypted text = " + ciphertext;
}
it should work if you sure, that addon_array
is really Array, not an Object. Arrays in js are slightly different from php. Read more here
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744181494a4562004.html
评论列表(0条)