Simple encryption example

I see requests for CF encryption examples on a regular basis in the newsgroups and the Forums.  Well, here’s one I used in an app for storing a password in the registry using the OpenNETCF Encryption stuff :

internal static byte[] GetIV(string keyString)
{
byte[] bytes = Encoding.Unicode.GetBytes(keyString);
byte[] iv = new byte[24];
if(bytes.Length < 24)
{
for(int i = 0 ; i < 24 ; i++)
{
iv[i] = bytes[i % bytes.Length];
}
}
else
{
Array.Copy(bytes, 0, iv, 0, 24);
}
return iv;
}
internal static byte[] Encrypt(string toEncrypt, byte[] iv)
{
TripleDESCryptoServiceProvider des = null;
try
{
des = new TripleDESCryptoServiceProvider();
}
catch(Exception)
{
MessageBox.Show(“The high encryption pack must be installed. Please install and try again.”,
“Crypto Failure”,
MessageBoxButtons.OK,
MessageBoxIcon.Exclamation,
MessageBoxDefaultButton.Button1);
return null;
}
des.Key = PRIVATE_KEY;
des.IV = iv;
return des.EncryptValue(Encoding.Unicode.GetBytes(toEncrypt));
}
internal static string Decrypt(byte[] toDecrypt, byte[] iv)
{
TripleDESCryptoServiceProvider des = null;
try
{
des = new TripleDESCryptoServiceProvider();
}
catch(Exception)
{
MessageBox.Show(“The high encryption pack must be installed. Please install and try again.”,
“Crypto Failure”,
MessageBoxButtons.OK,
MessageBoxIcon.Exclamation,
MessageBoxDefaultButton.Button1);
return null;
}
des.Key = PRIVATE_KEY;
des.IV = iv;
byte[] decBytes = des.DecryptValue(toDecrypt);
return Encoding.Unicode.GetString(decBytes, 0, decBytes.Length);
}
Usage is simple:
byte[] encryptedPwd = AppGlobal.Encrypt(password, GetIV(PUBLIC_KEY));
string previousPwd = Decrypt(encryptedPwd , GetIV(PUBLIC_KEY));

1 thought on “Simple encryption example”

  1. Hai,
    Last day I read the article about WorldReady pocket pc application in microsoft’s madn library. It is helps me a lot to understand the localization concept. Thanks for your valueable information.

    I’m in hungry to give a same solution for Thai application. I tried in your way, but as msdn mentiond I’m facing "PlatformNotSupported" error when I’m creating the instance for thai culture info.

    I want the solution for Pocket PC 2002. So I checked whether Thai is listed in the Regional settings. I thought that is the root cause for the mentioned error.
    (I coudn’t check whether Thai is supported or not by lising all possible culture info. Because, Getting any culture info is not possible in .Net CF)

    But, I saw some application which are running with Thai letters. So I confirmed that there is some othere option to do that. But I don’t know what it is.

    Please give me your hands to come out of this problem. Thanks in advance.

    Regards,
    Marimuthu K
    Envision Information Systems & Technologies,
    Chennai,
    Tamilnadu,
    India.
    E-mail: k.marimuthu@gmail.com

    Like

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s