Skip to content

Commit

Permalink
Added navigation bar; added copy to clipboard button for the key in t…
Browse files Browse the repository at this point in the history
…he 'encrypt' page
  • Loading branch information
alessandro-amadelli committed Feb 27, 2021
1 parent 352102b commit 4480542
Show file tree
Hide file tree
Showing 5 changed files with 192 additions and 3 deletions.
9 changes: 9 additions & 0 deletions static/scriptEncrypt.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@ document.addEventListener('DOMContentLoaded', () => {
encryptText();
}

document.querySelector("#btnCopyKey").onclick = () => {
copyKey();
}

});

function showPageMsg(msgClass, msgText){
Expand Down Expand Up @@ -101,3 +105,8 @@ function writeCypherText(cText) {
textDiv.innerText = cText;
textDiv.style.display = "block";
}

function copyKey(){
document.querySelector("#OTPKey").select()
document.execCommand("copy");
}
22 changes: 22 additions & 0 deletions templates/decrypt.html
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,27 @@
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.0.0-beta2/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-BmbxuPwQa2lc/FVzBcNJ7UAyJxM6wuqIj61tLrc4wSX0szH/Ev+nYRRuWlolflfl" crossorigin="anonymous">
<link rel="stylesheet" href="../static/style.css">
<script src="../static/scriptDecrypt.js"></script>

<div class="container">
<nav class="navbar navbar-expand-lg navbar-dark bg-primary">
<a class="navbar-brand" href="./index.html">JS_OTP</a>
<button class="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-target="#navbarSupportedContent" aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation">
<span class="navbar-toggler-icon"></span>
</button>

<div class="collapse navbar-collapse" id="navbarSupportedContent">
<ul class="navbar-nav mr-auto">
<li class="nav-item active">
<a class="nav-link" href="./encrypt.html">Encrypt</span></a>
</li>
<li class="nav-item">
<a class="nav-link" href="./decrypt.html">Decrypt</a>
</li>
</ul>
</div>
</nav>
</div>

</head>

<body>
Expand Down Expand Up @@ -34,6 +55,7 @@ <h2>Here you can decrypt a ciphertext encrypted with ONE TIME PAD</h2>
</div>

</div>
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.0.0-beta2/dist/js/bootstrap.bundle.min.js" integrity="sha384-b5kHyXgcpbZJO/tY9Ul7kGkf1S0CWuKcCD38l8YkeH8z8QjE0GmW1gYU5S9FOnJ0" crossorigin="anonymous"></script>
</body>

</html>
30 changes: 30 additions & 0 deletions templates/encrypt.html
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,30 @@
<head>
<title>Encrypt text</title>
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.0.0-beta2/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-BmbxuPwQa2lc/FVzBcNJ7UAyJxM6wuqIj61tLrc4wSX0szH/Ev+nYRRuWlolflfl" crossorigin="anonymous">

<link rel="stylesheet" href="../static/style.css">
<script src="../static/scriptEncrypt.js"></script>

<div class="container">
<nav class="navbar navbar-expand-lg navbar-dark bg-primary">
<a class="navbar-brand" href="./index.html">JS_OTP</a>
<button class="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-target="#navbarSupportedContent" aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation">
<span class="navbar-toggler-icon"></span>
</button>

<div class="collapse navbar-collapse" id="navbarSupportedContent">
<ul class="navbar-nav mr-auto">
<li class="nav-item active">
<a class="nav-link" href="./encrypt.html">Encrypt</span></a>
</li>
<li class="nav-item">
<a class="nav-link" href="./decrypt.html">Decrypt</a>
</li>
</ul>
</div>
</nav>
</div>

</head>

<body>
Expand All @@ -20,11 +42,17 @@ <h2>Here you can encrypt a text with ONE TIME PAD</h2>
<label for="plainText" class="sr-only">Insert your plain text message</label>
<textarea class="form-control" id="plainText" rows=5></textarea>
</div>

<br>

<div class="form-group">
<label for="OTPKey" class="sr-only">Key (you can change it but it's better to use the random-generated one)</label>
<textarea class="form-control" id="OTPKey" rows=5></textarea>
<button type="button" class="btn btn-secondary btn-sm" id="btnCopyKey">Copy to clipboard</button>
</div>

<br>

<div class="form-group">
<button type="submit" class="btn btn-primary mb-2" id="btnNewKey">Generate new key</button>
</div>
Expand All @@ -38,6 +66,8 @@ <h2>Here you can encrypt a text with ONE TIME PAD</h2>
</div>

</div>

<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.0.0-beta2/dist/js/bootstrap.bundle.min.js" integrity="sha384-b5kHyXgcpbZJO/tY9Ul7kGkf1S0CWuKcCD38l8YkeH8z8QjE0GmW1gYU5S9FOnJ0" crossorigin="anonymous"></script>
</body>

</html>
31 changes: 28 additions & 3 deletions templates/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,40 @@
<title>OTP Encryption</title>
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.0.0-beta2/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-BmbxuPwQa2lc/FVzBcNJ7UAyJxM6wuqIj61tLrc4wSX0szH/Ev+nYRRuWlolflfl" crossorigin="anonymous">
<link rel="stylesheet" href="../static/style.css">

<div class="container">
<nav class="navbar navbar-expand-lg navbar-dark bg-primary">
<a class="navbar-brand" href="./index.html">JS_OTP</a>
<button class="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-target="#navbarSupportedContent" aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation">
<span class="navbar-toggler-icon"></span>
</button>

<div class="collapse navbar-collapse" id="navbarSupportedContent">
<ul class="navbar-nav mr-auto">
<li class="nav-item active">
<a class="nav-link" href="./encrypt.html">Encrypt</span></a>
</li>
<li class="nav-item">
<a class="nav-link" href="./decrypt.html">Decrypt</a>
</li>
</ul>
</div>
</nav>
</div>


</head>

<body>
<h1>Welcome!</h1>
<h2>This site lets you encrypt and decrypt text using One Time Pad cipher</h2>
<div class="container">
<h1>Welcome!</h1>
<h2>This site lets you encrypt and decrypt text using One Time Pad cipher</h2>


<a href="https://en.wikipedia.org/wiki/One-time_pad" target="_blank">About One Time Pad</a>
<a href="https://en.wikipedia.org/wiki/One-time_pad" target="_blank">About One Time Pad</a>
</div>

<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.0.0-beta2/dist/js/bootstrap.bundle.min.js" integrity="sha384-b5kHyXgcpbZJO/tY9Ul7kGkf1S0CWuKcCD38l8YkeH8z8QjE0GmW1gYU5S9FOnJ0" crossorigin="anonymous"></script>
</body>

</html>
103 changes: 103 additions & 0 deletions templates/scriptEncrypt.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@

document.addEventListener('DOMContentLoaded', () => {

document.querySelector("#plainText").addEventListener('keyup', event => {
updateKey();
});

document.querySelector("#btnNewKey").onclick = () => {
updateKey();
}

document.querySelector("#btnEncrypt").onclick = () => {
encryptText();
}

});

function showPageMsg(msgClass, msgText){
let msg = document.querySelector("#pageMsg");

msg.setAttribute("class",msgClass);
msg.innerText = msgText;
}

function generateKey(keyLength) {
/**
This function randomly generates a key of the same length of the plaintext.
*/

//Possible characters in key
const characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
const charLength = characters.length;

let keyArray = [];
for (var i=0; i < keyLength; i++) {
keyArray.push(characters.charAt(Math.floor(Math.random() * charLength)) );
}

let keyString = keyArray.join("");

return keyString;
}

function updateKey() {
/**
This function let user update the key by generating a new random one.
*/
let newKey = "";
let plaintext = document.querySelector("#plainText");
let len = plaintext.value.length;
let otpKey = document.querySelector("#OTPKey");
if (len > 0) {
newKey = generateKey(len);
} else {
let cipherDiv = document.querySelector("#cipherText");
cipherDiv.innerText = "";
cipherDiv.style.display = "none";

let ciphLabel = document.querySelector("#labelCiphertext");
ciphLabel.innerText = "";
}
otpKey.value = newKey;
}

function encryptText() {
/**
The function encrypts text executing the bitwise XOR operation between the
plain text and the key.
*/

let plaintext = document.querySelector("#plainText");
let len = plaintext.value.length;
let otpKey = document.querySelector("#OTPKey");
let keyLen = otpKey.value.length;

//Check if the key has the correct length
if (keyLen < len) {
showPageMsg("alert alert-danger", "Incorrect key length...the key has to be at least as long as the plain text.");
return false;
}

cipherArray = [];
for (var i=0; i < len; i++) {
let bitVal = plaintext.value[i].charCodeAt(0) ^ otpKey.value[i].charCodeAt(0);
cipherArray.push(bitVal);
}

let ciphertext = cipherArray.join(" ");

writeCypherText(ciphertext);
}

function writeCypherText(cText) {
/**
This function write the ciphertext rendering the div visible
*/
let label = document.querySelector("#labelCiphertext");
let textDiv = document.querySelector("#cipherText");

label.innerText = "Ciphertext";
textDiv.innerText = cText;
textDiv.style.display = "block";
}

0 comments on commit 4480542

Please sign in to comment.