Take the 2-minute tour ×

I referred to this article http://www.codeproject.com/KB/security/DotNetCrypto.aspx and I am trying write an encrypted string instead of plain text. Below is the code I am using:

TextWriter tw = new StreamWriter("c:\\temp\\test.txt");
string plainString = "String to be encrypted";
PasswordDeriveBytes pdb = new PasswordDeriveBytes("Test",new byte[] {0x49, 0x76, 0x61, 0x6e, 0x20, 0x4d,0x65, 0x64, 0x76, 0x65, 0x64, 0x65, 0x76});
Rijndael alg = Rijndael.Create();
alg.Key = pdb.GetBytes(32);
alg.IV = pdb.GetBytes(16);
MemoryStream ms = new MemoryStream(); 
CryptoStream cs = new CryptoStream(ms,alg.CreateEncryptor(), CryptoStreamMode.Write);
byte[] clearBytes = System.Text.Encoding.Unicode.GetBytes(plainString);
cs.Write(clearBytes, 0, clearBytes.Length);

However, when I open the file, I get System.IO.MemoryStream instead of some encrypted characters. What did I miss?

share|improve this question
You forgot to read and understand any of the code you copy&pasted. –  GregS Dec 22 '10 at 12:02
add comment

3 Answers

up vote -1 down vote accepted

I think .net supports very well to encrypt string using MD5 algorithm. If you want to use MD5 see the following code.

private void encrypt(ref string password)
        Int32 counter;
        Char[] passwordArr;
        String encryptedPassword;
        Byte[] hashedPassword;
        MD5CryptoServiceProvider obj = new MD5CryptoServiceProvider();

        passwordArr = password.ToCharArray();
        Byte[] passwordBytes = new byte[passwordArr.Length - 1];
        for (counter = 0; counter < passwordBytes.Length; counter++)
            passwordBytes[counter] = Convert.ToByte(passwordArr[counter]);
        hashedPassword = obj.ComputeHash(passwordBytes);
        encryptedPassword = BitConverter.ToString(hashedPassword);
        password =  encryptedPassword;
        obj = null;
share|improve this answer
Dumb question, can i decrypt my string back if i use MD5? How to? –  JewelThief Dec 22 '10 at 4:49
I could figure the decryption part..and it works –  JewelThief Dec 22 '10 at 5:19
Your code assumes that char == byte, which is wrong. Use Encoding.GetBytes instead. –  Simon Svensson Dec 22 '10 at 7:07
-1 MD5 is a hashing algorithm, not an encryption algorithm i.e. it only goes one way. You cannot retrieve the original plaintext from an MD5 hash value. –  Chris Dickson Dec 22 '10 at 10:17
Still today i treat both are similar only. Thanks for the clarification between hashing and encryption. –  Mahantesh Dec 22 '10 at 14:19
add comment

well the issue is with the ms.ToString(). your should rather be reading the bytes from memorystream, changing it to appropriate encoding and then writing to the text stream.

share|improve this answer
How to? sample please –  JewelThief Dec 22 '10 at 4:47
byte[] bytes = ms.ToArray(); string s = Encoding.GetEncoding("whatever").GetString(bytes,0,bytes.Length);tw.WriteLine(s)‌​; –  Mubashir Khan Dec 22 '10 at 5:14
add comment

MemoryStream.ToString() relies on the default implementation of Object.ToString(), which writes out the classname. Create a FileStream and pass that to the CryptoStream ctor.


share|improve this answer
add comment

Your Answer


