Click here to Skip to main content
15,891,253 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i want to create an encrypted file like table where i can stored data and read form it when need. And can edit the file.

For example i wants to create a user file where i keep userName and password. i can insert, update, delete the userName and password. when i login to my application then check userName and password form the file.
Posted

Don't.
The best solution is not to store the password at all - store a hash of the password and check that instead.
There is a description of the process here: Password Storage: How to do it.[^]
It's based around databases, since that is where such info is normally held, but the same applies for text files as well. Encryption is not a good idea for passwords, as the decryption key is always needed by your code (and thus available to anyone who has access to the file itself)
 
Share this answer
 
Comments
Md. Mahfujul 21-Jan-13 5:20am    
I only give an example with password. i don't do this. i use this another purpose
OriginalGriff 21-Jan-13 5:33am    
Then explain the purpose - we can't give accurate answers from inaccurate data! :laugh:
HI,

You can use the encription and decription of any kind of strings using the following two methods.

public static string Decrypt(string stringToDecrypt)
       {
           MemoryStream ms = null;
           byte[] inputByteArray = new byte[stringToDecrypt.Length + 1];
           try
           {
               byte[] key = { };
               string sEncryptionKey = "!#$a54?3";
               byte[] IV = { 18, 52, 86, 120, 144, 171, 205, 239 };

               key = Encoding.UTF8.GetBytes(sEncryptionKey);
               DESCryptoServiceProvider des = new DESCryptoServiceProvider();
               inputByteArray = Convert.FromBase64String(stringToDecrypt);
               ms = new MemoryStream();
               CryptoStream cs = new CryptoStream(ms, des.CreateDecryptor(key, IV), CryptoStreamMode.Write);
               cs.Write(inputByteArray, 0, inputByteArray.Length);
               cs.FlushFinalBlock();
               System.Text.Encoding encoding = System.Text.Encoding.UTF8;
               return encoding.GetString(ms.ToArray());
           }
           catch (Exception e)
           {
               return e.Message;
           }
           finally
           {
               ms = null;
           }
       }


public static string Encrypt(string stringToEncrypt)
        {
            MemoryStream ms = null;
            try
            {
                byte[] key = { };
                string sEncryptionKey = "!#$a54?3";
                byte[] IV = { 18, 52, 86, 120, 144, 171, 205, 239 };
                key = Encoding.UTF8.GetBytes(sEncryptionKey);
                DESCryptoServiceProvider des = new DESCryptoServiceProvider();
                byte[] inputByteArray = Encoding.UTF8.GetBytes(stringToEncrypt);
                ms = new MemoryStream();
                CryptoStream cs = new CryptoStream(ms, des.CreateEncryptor(key, IV), CryptoStreamMode.Write);
                cs.Write(inputByteArray, 0, inputByteArray.Length);
                cs.FlushFinalBlock();
                return Convert.ToBase64String(ms.ToArray());
            }
            catch (Exception e)
            {
                return e.Message;
            }
            finally
            {
                ms = null;
            }
        }


Use these two method in your encription and decription of data.

Thanks
 
Share this answer
 

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