XORCipher-rand

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>
Seed:<label><input id="rand" type="checkbox" onclick="convert()" checked="checked">Random</label>
<br>
<textarea id="seed" oninput="convert()"></textarea>
<br>
<button id="submit" onclick="convert()">Submit</button>
<br><br>
Encrypted:
<br>
<textarea id="encode" readonly="true"></textarea>
<br>
Decrypted:
<br>
<textarea id="decode" readonly="true"></textarea>

CSS

textarea
{
  width: 100%;
  height: 2em;
	font-family: monospace;
}
div > textarea
{
  width: 49%;
}

JavaScript

var enc = document.getElementById("encode"),
		dec = document.getElementById("decode"),
    pass = document.getElementById("pass"),
    text = document.getElementById("text"),
    seed = document.getElementById("seed"),
    rand = document.getElementById("rand");

function convert()
{
	seed.disabled = rand.checked;
	enc.value = XORCipher.encode(pass.value, text.value, rand.checked ? undefined : seed.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" (random each time)
//     XORCipher.decode("test", "EgocFhUX"); // => "foobar"
//     XORCipher.encode("test", "foobar", "");   // => "dHdrcGZiYw==" (static each time)
//     XORCipher.decode("test", "dHdrcGZiYw=="); // => "foobar"
//     XORCipher.encode("test", "foobar", XORCipher.seed(10));   // => "XAIFMUMbeURsFRdyHXMTcQM=" (random each time, 10 char long seed)
//     XORCipher.decode("test", "XAIFMUMbeURsFRdyHXMTcQM="); // => "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...