Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Problem Decrypting password
Message
From
21/01/2011 03:31:05
 
 
To
All
General information
Forum:
ASP.NET
Category:
Coding, syntax and commands
Title:
Problem Decrypting password
Environment versions
Environment:
C# 4.0
OS:
Windows 7
Network:
Windows 2003 Server
Database:
Visual FoxPro
Application:
Desktop
Miscellaneous
Thread ID:
01496813
Message ID:
01496813
Views:
144
I am trying to store username & password in isolated storage. I have no issue when I use plain test, but I have now added encryption to the password and can not get it to decrypt when I read it back.

The code is run from a form which has 2 testboxes on txtUserName and txtPassword, the writing of the password to isolated storage works fine, it's when I try and decrypt the password, the problem appears to be;
CryptoStream myCryptoStream = new CryptoStream(myMemoryStream,cryptoKey.CreateDecryptor(), CryptoStreamMode.Read);
MyMemoryStream has a length of 28, but querying the length of myCryptoStream shows

Length = 'myCryptoStream.Length' threw an exception of type 'System.NotSupportedException'


And then the code calls over on
string myPlainText = myStreamReader.ReadToEnd();                
With an "invalid Length" error.

Any ideas? Full code below

Gary.
   private void WriteUserLogon()
        {
            //create a store stream
            IsolatedStorageFile myStorage = IsolatedStorageFile.GetUserStoreForAssembly();
            IsolatedStorageFileStream userStream = new IsolatedStorageFileStream("Logon.dat", System.IO.FileMode.OpenOrCreate);

            String userPassword_Encrypted = Cryptography.EncryptString(txtPassword.Text);
            StreamWriter userLogonStream = new StreamWriter(userStream);
            userLogonStream.WriteLine("<UserName>" + txtUserName.Text + "</UserName>");
            userLogonStream.WriteLine("<Password>" + userPassword_Encrypted + "</Password>");
            userLogonStream.Close();
            userStream.Close();
            myStorage.Close();

     private void ReadUserLogon()
        {
            //create a store stream
            IsolatedStorageFile myStorage = IsolatedStorageFile.GetUserStoreForAssembly();
            IsolatedStorageFileStream userStream = new IsolatedStorageFileStream("Logon.dat", System.IO.FileMode.OpenOrCreate);

            StreamReader userLogonStream = new StreamReader(userStream);
            String logonData = userLogonStream.ReadToEnd();
            userLogonStream.Close();
            userStream.Close();
            myStorage.Close();

            if(logonData.IndexOf("UserName")>0) 
            {
                int startPos = logonData.IndexOf("<UserName>")+10;
                int endPos = logonData.IndexOf("</UserName>");
                String userName = logonData.Substring(startPos , endPos-startPos);

                txtUserName.Text = userName;
            }
            
            if (logonData.IndexOf("Password") > 0)
            {
                int startPos = logonData.IndexOf("<Password>") + 10;
                int endPos = logonData.IndexOf("</Password>");
                String userPassword_Encrypted = logonData.Substring(startPos, endPos - startPos);
                String userPassword = Cryptography.DecryptString(userPassword_Encrypted);
                txtPassword.Text = userPassword;
            }

        }

public static byte[] Encrypt(string myPlainText)
        {
            MemoryStream myMemoryStream = new MemoryStream();
            DESCryptoServiceProvider cryptoKey = new DESCryptoServiceProvider();
            CryptoStream myCryptoStream = new CryptoStream(myMemoryStream, cryptoKey.CreateEncryptor(), CryptoStreamMode.Write);
            StreamWriter myStreamWriter = new StreamWriter(myCryptoStream);
            myStreamWriter.WriteLine(myPlainText);
            myStreamWriter.Close();
            myCryptoStream.Close();
            byte[] myBuffer = myMemoryStream.ToArray();
            myMemoryStream.Close();
            return myBuffer;

        }
        public static string Decrypt(byte[] myCryptoText)
        {
            MemoryStream myMemoryStream = new MemoryStream(myCryptoText);
            DESCryptoServiceProvider cryptoKey = new DESCryptoServiceProvider();
            CryptoStream myCryptoStream = new CryptoStream(myMemoryStream, cryptoKey.CreateDecryptor(), CryptoStreamMode.Read);

            StreamReader myStreamReader = new StreamReader(myCryptoStream);

//Next Line Fails
            string myPlainText = myStreamReader.ReadToEnd();                

            myStreamReader.Close();
            myCryptoStream.Close();
            myMemoryStream.Close();
            return myPlainText;

        }
Next
Reply
Map
View

Click here to load this message in the networking platform