Wednesday, March 4, 2015

Simple Encryption Alogarithm Multiple Language: JavaScript to C#

Some time need encrypt some data on client-side (Javascript) and decrypt it on server-side here is small solution.


function encrypt_str(a, b)
{

var the_res="";
for(i=0;i<b.length;++i)
{
the_res+=String.fromCharCode(a^b.charCodeAt(i));
}

return the_res;
}

function decrypt_str(c, a)
{

var the_res="";//the result will be here

for(i=0;i<c.length;i++)
{
the_res+=String.fromCharCode(a^c.charCodeAt(i));
}

return the_res;
}


  public static string encrypt_str(int a, string b)
  {
       string c ="";

       for(int i=0;i<b.Length;++i)
       {
                var xor = (char)(a ^ ((int)b[i]));
           c = c + xor;
       }

            return c;
}



           
public static string decrypt_str(string c, int a)
{

       string b ="";

       for(int i=0;i<c.Length;i++)
       {

           var xor = (char) (a ^ ((int) c[i]));

           b = b + xor;


       }

             return b;
}


Source: http://th.atguy.com/mycode/xor_js_encryption/

No comments:

Post a Comment