Click here to Skip to main content
15,911,531 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
Hi All,

I was working on an application where I want to keep all my data in encrypted format in files but when I will send mails to user it should be decrypted reading from files and send to user.

So I wrote a security dll using Triple Des algorithm. While encrypting I will convert whole text to UTF8 format and use MD5 hashing and encrypt my string text.

byte[] toEncryptArray = UTF8Encoding.UTF8.GetBytes(ToEncrypt);


While decrypting I will convert to Base64 and try to decrypt but this is creating problem for me.

byte[] toDecryptArray = Convert.FromBase64String(cypherString);


While decrypting it will work fine sometimes but it gives error for some string after few minutes. Not valid Base64 format exception.
I don’t know what the issue here is. Why it’s giving me error. But when same text I will try to convert from by test application it work fine. I am reading text from files and converting it.

Please any suggestions

Thanks
Posted
Updated 5-May-11 23:32pm
v2

1 solution

Why the heck are you trying to convert from a Base64 string? Did you convert you encrypted data to a Base64 string?
If you didn't, then that's your problem, right there...

MD5 is not an encryption algorithm. It is a hashing algorithm. The difference is that encrypted text can be decrypted back to the original data. Hashed text cannot: hashing is a one way process. So if you are trying to use MD5 to encrypt your data, then:
1) It won't work - you cannot get your data back from an MD5 hash.
2) MD5 is not recommended any more - it is officially classed as "broken" - use SHA instead.
 
Share this answer
 
Comments
sunder.tinwar 6-May-11 5:26am    
Hi griff thanks for reply but you should read question again i am not using MD5 for encrypting data its just for validating that user should not temper with my data.

anyhow this is code i am using.
class CryptorEngine
{
public static string Encrypt(string ToEncrypt, bool useHasing)
{
byte[] keyArray;
byte[] toEncryptArray = UTF8Encoding.UTF8.GetBytes(ToEncrypt);
//System.Configuration.AppSettingsReader settingsReader = new AppSettingsReader();
string Key = "some key";
if (useHasing)
{
MD5CryptoServiceProvider hashmd5 = new MD5CryptoServiceProvider();
keyArray = hashmd5.ComputeHash(UTF8Encoding.UTF8.GetBytes(Key));
hashmd5.Clear();
}
else
{
keyArray = UTF8Encoding.UTF8.GetBytes(Key);
}
TripleDESCryptoServiceProvider tDes = new TripleDESCryptoServiceProvider();
tDes.Key = keyArray;
tDes.Mode = CipherMode.ECB;
tDes.Padding = PaddingMode.PKCS7;
ICryptoTransform cTransform = tDes.CreateEncryptor();
byte[] resultArray = cTransform.TransformFinalBlock(toEncryptArray, 0, toEncryptArray.Length);
tDes.Clear();
return Convert.ToBase64String(resultArray, 0, resultArray.Length);
}
public static string Decrypt(string cypherString, bool useHasing)
{
byte[] keyArray;
byte[] toDecryptArray = Convert.FromBase64String(cypherString);
//byte[] toEncryptArray = Convert.FromBase64String(cypherString);
//System.Configuration.AppSettingsReader settingReader = new AppSettingsReader();
string key = "some key";
if (useHasing)
{
MD5CryptoServiceProvider hashmd = new MD5CryptoServiceProvider();
keyArray = hashmd.ComputeHash(UTF8Encoding.UTF8.GetBytes(key));
hashmd.Clear();
}
else
{
keyArray = UTF8Encoding.UTF8.GetBytes(key);
}
TripleDESCryptoServiceProvider tDes = new TripleDESCryptoServiceProvider();
tDes.Key = keyArray;
tDes.Mode = CipherMode.ECB;
tDes.Padding = PaddingMode.PKCS7;
ICryptoTransform cTransform = tDes.CreateDecryptor();
try
{
byte[] resultArray = cTransform.TransformFinalBlock(toDecryptArray, 0, toDecryptArray.Length);
tDes.Clear();
return UTF8Encoding.UTF8.GetString(resultArray, 0, resultArray.Length);
}
catch (Exception ex)
{
throw ex;
}
}
sunder.tinwar 6-May-11 5:38am    
so griff you tried the code it would be working fine for you. So now how to resolve this issue.
Sergey Alexandrovich Kryukov 6-May-11 10:40am    
Stop using hashing! You've been explained that already; why posting this code? It has nothing to do with encryption (even though it is cryptic :-)
--SA
Sergey Alexandrovich Kryukov 6-May-11 10:38am    
What a great OP's confusion!
Good luck answering the follow-up questions... :-)

I just posted a short comment to OP, that's all I can help you with right now... :-)

For now, get my 5.
--SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900