Fully working encryption via command line.

pull/99/head
AaronCoplan 2017-12-14 17:38:34 -05:00
rodzic 10b0cebe31
commit 94a84c5837
Nie znaleziono w bazie danych klucza dla tego podpisu
ID klucza GPG: 2D904C521DB00D86
2 zmienionych plików z 250 dodań i 2 usunięć

Wyświetl plik

@ -1,21 +1,102 @@
'use strict';
var CryptoJS = require("crypto-js");
var FileSystem = require("fs");
var https = require("https");
const SCRIPT_URL = 'https://cdnjs.cloudflare.com/ajax/libs/crypto-js/3.1.9-1/crypto-js.min.js';
const SCRIPT_TAG = '<script src="' + SCRIPT_URL + '" integrity="sha384-lp4k1VRKPU9eBnPePjnJ9M2RF3i7PC30gXs70+elCVfgwLwx1tv5+ctxdtwxqZa7" crossorigin="anonymous"></script>';
const argv = process.argv;
const argc = process.argv.length;
if(argc != 4){
if(argc < 4 || argc > 7){
console.log("Failure: invalid argument length!");
process.exit(1);
}
const htmlFilepath = argv[2];
const outputFilePath = htmlFilepath + ".encrypted";
const password = argv[3];
var pageTitle = "Protected Page";
if(argc >= 5){
pageTitle = argv[4];
}
var instructions = "";
if(argc >= 6){
instructions = argv[5];
}
var embed = true;
if(argc >= 7){
embed = (argv[6] == 'true');
}
try{
var contents = FileSystem.readFileSync(htmlFilepath, 'utf8');
}catch(e){
console.log("Failure: file does not exist!");
process.exit(1);
}
console.log(contents);
var encrypted = CryptoJS.AES.encrypt(contents, password);
var hmac = CryptoJS.HmacSHA256(encrypted.toString(), CryptoJS.SHA256(password)).toString();
var encryptedMessage = hmac + encrypted;
var data = {
title: pageTitle,
instructions: instructions,
encrypted: encryptedMessage,
crypto_tag: SCRIPT_TAG,
embed: embed,
outputFilePath: outputFilePath
};
if(data.embed){
https.get(SCRIPT_URL, (resp) => {
let txt = '';
resp.on('data', (chunk) => {
txt += chunk;
});
resp.on('end', () => {
data["crypto_tag"] = '<script>' + txt + '</script>';
console.log(data);
genFile(data);
});
}).on('error', (err) => {
console.log("Failure: could not fetch embedded script");
process.exit(1);
});
}else{
genFile(data);
}
function genFile(data){
try{
var templateContents = FileSystem.readFileSync('./password_template.html', 'utf8');
}catch(e){
console.log("Failure: could not read template!");
process.exit(1);
}
var renderedTemplate = render(templateContents, data);
try{
FileSystem.writeFileSync(data.outputFilePath, renderedTemplate);
}catch(e){
console.log("Failure: could not generate output file!");
process.exit(1);
}
}
function render(tpl, data){
return tpl.replace(/{(.*?)}/g, function (_, key) {
return data && data[key] || '';
});
}

Wyświetl plik

@ -0,0 +1,167 @@
<!doctype html>
<html class="staticrypt-html">
<head>
<meta charset="utf-8">
<title>{title}</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<!-- do not cache this page -->
<meta http-equiv="cache-control" content="max-age=0"/>
<meta http-equiv="cache-control" content="no-cache"/>
<meta http-equiv="expires" content="0"/>
<meta http-equiv="expires" content="Tue, 01 Jan 1980 1:00:00 GMT"/>
<meta http-equiv="pragma" content="no-cache"/>
<style>
.staticrypt-hr {
margin-top: 20px;
margin-bottom: 20px;
border: 0;
border-top: 1px solid #eee;
}
.staticrypt-page {
width: 360px;
padding: 8% 0 0;
margin: auto;
box-sizing: border-box;
}
.staticrypt-form {
position: relative;
z-index: 1;
background: #FFFFFF;
max-width: 360px;
margin: 0 auto 100px;
padding: 45px;
text-align: center;
box-shadow: 0 0 20px 0 rgba(0, 0, 0, 0.2), 0 5px 5px 0 rgba(0, 0, 0, 0.24);
}
.staticrypt-form input {
outline: 0;
background: #f2f2f2;
width: 100%;
border: 0;
margin: 0 0 15px;
padding: 15px;
box-sizing: border-box;
font-size: 14px;
}
.staticrypt-form .staticrypt-decrypt-button {
text-transform: uppercase;
outline: 0;
background: #4CAF50;
width: 100%;
border: 0;
padding: 15px;
color: #FFFFFF;
font-size: 14px;
cursor: pointer;
}
.staticrypt-form .staticrypt-decrypt-button:hover, .staticrypt-form .staticrypt-decrypt-button:active, .staticrypt-form .staticrypt-decrypt-button:focus {
background: #43A047;
}
.staticrypt-html {
height: 100%;
}
.staticrypt-body {
margin-bottom: 1em;
background: #76b852; /* fallback for old browsers */
background: -webkit-linear-gradient(right, #76b852, #8DC26F);
background: -moz-linear-gradient(right, #76b852, #8DC26F);
background: -o-linear-gradient(right, #76b852, #8DC26F);
background: linear-gradient(to left, #76b852, #8DC26F);
font-family: "Arial", sans-serif;
-webkit-font-smoothing: antialiased;
-moz-osx-font-smoothing: grayscale;
}
.staticrypt-instructions {
margin-top: -1em;
margin-bottom: 1em;
}
.staticrypt-title {
font-size: 1.5em;
}
.staticrypt-footer {
position: fixed;
height: 20px;
font-size: 16px;
padding: 2px;
bottom: 0;
left: 0;
right: 0;
margin-bottom: 0;
}
.staticrypt-footer p {
margin: 2px;
text-align: center;
float: right;
}
.staticrypt-footer a {
text-decoration: none;
}
</style>
</head>
<body class="staticrypt-body">
<div class="staticrypt-page">
<div class="staticrypt-form">
<div class="staticrypt-instructions">
<p class="staticrypt-title">{title}</p>
<p>{instructions}</p>
</div>
<hr class="staticrypt-hr">
<form id="staticrypt-form" action="#" method="post">
<input id="staticrypt-password"
type="password"
name="password"
placeholder="passphrase"
autofocus/>
<input type="submit" class="staticrypt-decrypt-button" value="DECRYPT"/>
</form>
</div>
</div>
<footer class="staticrypt-footer">
<p class="pull-right">Created with <a href="https://robinmoisson.github.io/staticrypt">StatiCrypt</a></p>
</footer>
{crypto_tag}
<script>
document.getElementById('staticrypt-form').addEventListener('submit', function(e) {
e.preventDefault();
var passphrase = document.getElementById('staticrypt-password').value,
encryptedMsg = '{encrypted}',
encryptedHMAC = encryptedMsg.substring(0, 64),
encryptedHTML = encryptedMsg.substring(64),
decryptedHMAC = CryptoJS.HmacSHA256(encryptedHTML, CryptoJS.SHA256(passphrase)).toString();
if (decryptedHMAC !== encryptedHMAC) {
alert('Bad passphrase !');
return;
}
var plainHTML = CryptoJS.AES.decrypt(encryptedHTML, passphrase).toString(CryptoJS.enc.Utf8);
document.write(plainHTML);
document.close();
});
</script>
</body>
</html>