Click here to Skip to main content
15,887,175 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi i have the following encrypt and decrypt codes. But when i run my program, the weird Mars language for the userID and password would appear. What is the problem?

C#
   public string EncryptPassWord(string password)
    {
        if (string.IsNullOrEmpty(password)) throw new ArgumentNullException("password");
        string en_password = null;

        RijndaelManaged aesPassword = null;

        try
        {
            Rfc2898DeriveBytes passwordkey = new Rfc2898DeriveBytes(password, _salt);

            aesPassword = new RijndaelManaged();
            aesPassword.Padding = PaddingMode.None;

            ICryptoTransform passwordEncryptor = aesPassword.CreateEncryptor(aesPassword.Key, aesPassword.IV);

            using (MemoryStream passEncryptor = new MemoryStream())
            {
                passEncryptor.Write(BitConverter.GetBytes(aesPassword.IV.Length), 0, sizeof(int));
                passEncryptor.Write(aesPassword.IV, 0, aesPassword.IV.Length);
                using (CryptoStream cspassEncrypt = new CryptoStream(passEncryptor, passwordEncryptor, CryptoStreamMode.Write))
                {
                    using (StreamWriter swpassEncryptor = new StreamWriter(cspassEncrypt))
                    {
                        swpassEncryptor.Write(password);
                    }
                }
                en_password = Convert.ToBase64String(passEncryptor.ToArray());
            }

        }
        finally
        {
            if (aesPassword != null) { aesPassword.Clear(); }
        }
        return en_password;
    }







public string DecryptPassword(string dePassword)
    {
        if (string.IsNullOrEmpty(dePassword)) throw new ArgumentNullException("dePassword");

        RijndaelManaged aesDePassword = null;
        string deCPassword = null;

        try
        {
            Rfc2898DeriveBytes dePassKey = new Rfc2898DeriveBytes(dePassword, _salt);

            byte[] bytes = Convert.FromBase64String(dePassword);
            using(MemoryStream msPassDecrypt = new MemoryStream(bytes))
            {
                aesDePassword = new RijndaelManaged();
                aesDePassword.Padding = PaddingMode.None;
                aesDePassword.Key = dePassKey.GetBytes(aesDePassword.KeySize / 8);
                aesDePassword.IV = ReadByteArray(msPassDecrypt);

                ICryptoTransform passDecryptor = aesDePassword.CreateDecryptor(aesDePassword.Key, aesDePassword.IV);
                using (CryptoStream csPassDecrypt = new CryptoStream(msPassDecrypt, passDecryptor, CryptoStreamMode.Read))
                {
                    using(StreamReader srPassDecrypt = new StreamReader(csPassDecrypt))
                    {
                        deCPassword = srPassDecrypt.ReadToEnd();
                    }
                }

            }
        }
        finally
        {
            if (aesDePassword != null)
            {
                aesDePassword.Clear();
            }
        }

        return deCPassword;
    }









    private static byte[] ReadByteArray(Stream s)
    {
        byte[] rawLength = new byte[sizeof(int)];
        if (s.Read(rawLength, 0, rawLength.Length) != rawLength.Length)
        {
            throw new SystemException("Stream did not contain properly formatted byte array");
        }

        byte[] buffer = new byte[BitConverter.ToInt32(rawLength, 0)];
        if(s.Read(buffer, 0, buffer.Length) != buffer.Length)
        {
            throw new SystemException("Did not read byte array properly");
        }
        return buffer;
    }
Posted
Updated 30-Mar-14 21:26pm
v2

1 solution

The problem is not that you just screwed up encryption (apparently). It's worse: the idea to use any symmetric algorithm for encrypting password it totally disastrous, and the idea to encrypt it at all would be questionable, or, rather, unacceptable.

The thing is: you don't know to have somehow decrypted password at all. It is absolutely not needed for authentication. Surprised? Feel disagree? Then please read my past answers:
i already encrypt my password but when i log in it gives me an error. how can decrypte it[^],
Decryption of Encrypted Password[^],
storing password value int sql server with secure way[^].

—SA
 
Share this answer
 
Comments
Maciej Los 1-Apr-14 16:46pm    
Absolutely agree!
+5!
Sergey Alexandrovich Kryukov 1-Apr-14 17:12pm    
Thank you, Maciej.
—SA
Sergey Alexandrovich Kryukov 1-Apr-14 17:13pm    
Maciej, did you receive a notification on this post? It looks like there a glitch in posting comments again...
—SA
Maciej Los 1-Apr-14 17:15pm    
Yes, i did.
There are some new visual effects. Comments are not displayed in a tree structure (sometimes).
Sergey Alexandrovich Kryukov 1-Apr-14 18:34pm    
I see, thank you.
—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