Skip to content

Instantly share code, notes, and snippets.

@19h
Last active July 19, 2023 13:34
Show Gist options
  • Save 19h/5877106 to your computer and use it in GitHub Desktop.
Save 19h/5877106 to your computer and use it in GitHub Desktop.
A Probabilistic Encryption Algorithm (V8/ECMA)
var encrypt = function (i, d) {
if (null == d || 0 >= d.length) return null;
for (var a = "", b = 0; b < d.length; b++) a += d.charCodeAt(b).toString();
var b = Math.floor(a.length / 5),
g = parseInt(a.charAt(b) + a.charAt(2 * b) + a.charAt(3 * b) + a.charAt(4 * b) + a.charAt(5 * b), 10),
j = Math.ceil(d.length / 2),
h = Math.pow(2, 31) - 1;
if (2 > g) return null;
for (var c = Math.round(1E9 * Math.random()) % 1E8, a = a + c; 10 < a.length;) a = (parseInt(a.substring(0, 10), 10) + parseInt(a.substring(10, a.length), 10)).toString();
for (a = (g * a + j) % h, e = "", f = "", b = 0; b < i.length; b++) e = parseInt(i.charCodeAt(b) ^ Math.floor(255 * (a / h)), 10), f = 16 > e ? f + ("0" + e.toString(16)) : f + e.toString(16), a = (g * a + j) % h;
for (c = c.toString(16); 8 > c.length;) c = "0" + c;
return f + c
};
var decrypt = function (c, e) {
if (null == c || 8 > c.length) return null;
if (null == e || 0 >= e.length) return null;
for (var a = "", b = 0; b < e.length; b++) a += e.charCodeAt(b).toString();
for (d = Math.floor(a.length / 5), d = parseInt(a.charAt(d) + a.charAt(2 * d) + a.charAt(3 * d) + a.charAt(4 * d) + a.charAt(5 * d), 10), h = Math.round(e.length / 2), f = Math.pow(2, 31) - 1, b = parseInt(c.substring(c.length - 8, c.length), 16), c = c.substring(0, c.length - 8), a += b; 10 < a.length;) a = (parseInt(a.substring(0, 10), 10) + parseInt(a.substring(10, a.length), 10)).toString();
for (var a = (d * a + h) % f, i = "", g = "", b = 0; b < c.length; b += 2) i = parseInt(parseInt(c.substring(b, b + 2), 16) ^ Math.floor(255 * (a / f)), 10), g += String.fromCharCode(i), a = (d * a + h) % f;
return g
};
key = "secret"
v1 = encrypt("This is an arbitrary cipher.", key);
v2 = encrypt("This is an arbitrary cipher.", key);
v3 = encrypt("This is an arbitrary cipher.", key);
console.log("Is v1 different from v2? ", v1 !== v2);
console.log("Is v1 different from v3? ", v1 !== v3);
console.log("Is v2 different from v3? ", v2 !== v3);
console.log("\tv1:", v1, "\n\tv2:", v2, "\n\tv3:", v3)
console.log("Decrypting v1:", decrypt(v1, key))
console.log("Decrypting v2:", decrypt(v2, key))
console.log("Decrypting v3:", decrypt(v3, key))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment