From d552be851f8b7f28648e95b435bd4640be9e6df9 Mon Sep 17 00:00:00 2001 From: Kevin Froman Date: Fri, 5 Aug 2016 06:08:47 -0500 Subject: [PATCH] Complete rework to not use server side code, added no encrypt option --- aes.min.js | 0 index.html | 20 +++--- main.js | 164 ++++++++++++++++++++++++++++++-------------------- main.min.js | 1 - snow2.py | 40 ------------ theme.css | 0 theme.min.css | 0 7 files changed, 112 insertions(+), 113 deletions(-) mode change 100644 => 100755 aes.min.js mode change 100644 => 100755 index.html mode change 100644 => 100755 main.js delete mode 100644 main.min.js delete mode 100644 snow2.py mode change 100644 => 100755 theme.css mode change 100644 => 100755 theme.min.css diff --git a/aes.min.js b/aes.min.js old mode 100644 new mode 100755 diff --git a/index.html b/index.html old mode 100644 new mode 100755 index 2460a23..97e80be --- a/index.html +++ b/index.html @@ -10,7 +10,7 @@ - + @@ -45,15 +45,19 @@

-
- -
- -
+ +

+
+ + +
+ +
+
- + - + \ No newline at end of file diff --git a/main.js b/main.js old mode 100644 new mode 100755 index 9e243cc..e6629a5 --- a/main.js +++ b/main.js @@ -10,103 +10,139 @@ clipboard.on('success', function(e) { clipboard.on('error', function(e) { $('#copyFeedback').css('display', 'inherit'); $('#copyFeedback').css('color', 'red'); - $('#copyFeedback').html('Failed to copy.'); + $('#copyFeedback').html('Your browser doesn\'t seem to support automatic copying. Get a better one.'); e.clearSelection(); }); +if ($('#useEncrypt').is(':checked') == false) +{ + $('#encryptArea').css('display', 'none'); +} + $('#modalClose').click(function(){ $('#copyFeedback').css('display', 'none'); }); -window.snowMode = 'encrypt'; +window.snowMode = 'encode'; + +$("#output").on("click", function () { + $(this).select(); +}); $('#toggle').click(function(){ - if (window.snowMode == 'encrypt') + if (window.snowMode == 'encode') { - window.snowMode = 'decrypt'; - $('#toggle').html("Decrypt Mode"); + window.snowMode = 'decode'; + $('#toggle').html("Decode "); $('#confirmPass').css('display', 'none'); } else { - window.snowMode = 'encrypt'; - $('#toggle').html("Encrypt Mode"); + window.snowMode = 'encode'; + $('#toggle').html("Encode "); $('#confirmPass').css('display', 'inline'); } }); -$('#go').click(function(){ +/* based on stackoverflow.com/questions/14430633/how-to-convert-text-to-binary-code-in-javascript */ +function binToText(str) { + var str = str.replace(/ /g, "1"); + var str = str.replace(/\t/g, "0"); + if(str.match(/[10]{8}/g)){ + var wordFromBinary = str.match(/([10]{8}|\s+)/g).map(function(fromBinary){ + return String.fromCharCode(parseInt(fromBinary, 2) ); + }).join(''); + return wordFromBinary; + } +} - document.getElementById("go").disabled = true; +/* based on stackoverflow.com/questions/21354235/converting-binary-to-text-using-javascript */ +function textToBin(text) { + var output = ''; + var length = text.length, + output = []; + for (var i = 0;i < length; i++) { + var bin = text[i].charCodeAt().toString(2); + output.push(Array(8-bin.length+1).join("0") + bin); + } + return output.join(''); +} - var password; - var confirmPass; - var encrypted; - var decrypted; - var encodeChoice; - - var text = $('#text').val(); - - if (text == '') +$('#useEncrypt').click(function(){ + if (! this.checked) { - document.getElementById("go").disabled = false; - return; - } - - if (window.snowMode == 'encrypt') - { - encodeChoice = '1'; - - password = $('#password').val(); - confirmPass = $('#confirmPass').val(); - - if (password != confirmPass) - { - alert("Passwords must match"); - document.getElementById("go").disabled = false; - return; - } - else - { - encrypted = CryptoJS.AES.encrypt(text, password); - - $.post( "./snow2.py", { choice: encodeChoice, text: encrypted.toString()} ) - .done(function( data ) { - document.getElementById("go").disabled = false; - - $('#output').val(data); - - $('#outputModal').modal(); - }); - } + $('#encryptArea').css('display', 'none'); } else { - encodeChoice = '2'; + $('#encryptArea').css('display', 'inherit'); + } +}) - $.post( "./snow2.py", { choice: encodeChoice, text: text} ) - .done(function( data ) { +function verifyPass(mode) +{ + if ($('#password').val() == '') + { + alert('You must provide a password.'); + return false; + } - text = data; - password = $('#password').val(); + if (mode == 'encrypt') + { + if ($('#password').val() != $('#confirmPass').val()) + { + alert('Passwords must match.'); + return false; + } + } + return true; +} - decrypted = CryptoJS.AES.decrypt(text, password); - decrypted = decrypted.toString(CryptoJS.enc.Utf8); - if (decrypted == '') +$('#go').click(function(){ + var output = ''; + + var input = $('#text').val(); + + if (input == '') { return false; } + + + // If we're encoding: + if (window.snowMode == 'encode') + { + // If we should use encryption, encrypt first: + if ($('#useEncrypt').is(':checked')) + { + // verify password first + if (verifyPass('encrypt')) { - alert('invalid password'); - document.getElementById("go").disabled = false; + input = CryptoJS.AES.encrypt(input, $('#password').val()).toString(); } else { - document.getElementById("go").disabled = false; - $('#output').val(decrypted); - $('#outputModal').modal(); + return false; } - - }); + } + // convert result to binary + output = textToBin(input); + $('#output').val(output.toString().replace(/1/g, " ").replace(/0/g, "\t")); } - + else + { + var output = binToText(input); + if ($('#useEncrypt').is(':checked')) + { + if (verifyPass('decrypt')) + { + output = CryptoJS.AES.decrypt(output, $('#password').val()).toString(CryptoJS.enc.Utf8); + } + else + { + return false; + } + } + $('#output').val(output.toString()); + } + $('#outputModal').modal(); }); \ No newline at end of file diff --git a/main.min.js b/main.min.js deleted file mode 100644 index c273f2e..0000000 --- a/main.min.js +++ /dev/null @@ -1 +0,0 @@ -var clipboard=new Clipboard(".btn");clipboard.on("success",function(e){$("#copyFeedback").css("display","inherit");$("#copyFeedback").css("color","green");$("#copyFeedback").html("Copied!");e.clearSelection()});clipboard.on("error",function(e){$("#copyFeedback").css("display","inherit");$("#copyFeedback").css("color","red");$("#copyFeedback").html("Failed to copy.");e.clearSelection()});$("#modalClose").click(function(){$("#copyFeedback").css("display","none")});window.snowMode="encrypt";$("#toggle").click(function(){if(window.snowMode=="encrypt"){window.snowMode="decrypt";$("#toggle").html("Decrypt Mode");$("#confirmPass").css("display","none")}else{window.snowMode="encrypt";$("#toggle").html("Encrypt Mode");$("#confirmPass").css("display","inline")}});$("#go").click(function(){document.getElementById("go").disabled=true;var password;var confirmPass;var encrypted;var decrypted;var encodeChoice;var text=$("#text").val();if(text==""){document.getElementById("go").disabled=false;return}if(window.snowMode=="encrypt"){encodeChoice="1";password=$("#password").val();confirmPass=$("#confirmPass").val();if(password!=confirmPass){alert("Passwords must match");document.getElementById("go").disabled=false;return}else{encrypted=CryptoJS.AES.encrypt(text,password);$.post("./snow2.py",{choice:encodeChoice,text:encrypted.toString()}).done(function(data){document.getElementById("go").disabled=false;$("#output").val(data);$("#outputModal").modal()})}}else{encodeChoice="2";$.post("./snow2.py",{choice:encodeChoice,text:text}).done(function(data){text=data;password=$("#password").val();decrypted=CryptoJS.AES.decrypt(text,password);decrypted=decrypted.toString(CryptoJS.enc.Utf8);if(decrypted==""){alert("invalid password");document.getElementById("go").disabled=false}else{document.getElementById("go").disabled=false;$("#output").val(decrypted);$("#outputModal").modal()}})}}); \ No newline at end of file diff --git a/snow2.py b/snow2.py deleted file mode 100644 index 6d574ab..0000000 --- a/snow2.py +++ /dev/null @@ -1,40 +0,0 @@ -#!/usr/bin/python3 - -import binascii - -import cgi -import cgitb; cgitb.enable() # Optional; for debugging only -import sys -arguments = cgi.FieldStorage() - -# echo headers, additional line-break nessasary. -print("content-type: text/plain") -print("") - -# get if request is to encode or decode -choice = arguments['choice'].value - -if choice == "1": - - # Encode to whitespace - - orig = arguments['text'].value - - binary = bin(int.from_bytes(orig.encode(), 'big')) - - binary = binary.replace("0", " ").replace("1", "\t") - - print(binary[2:]) - -elif choice == "2": - - # Decode to the aes text - - binary = " b" + arguments['text'].value - - binary = binary.replace("\t", "1").replace(" ", "0") - - - n = int(binary, 2) - - print(n.to_bytes((n.bit_length() + 7) // 8, 'big').decode()) diff --git a/theme.css b/theme.css old mode 100644 new mode 100755 diff --git a/theme.min.css b/theme.min.css old mode 100644 new mode 100755