T
toupeira23
Hello,
I'm trying to encrypt passwords in my app. After discovering that
there's no simple function to do this, I wrote a wrapper class which
decodes a string using UTF8, encrypts it with TripleDES and returns a
Base64-encoded string. The decryption function does the reverse, i.e.
Base64-decodes the string, decrypts it with the same Key and IV, and
encodes it again with UTF8. The problem is that after decrypting, the
8th character is not the same as it was before, e.g. in the example
below "testtest" changes to "testtesC". It's always on the 8th
character, all others are correctly decrypted. And the really strange
thing is, if you enable the commented-out code in Main(), it works
correctly! I've tried recreating the TripleDESCryptoServiceProvider on
every encrypt/decrypt, but that doesn't help.
Can anybody explain what's going on? Is this a(nother) bug in .NET
itself?
Here's the code I'm using:
using System;
using System.Text;
using System.Security.Cryptography;
public sealed class Crypto
{
private static TripleDESCryptoServiceProvider des;
private static TripleDESCryptoServiceProvider DES
{
get {
if (des == null) {
des = new TripleDESCryptoServiceProvider();
des.Key = Encoding.UTF8.GetBytes("0123456789012345");
des.IV = Encoding.UTF8.GetBytes(System.Environment.MachineName);
}
return des;
}
}
public static string Encrypt(string text)
{
try {
if (text == "")
return "";
byte[] bytes = Encoding.UTF8.GetBytes(text);
ICryptoTransform encryptor = DES.CreateEncryptor();
return Convert.ToBase64String(encryptor.TransformFinalBlock(bytes,
0, bytes.Length));
} catch (Exception e) {
Console.WriteLine(e.ToString());
return "";
}
}
public static string Decrypt(string text)
{
try {
if (text == "")
return "";
byte[] bytes = Convert.FromBase64String(text);
ICryptoTransform decryptor = DES.CreateDecryptor();
return Encoding.UTF8.GetString(decryptor.TransformFinalBlock(bytes,
0, bytes.Length));
} catch (Exception e) {
Console.WriteLine(e.ToString());
return "";
}
}
public static void Main()
{
string test = "testtest";
/*
Console.WriteLine(Encrypt(test));
Console.WriteLine(Decrypt(Encrypt(test)));
*/
string encrypted = Encrypt(test);
Console.WriteLine(encrypted);
string decrypted = Decrypt(encrypted);
Console.WriteLine(decrypted);
}
}
thanks,
markus
I'm trying to encrypt passwords in my app. After discovering that
there's no simple function to do this, I wrote a wrapper class which
decodes a string using UTF8, encrypts it with TripleDES and returns a
Base64-encoded string. The decryption function does the reverse, i.e.
Base64-decodes the string, decrypts it with the same Key and IV, and
encodes it again with UTF8. The problem is that after decrypting, the
8th character is not the same as it was before, e.g. in the example
below "testtest" changes to "testtesC". It's always on the 8th
character, all others are correctly decrypted. And the really strange
thing is, if you enable the commented-out code in Main(), it works
correctly! I've tried recreating the TripleDESCryptoServiceProvider on
every encrypt/decrypt, but that doesn't help.
Can anybody explain what's going on? Is this a(nother) bug in .NET
itself?
Here's the code I'm using:
using System;
using System.Text;
using System.Security.Cryptography;
public sealed class Crypto
{
private static TripleDESCryptoServiceProvider des;
private static TripleDESCryptoServiceProvider DES
{
get {
if (des == null) {
des = new TripleDESCryptoServiceProvider();
des.Key = Encoding.UTF8.GetBytes("0123456789012345");
des.IV = Encoding.UTF8.GetBytes(System.Environment.MachineName);
}
return des;
}
}
public static string Encrypt(string text)
{
try {
if (text == "")
return "";
byte[] bytes = Encoding.UTF8.GetBytes(text);
ICryptoTransform encryptor = DES.CreateEncryptor();
return Convert.ToBase64String(encryptor.TransformFinalBlock(bytes,
0, bytes.Length));
} catch (Exception e) {
Console.WriteLine(e.ToString());
return "";
}
}
public static string Decrypt(string text)
{
try {
if (text == "")
return "";
byte[] bytes = Convert.FromBase64String(text);
ICryptoTransform decryptor = DES.CreateDecryptor();
return Encoding.UTF8.GetString(decryptor.TransformFinalBlock(bytes,
0, bytes.Length));
} catch (Exception e) {
Console.WriteLine(e.ToString());
return "";
}
}
public static void Main()
{
string test = "testtest";
/*
Console.WriteLine(Encrypt(test));
Console.WriteLine(Decrypt(Encrypt(test)));
*/
string encrypted = Encrypt(test);
Console.WriteLine(encrypted);
string decrypted = Decrypt(encrypted);
Console.WriteLine(decrypted);
}
}
thanks,
markus