62 lines
1.7 KiB
JavaScript
62 lines
1.7 KiB
JavaScript
let encrypt = function() {
|
|
key = document.getElementById("key").value;
|
|
string = document.getElementById("plaintext").value;
|
|
if (typeof key !== "number") {
|
|
throw new Error("Key must be a number");
|
|
}
|
|
let resultArray = []
|
|
for (let i = 0; i < string.length; i++) {
|
|
if (string.charCodeAt(i) < 97 || string.charCodeAt(i) > 122) {
|
|
resultArray.push(string[i]);
|
|
continue;
|
|
}
|
|
let code = string.charCodeAt(i) + key;
|
|
while (code > 122) {
|
|
code = (code - 122) + 96;
|
|
}
|
|
resultArray.push(String.fromCharCode(code))
|
|
}
|
|
|
|
document.getElementById("encoded").value = resultArray.join(" ");
|
|
}
|
|
|
|
let bruteforce = function() {
|
|
let know_element = document.getElementById("know_element").value;
|
|
let string = document.getElementById("encoded").value;
|
|
if (typeof key !== "number") {
|
|
throw new Error("Key must be a number");
|
|
}
|
|
|
|
let resultArray = []
|
|
let workingKeys = []
|
|
|
|
for (key = 0; key < 26; key++) {
|
|
resultArray = decrypt(key, string);
|
|
if (resultArray.includes(know_element)) {
|
|
workingKeys.push(key);
|
|
}
|
|
}
|
|
document.getElementById("key").value = workingKeys.join(",");
|
|
}
|
|
|
|
let decrypt = function(key, string) {
|
|
resultArray = []
|
|
for (let i = 0; i < string.length; i++) {
|
|
if (string.charCodeAt(i) < 97 || string.charCodeAt(i) > 122) {
|
|
resultArray.push(string[i]);
|
|
continue;
|
|
}
|
|
let code = string.charCodeAt(i) - key;
|
|
while (code - 96) {
|
|
code = (code + 122) - 96;
|
|
}
|
|
resultArray.push(String.fromCharCode(code))
|
|
}
|
|
return resultArray.join(" ");
|
|
}
|
|
|
|
let manual_decrypt = function() {
|
|
let key = document.getElementById("key").value;
|
|
let string = document.getElementById("encoded").value;
|
|
document.getElementById("plaintext").value = decrypt(key, string);
|
|
}
|