XORCipher
https://gist.github.com/sukima/5613286
by vanowm
HTML
Text:
<br>
<textarea id="text" oninput="convert()">abcde abcde abcde абвгд абвгд абвгд edcba edcba edcba</textarea>
<br>
Key:
<textarea id="pass" oninput="convert()">edcba абвгд abcde</textarea>
<br><br>
Encrypted:
<br>
<textarea id="encode" readonly="true"></textarea>
Decrypted:
<br>
<textarea id="decode" readonly="true"></textarea>
CSS
textarea
{
width: 100%;
height: 2em;
}
div > textarea
{
width: 49%;
}
JavaScript
var enc = document.getElementById("encode"),
dec = document.getElementById("decode"),
pass = document.getElementById("pass"),
text = document.getElementById("text");
function convert()
{
enc.value = XORCipher.encode(pass.value, text.value);
dec.value = XORCipher.decode(pass.value, enc.value);
}
// XORCipher - Super simple encryption using XOR and Base64
//
// As a warning, this is **not** a secure encryption algorithm. It uses a very
// simplistic keystore and will be easy to crack.
//
// The Base64 algorithm is a modification of the one used in phpjs.org
// * http://phpjs.org/functions/base64_encode/
// * http://phpjs.org/functions/base64_decode/
//
// stringToUtf8ByteArray() and utf8ByteArrayToString()
// https://github.com/google/closure-library/blob/466a34e7e2d4cb49ac1c731347e845235d8ce7cc/closure/goog/crypt/crypt.js#L117-L143
// https://github.com/google/closure-library/blob/466a34e7e2d4cb49ac1c731347e845235d8ce7cc/closure/goog/crypt/crypt.js#L151-L178
// Examples
// --------
//
// XORCipher.encode("test", "foobar"); // => "EgocFhUX"
// XORCipher.decode("test", "EgocFhUX"); // => "foobar"
//
// Copyright © 2013 Devin Weaver <suki@tritarget.org>
//
// This program is free software. It comes without any warranty, to
// the extent permitted by applicable law. You can redistribute it
// and/or modify it under the terms of the Do What The Fuck You Want
// To Public License, Version 2, as published by Sam Hocevar. See
// http://www.wtfpl.net/ for more details.
/* jshint forin:true, noarg:true, noempty:true, eqeqeq:true, strict:true,
undef:true, unused:true, curly:true, browser:true, indent:2, maxerr:50 */
/* global _ */
(function(exports) {
"use strict";
var XORCipher = {
encode: function(key, data) {
data = xor_encrypt(key, data);
return b64_encode(data);
},
decode: function(key, data) {
data = b64_decode(data);
return xor_decrypt(key, data);
}
};
function stringToUtf8ByteArray(str) {
var out = [], p =...