-
-
Save shangab/b392726b80f0f0fa7eb3b0391e74ad89 to your computer and use it in GitHub Desktop.
A Super simple encryption cipher using XOR and Base64 in JavaScript - Just made the typescript version for angular 4, and I removed the underscore dependency
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// XORCipher - Super simple encryption using XOR and Base64 | |
// Usage | |
// -------- | |
// | |
// XORCipher.encode("test", "foobar"); // => "EgocFhUX" | |
// XORCipher.decode("test", "EgocFhUX"); // => "foobar" | |
// | |
// Copyright © 2013 Devin Weaver <[email protected]> | |
// | |
// 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. | |
export class XORCipher { | |
public encode(key:string, data:string):string { | |
data = this.xor_encrypt(key, data); | |
return this.b64_encode(data); | |
} | |
public decode(key:string, data:string):string { | |
data = this.b64_decode(data); | |
return this.xor_decrypt(key, data); | |
} | |
private b64_table = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/="; | |
private b64_encode(data) { | |
let o1, o2, o3, h1, h2, h3, h4, bits, r, i = 0, enc = ""; | |
if (!data) { | |
return data; | |
} | |
do { | |
o1 = data[i++]; | |
o2 = data[i++]; | |
o3 = data[i++]; | |
bits = o1 << 16 | o2 << 8 | o3; | |
h1 = bits >> 18 & 0x3f; | |
h2 = bits >> 12 & 0x3f; | |
h3 = bits >> 6 & 0x3f; | |
h4 = bits & 0x3f; | |
enc += this.b64_table.charAt(h1) + this.b64_table.charAt(h2) + this.b64_table.charAt(h3) + this.b64_table.charAt(h4); | |
} while (i < data.length); | |
r = data.length % 3; | |
return (r ? enc.slice(0, r - 3) : enc) + "===".slice(r || 3); | |
} | |
private b64_decode(data) { | |
let o1, o2, o3, h1, h2, h3, h4, bits, i = 0, result = []; | |
if (!data) { | |
return data; | |
} | |
data += ""; | |
do { | |
h1 = this.b64_table.indexOf(data.charAt(i++)); | |
h2 = this.b64_table.indexOf(data.charAt(i++)); | |
h3 = this.b64_table.indexOf(data.charAt(i++)); | |
h4 = this.b64_table.indexOf(data.charAt(i++)); | |
bits = h1 << 18 | h2 << 12 | h3 << 6 | h4; | |
o1 = bits >> 16 & 0xff; | |
o2 = bits >> 8 & 0xff; | |
o3 = bits & 0xff; | |
result.push(o1); | |
if (h3 !== 64) { | |
result.push(o2); | |
if (h4 !== 64) { | |
result.push(o3); | |
} | |
} | |
} while (i < data.length); | |
return result; | |
} | |
private keyCharAt(key, i) { | |
return key.charCodeAt(Math.floor(i % key.length)); | |
} | |
private xor_encrypt(key, data) { | |
return Array.prototype.map.call(data,(c, i)=>{ | |
return c.charCodeAt(0) ^ this.keyCharAt(key, i); | |
}) | |
} | |
private xor_decrypt(key, data) { | |
return Array.prototype.map.call(data,(c, i)=>{ | |
return String.fromCharCode(c ^ this.keyCharAt(key, i)); | |
}).join(""); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment