|
|
|
<!DOCTYPE html>
|
|
|
|
<html>
|
|
|
|
<head>
|
|
|
|
<meta charset="utf8" />
|
|
|
|
</head>
|
|
|
|
|
|
|
|
<body>
|
|
|
|
<script src="js/crypto-js.js"></script>
|
|
|
|
<script src="js/jsencrypt.min.js"></script>
|
|
|
|
<script src="js/NodeRSA.js"></script>
|
|
|
|
<script type="text/javascript">
|
|
|
|
function test(priv, pub) {
|
|
|
|
//const priv = priv || document.getElementById("privkey").textContent;
|
|
|
|
//const pub = pub || document.getElementById("pubkey").textContent;
|
|
|
|
console.log(`Priv: ${priv}`);
|
|
|
|
console.log(`Pub: ${pub}`);
|
|
|
|
|
|
|
|
const encrypt = new JSEncrypt();
|
|
|
|
encrypt.setPublicKey(pub);
|
|
|
|
const ciphertext = encrypt.encrypt("test input");
|
|
|
|
|
|
|
|
console.log(`Ciphertext: ${ciphertext}`);
|
|
|
|
|
|
|
|
const decrypt = new JSEncrypt();
|
|
|
|
decrypt.setPrivateKey(priv);
|
|
|
|
const plaintext = decrypt.decrypt(ciphertext);
|
|
|
|
|
|
|
|
console.log(`Plaintext: ${plaintext}`);
|
|
|
|
|
|
|
|
const sign = new JSEncrypt();
|
|
|
|
sign.setPrivateKey(priv);
|
|
|
|
const signature = sign.sign("test input", CryptoJS.SHA256, "sha256");
|
|
|
|
|
|
|
|
console.log(`Signature: ${signature}`);
|
|
|
|
|
|
|
|
const verify = new JSEncrypt();
|
|
|
|
verify.setPublicKey(pub);
|
|
|
|
const verified = verify.verify("test input", signature, CryptoJS.SHA256);
|
|
|
|
|
|
|
|
console.log(`Verified: ${verified}`);
|
|
|
|
|
|
|
|
|
|
|
|
const key = "test key 123";
|
|
|
|
const aes_ciphertext = CryptoJS.AES.encrypt("test input", key).toString();
|
|
|
|
|
|
|
|
console.log(`AES ciphertext: ${aes_ciphertext}`);
|
|
|
|
|
|
|
|
const bytes = CryptoJS.AES.decrypt(aes_ciphertext, key);
|
|
|
|
const aes_plaintext = bytes.toString(CryptoJS.enc.Utf8);
|
|
|
|
|
|
|
|
console.log(`AES plaintext: ${aes_plaintext}`);
|
|
|
|
}
|
|
|
|
window.onload = (async() => {
|
|
|
|
const NodeRSA = require("node-rsa");
|
|
|
|
const key = new NodeRSA({b: 1024});
|
|
|
|
//key.generateKeyPair(); //unneeded I think
|
|
|
|
const pub = key.exportKey("public");
|
|
|
|
const priv = key.exportKey("private");
|
|
|
|
//console.log(`Pub: ${pub}, priv: ${priv}`);
|
|
|
|
document.getElementById("privkey").textContent = priv;
|
|
|
|
document.getElementById("pubkey").textContent = pub;
|
|
|
|
|
|
|
|
test(priv, pub);
|
|
|
|
});
|
|
|
|
</script>
|
|
|
|
<textarea id="privkey" rows="15" cols="65">(unbound)</textarea>
|
|
|
|
<textarea id="pubkey" rows="15" cols="65">(unbound)</textarea>
|
|
|
|
</body>
|
|
|
|
</html>
|