Windows Develop Bookmark and Share   
 index > Windows Forms General > Textbox Dropdown, load username and password
 

Textbox Dropdown, load username and password

Hi there.

I wonder how i do so when i write a word in a textbox and close my program it saves it.
So when i start my program it loads all the words in a dropdownlist.

Dose the textbox have this propertie or must i use a dropdownlist
  • Edited byAlcstudio Monday, September 14, 2009 11:22 PM
  •  
Alcstudio  Monday, September 14, 2009 10:14 PM
You can encrypt and store the password and the corresponding user in some xml file, and whenever the user name entered by user matches the username in the xml then decrypt the password and populate in the form.

Here is the sample code for encryption and decryption, but the problem is this code has key hardcoded in it. its just an example, you can use DPAPI if you dont wanna hardcode th ekey..

using System;
using System.Data;
using System.Security.Cryptography;
using System.IO;


public class SimpleAES
{
    // Change these keys
    private byte[] Key = { 123, 217, 19, 11, 24, 26, 85, 45, 114, 184, 27, 162, 37, 112, 222, 209, 241, 24, 175, 144, 173, 53, 196, 29, 24, 26, 17, 218, 131, 236, 53, 209 };
    private byte[] Vector = { 146, 64, 191, 111, 23, 3, 113, 119, 231, 121, 25, 21, 112, 79, 32, 114, 156 };


    private ICryptoTransform EncryptorTransform, DecryptorTransform;
    private System.Text.UTF8Encoding UTFEncoder;

    public SimpleAES()
    {
        //This encryption method
        RijndaelManaged rm = new RijndaelManaged();

        //Create an encryptor and a decryptor using our encryption method, key, and vector.
        EncryptorTransform = rm.CreateEncryptor(this.Key, this.Vector);
        DecryptorTransform = rm.CreateDecryptor(this.Key, this.Vector);

        //Used to translate bytes to text and vice versa
        UTFEncoder = new System.Text.UTF8Encoding();
    }

    /// ----------- The commonly used methods ------------------------------ 
    /// Encrypt some text and return a string suitable for passing in a URL.
    public string EncryptToString(string TextValue)
    {
        return ByteArrToString(Encrypt(TextValue));
    }

    /// Encrypt some text and return an encrypted byte array.
    public byte[] Encrypt(string TextValue)
    {
        //Translates our text value into a byte array.
        Byte[] bytes = UTFEncoder.GetBytes(TextValue);

        //Used to stream the data in and out of the CryptoStream.
        MemoryStream memoryStream = new MemoryStream();

        /*
        * We will have to write the unencrypted bytes to the stream,
        * then read the encrypted result back from the stream.
        */
        #region Write the decrypted value to the encryption stream
        CryptoStream cs = new CryptoStream(memoryStream, EncryptorTransform, CryptoStreamMode.Write);
        cs.Write(bytes, 0, bytes.Length);
        cs.FlushFinalBlock();
        #endregion

        #region Read encrypted value back out of the stream
        memoryStream.Position = 0;
        byte[] encrypted = new byte[memoryStream.Length];
        memoryStream.Read(encrypted, 0, encrypted.Length);
        #endregion

        //Clean up.
        cs.Close();
        memoryStream.Close();

        return encrypted;
    }

    /// The other side: Decryption methods
    public string DecryptString(string EncryptedString)
    {
        return Decrypt(StrToByteArray(EncryptedString));
    }

    /// Decryption when working with byte arrays. 
    public string Decrypt(byte[] EncryptedValue)
    {
        #region Write the encrypted value to the decryption stream
        MemoryStream encryptedStream = new MemoryStream();
        CryptoStream decryptStream = new CryptoStream(encryptedStream, DecryptorTransform, CryptoStreamMode.Write);
        decryptStream.Write(EncryptedValue, 0, EncryptedValue.Length);
        decryptStream.FlushFinalBlock();
        #endregion

        #region Read the decrypted value from the stream.
        encryptedStream.Position = 0;
        Byte[] decryptedBytes = new Byte[encryptedStream.Length];
        encryptedStream.Read(decryptedBytes, 0, decryptedBytes.Length);
        encryptedStream.Close();
        #endregion
        return UTFEncoder.GetString(decryptedBytes);
    }

    /// Convert a string to a byte array. NOTE: Normally we'd create a Byte Array from a string using an ASCII encoding (like so).
    // System.Text.ASCIIEncoding encoding = new System.Text.ASCIIEncoding();
    // return encoding.GetBytes(str);
    // However, this results in character values that cannot be passed in a URL. So, instead, I just
    // lay out all of the byte values in a long string of numbers (three per - must pad numbers less than 100).
    public byte[] StrToByteArray(string str)
    {
        if (str.Length == 0)
            throw new Exception("Invalid string value in StrToByteArray");

        byte val;
        byte[] byteArr = new byte[str.Length / 3];
        int i = 0;
        int j = 0;
        do
        {
            val = byte.Parse(str.Substring(i, 3));
            byteArr[j++] = val;
            i += 3;
        }
        while (i < str.Length);
        return byteArr;
    }

    // Same comment as above. Normally the conversion would use an ASCII encoding in the other direction:
    // System.Text.ASCIIEncoding enc = new System.Text.ASCIIEncoding();
    // return enc.GetString(byteArr); 
    public string ByteArrToString(byte[] byteArr)
    {
        byte val;
        string tempStr = "";
        for (int i = 0; i <= byteArr.GetUpperBound(0); i++)
        {
            val = byteArr[i];
            if (val < (byte)10)
                tempStr += "00" + val.ToString();
            else if (val < (byte)100)
                tempStr += "0" + val.ToString();
            else
                tempStr += val.ToString();
        }
        return tempStr;

    }
}
hope this helps
Paras
paras kumar  Tuesday, September 15, 2009 9:48 PM
no this is just for encryption and decryption
after successful login you can encrypt the password and save username and password in an xml file.

if(IsLoginSuccessful)
{
            SimpleAES s = new SimpleAES();
            string encryptedPassword = s.EncryptToString(password);
            // write username and encrypted password in xml
}

now while logging in when user provides username then you can search in xml file for this username, find corresponding encrypted password then decrypt it and fill it up in UI for the user.

if(UsernameExistsInXmlFile)
{
            string encryptedPassword = GetpasswordFromXml(username); // this function reads password for a particular username from xml
            SimpleAES s = new SimpleAES();
            string decruptedPassword = s.DecryptString(encryptedPassword);
}
check this out for reading andwriting into xml :
http://www.c-sharpcorner.com/uploadfile/mahesh/readwritexmltutmellli2111282005041517am/readwritexmltutmellli21.aspx

lemme know if you have any doubts
Paras
paras kumar  Wednesday, September 16, 2009 3:09 AM

you can use combobox...

on formclose save combobox.Text to where ever u wanna save (ex : txt or xml file)...
on opening the form populate the combobox from that file.

like combobox.Items.Add("Data from file")

Paras
paras kumar  Monday, September 14, 2009 10:27 PM
To save something on close it looks like this:

Properties.Settings.Default.UsernameSave = username.Text;

And to load it in to the textbox that i had before it looks like this.

username.Text = Properties.Settings.Default.UsernameSave;


How does it look if i want to save it in somekind of file or in the program itself.

The case here is that i have username and password.

When i close the program it saves the username automaticly.
And if you want to save the password you can check a checkbox.

How do i do so when i choose a username from the combobox that it retrives the right password for that user if you have checked the checkbox
Alcstudio  Monday, September 14, 2009 11:22 PM
Understand?
Alcstudio  Tuesday, September 15, 2009 9:12 PM
You can encrypt and store the password and the corresponding user in some xml file, and whenever the user name entered by user matches the username in the xml then decrypt the password and populate in the form.

Here is the sample code for encryption and decryption, but the problem is this code has key hardcoded in it. its just an example, you can use DPAPI if you dont wanna hardcode th ekey..

using System;
using System.Data;
using System.Security.Cryptography;
using System.IO;


public class SimpleAES
{
    // Change these keys
    private byte[] Key = { 123, 217, 19, 11, 24, 26, 85, 45, 114, 184, 27, 162, 37, 112, 222, 209, 241, 24, 175, 144, 173, 53, 196, 29, 24, 26, 17, 218, 131, 236, 53, 209 };
    private byte[] Vector = { 146, 64, 191, 111, 23, 3, 113, 119, 231, 121, 25, 21, 112, 79, 32, 114, 156 };


    private ICryptoTransform EncryptorTransform, DecryptorTransform;
    private System.Text.UTF8Encoding UTFEncoder;

    public SimpleAES()
    {
        //This encryption method
        RijndaelManaged rm = new RijndaelManaged();

        //Create an encryptor and a decryptor using our encryption method, key, and vector.
        EncryptorTransform = rm.CreateEncryptor(this.Key, this.Vector);
        DecryptorTransform = rm.CreateDecryptor(this.Key, this.Vector);

        //Used to translate bytes to text and vice versa
        UTFEncoder = new System.Text.UTF8Encoding();
    }

    /// ----------- The commonly used methods ------------------------------ 
    /// Encrypt some text and return a string suitable for passing in a URL.
    public string EncryptToString(string TextValue)
    {
        return ByteArrToString(Encrypt(TextValue));
    }

    /// Encrypt some text and return an encrypted byte array.
    public byte[] Encrypt(string TextValue)
    {
        //Translates our text value into a byte array.
        Byte[] bytes = UTFEncoder.GetBytes(TextValue);

        //Used to stream the data in and out of the CryptoStream.
        MemoryStream memoryStream = new MemoryStream();

        /*
        * We will have to write the unencrypted bytes to the stream,
        * then read the encrypted result back from the stream.
        */
        #region Write the decrypted value to the encryption stream
        CryptoStream cs = new CryptoStream(memoryStream, EncryptorTransform, CryptoStreamMode.Write);
        cs.Write(bytes, 0, bytes.Length);
        cs.FlushFinalBlock();
        #endregion

        #region Read encrypted value back out of the stream
        memoryStream.Position = 0;
        byte[] encrypted = new byte[memoryStream.Length];
        memoryStream.Read(encrypted, 0, encrypted.Length);
        #endregion

        //Clean up.
        cs.Close();
        memoryStream.Close();

        return encrypted;
    }

    /// The other side: Decryption methods
    public string DecryptString(string EncryptedString)
    {
        return Decrypt(StrToByteArray(EncryptedString));
    }

    /// Decryption when working with byte arrays. 
    public string Decrypt(byte[] EncryptedValue)
    {
        #region Write the encrypted value to the decryption stream
        MemoryStream encryptedStream = new MemoryStream();
        CryptoStream decryptStream = new CryptoStream(encryptedStream, DecryptorTransform, CryptoStreamMode.Write);
        decryptStream.Write(EncryptedValue, 0, EncryptedValue.Length);
        decryptStream.FlushFinalBlock();
        #endregion

        #region Read the decrypted value from the stream.
        encryptedStream.Position = 0;
        Byte[] decryptedBytes = new Byte[encryptedStream.Length];
        encryptedStream.Read(decryptedBytes, 0, decryptedBytes.Length);
        encryptedStream.Close();
        #endregion
        return UTFEncoder.GetString(decryptedBytes);
    }

    /// Convert a string to a byte array. NOTE: Normally we'd create a Byte Array from a string using an ASCII encoding (like so).
    // System.Text.ASCIIEncoding encoding = new System.Text.ASCIIEncoding();
    // return encoding.GetBytes(str);
    // However, this results in character values that cannot be passed in a URL. So, instead, I just
    // lay out all of the byte values in a long string of numbers (three per - must pad numbers less than 100).
    public byte[] StrToByteArray(string str)
    {
        if (str.Length == 0)
            throw new Exception("Invalid string value in StrToByteArray");

        byte val;
        byte[] byteArr = new byte[str.Length / 3];
        int i = 0;
        int j = 0;
        do
        {
            val = byte.Parse(str.Substring(i, 3));
            byteArr[j++] = val;
            i += 3;
        }
        while (i < str.Length);
        return byteArr;
    }

    // Same comment as above. Normally the conversion would use an ASCII encoding in the other direction:
    // System.Text.ASCIIEncoding enc = new System.Text.ASCIIEncoding();
    // return enc.GetString(byteArr); 
    public string ByteArrToString(byte[] byteArr)
    {
        byte val;
        string tempStr = "";
        for (int i = 0; i <= byteArr.GetUpperBound(0); i++)
        {
            val = byteArr[i];
            if (val < (byte)10)
                tempStr += "00" + val.ToString();
            else if (val < (byte)100)
                tempStr += "0" + val.ToString();
            else
                tempStr += val.ToString();
        }
        return tempStr;

    }
}
hope this helps
Paras
paras kumar  Tuesday, September 15, 2009 9:48 PM
All this code is it for the xml file?
Dont have a clue how use it.
Alcstudio  Wednesday, September 16, 2009 1:59 AM
no this is just for encryption and decryption
after successful login you can encrypt the password and save username and password in an xml file.

if(IsLoginSuccessful)
{
            SimpleAES s = new SimpleAES();
            string encryptedPassword = s.EncryptToString(password);
            // write username and encrypted password in xml
}

now while logging in when user provides username then you can search in xml file for this username, find corresponding encrypted password then decrypt it and fill it up in UI for the user.

if(UsernameExistsInXmlFile)
{
            string encryptedPassword = GetpasswordFromXml(username); // this function reads password for a particular username from xml
            SimpleAES s = new SimpleAES();
            string decruptedPassword = s.DecryptString(encryptedPassword);
}
check this out for reading andwriting into xml :
http://www.c-sharpcorner.com/uploadfile/mahesh/readwritexmltutmellli2111282005041517am/readwritexmltutmellli21.aspx

lemme know if you have any doubts
Paras
paras kumar  Wednesday, September 16, 2009 3:09 AM

You can use google to search for other answers

Custom Search

More Threads

• Text Editor
• Multi Lingual Q
• show form without knowing name of form
• How add items to default right-click contextmenu
• StatusStrip hides DataGridView's Horizontal scroll
• how to control excel on C# .Net winform using DSOFramer?
• Track the Browsers urls accessed, Messenger, Mails on Computer...
• Making A Label Resizable? Possible?
• How can I insert user defined data to a virtual listview?
• Listview with persistent view management