Our Single Key Encryption code in Javascript.
Single Key Encryption JavaScript File:
"use strict";
function encodeWord(msg, key) {
// encoding eqn { Tn = 3^n-1(2t1 + 1) - 1 } - please use your own eqn
// 2
var encryption = [];
var n;
var t1;
var Tn;
for (var i = 0; i < msg.length; i++) {
// get unicode of this character as t1
t1 = msg.charCodeAt(i);
// get next key digit as n
n = Number('0x' + key[i % (key.length - 1)]);
// use recurrence series equation to encrypt & save in base 16
Tn = bigInt(3).pow(n - 1).multiply(bigInt(2 * t1 + 1)).subtract(bigInt.one).divide(bigInt(2));
encryption[i] = Tn.toString(16);
}
return encryption;
}
function decodeWord(code, key) {
// decoding eqn { t1 = 3^1-n(2Tn + 1) - 1 }
// 2
var decryption = [];
var n;
var t1;
var Tn;
for (var i = 0; i < code.length; i++) {
Tn = bigInt(code[i], 16);
// get next key digit as n
n = Number('0x' + key[i % (key.length - 1)]);
// use recurrence series equation to decrypt
t1 = bigInt(2 * Tn + 1).divide(bigInt(3).pow(n - 1)).subtract(bigInt.one).divide(bigInt(2));
decryption[i] = String.fromCharCode(t1);
}
return decryption;
}
HTML File:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Single Key Encryption</title>
<script src="BigInteger.min.js"></script>
<script src="SoleKeyEncryption.js"></script>
</head>
<body>
<h3>Single Secret Key Encryption</h3>
<!-- This is where the result will be displayed when it is ready.-->
<div id="word_encrypt"></div>
<script>
var message = "merry xmas";
var key = "A5FB17C4D8".split(""); // you might want to avoid zeroes
var result = encodeWord(message, key);
document.getElementById("word_encrypt").innerHTML +=
"Message is '" + message + "';<br/>Encrypted version is '" + result.join(", ") + "'.";
result = decodeWord(result, key);
document.getElementById("word_encrypt").innerHTML +=
"<br/><br/>Decrypted message is '" + result.join("") + "'.";
</script>
</body>
</html>
Try it out!
Elegance (0.0)