Code for encrypting and decrypting Input String

The sample C# code shown below demonstrates the functions to encrypt and decrypt the given data. If you want to encrypt Your data and futher want to decrypt it to use . You can use this Class...

The Class has two functions EnCrypt and DeCrypt...


public class EncryptionAndDecryption
{
const char CharacterToFill = '@';
static string Key = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";

static public string EnCrypt(string str)
{
int count;
int ctr;
int size = str.Length;
string R="";

for (count = 0; count < size; ++count)
{
ctr = (str[count] >> 2) & 0x3f;
R +=Key[ctr];
ctr = (str[count] << 4) & 0x3f;
if (++count < size)
ctr |= (str[count] >> 4) & 0x0f;

R += Key[ctr];
if (count < size)
{
ctr = (str[count] << 2) & 0x3f;
if (++count < size)
ctr |= (str[count] >> 6) & 0x03;

R += Key[ctr];
}
else
{
++count;
R += CharacterToFill;
}

if (count < size)
{
ctr = str[count] & 0x3f;
R += Key[ctr];
}
else
{
R += CharacterToFill;
}
}

return(R);
}

static public string DeCrypt(string str)
{
string R="";
int count;
char ctr;
char ctrNext;
int size = str.Length;

for (count = 0; count < size; ++count)
{
ctr = (char) Key.IndexOf(str[count]);
++count;
ctrNext = (char) Key.IndexOf(str[count]);
ctr = ((char)((ctr << 2) | ((ctrNext >> 4) & 0x3)));
R += ctr;
if (++count < size)
{
ctr = str[count];
if (CharacterToFill == ctr)
break;

ctr = (char) Key.IndexOf(ctr);
ctrNext = (char)(((ctrNext << 4) & 0xf0) | ((ctr >> 2) & 0xf));
R += ctrNext;
}

if (++count < size)
{
ctrNext = str[count];
if (CharacterToFill == ctrNext)
break;

ctrNext = (char) Key.IndexOf(ctrNext);
ctr = (char)(((ctr << 6) & 0xc0) | ctrNext);
R += ctr;
}
}
return(R);
}
}


Comments

No responses found. Be the first to comment...


  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name:
    Email: