Another good method to encrypt ASCII based text. To view this example in work, http://editor.clizware.net/?id=7 (remember to press compile)
//Copyright (C) 2012 Artem Los, All rights reserved
function mod_c(text,key,enc_dec)
{
//only for 128 ascii values
if (key == "")
{
return "Error: no key given";
}
if (enc_dec === true)
{
var result = "";
for (var i=0; i < text.length; i++)
{
result += String.fromCharCode( (text.charCodeAt(i) + key.charCodeAt(i % key.length)).mod(128) );
}
return result;
}
else if(enc_dec === false)
{
var result = "";
for (var i=0; i < text.length; i++)
{
result += String.fromCharCode( (text.charCodeAt(i) - key.charCodeAt(i % key.length)).mod(128) );
}
return result;
}
else
{
return "Error: enc_dec is not given";
}
}
Number.prototype.mod = function(n) {
return ((this%n)+n)%n;
}
Leave a Reply