Answered by:
Decrypted string displays kanji and korean characters

Question
-
Hello, I'm pretty new to programming so please be gentle. I'm transferring to a different school and a professor gave me an outline of a program they want me to write. I have already done everything they asked for, but I am really interested in security so I added a way to encrypt and decrypt the file made by the program. I know it has no practical purpose, I just wanted to learn how to do it. Anyways; I almost have it working, but during decryption the first 4 bytes of my string are displayed as kanji and korean characters and the rest is displayed properly. I have no idea where the problem is occurring. Any help would be appreciated.
Here is some of the code
public class CMethod { public TripleDES encryptMethod(string key) { Rfc2898DeriveBytes iv = new Rfc2898DeriveBytes(passphrase, salt); MD5 m = new MD5CryptoServiceProvider(); TripleDES d = new TripleDESCryptoServiceProvider(); d.Key = m.ComputeHash(Encoding.Unicode.GetBytes(key)); d.IV = iv.GetBytes(d.BlockSize / 8); return d; } public byte[] createEncryption(string sEncryption, string key) { TripleDES d = encryptMethod(key); ICryptoTransform c = d.CreateEncryptor(); byte[] input = Encoding.Unicode.GetBytes(sEncryption); return c.TransformFinalBlock(input, 0, input.Length); } public string createDecryption(string sDecryption, string key) { byte[] b = Convert.FromBase64String(sDecryption); TripleDES d = encryptMethod(key); ICryptoTransform c = d.CreateDecryptor(); byte[] output = c.TransformFinalBlock(b, 0, b.Length); return Encoding.Unicode.GetString(output); } } public void encryptFile() { string b = sEncryption; CMethod c = new CMethod(); byte[] d = c.createEncryption(b, passphrase); string e = Convert.ToBase64String(d); File.WriteAllText(filePath, e); } public void decryptFile() { string b = File.ReadAllText(filePath); CMethod c = new CMethod(); string d = c.createDecryption(b, passphrase); File.WriteAllText(filePath, d); }
I've been trying a few things and I got it a bit closer by changing the encoding in my createEncryption and createDecryption methods to UTF32, but the first 2 bytes in the array are still not showing up properly. At least it's no longer in kanji. Does anyone have any ideas?
- Edited by eddieb117 Monday, June 25, 2012 7:38 AM
Saturday, June 23, 2012 8:25 PM
Answers
-
I feel really embarrassed. I had accidentally declared two variables with the same name, so once I changed that it worked fine. I said I was pretty new right?...Anyway, I found that after I read your post, so thank you for helping me.
- Marked as answer by Bob Wu-MT Wednesday, June 27, 2012 7:32 AM
Tuesday, June 26, 2012 5:36 PM
All replies
-
Hi eddieb,
Could you please give show a complete sample to reproduce this issue?
You should try use the UTF8Encoding class to get the string and byte. For more details, see the sample in the link below.
http://msdn.microsoft.com/en-us/library/system.security.cryptography.rfc2898derivebytes.aspx
Bob Wu [MSFT]
MSDN Community Support | Feedback to us
Tuesday, June 26, 2012 12:34 PM -
I feel really embarrassed. I had accidentally declared two variables with the same name, so once I changed that it worked fine. I said I was pretty new right?...Anyway, I found that after I read your post, so thank you for helping me.
- Marked as answer by Bob Wu-MT Wednesday, June 27, 2012 7:32 AM
Tuesday, June 26, 2012 5:36 PM -
You're welcome.
I'm glad you detect and solved the issue.
Best Regards,
Bob Wu [MSFT]
MSDN Community Support | Feedback to us
Wednesday, June 27, 2012 7:32 AM