Click here to Skip to main content
15,887,596 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hi ,I have problem durring writing encrypted data in file and also for decryption
(I used code of c given in MSDN ( CryptEncrpt & cryptdecrypt function))
Posted
Comments
Prerak Patel 17-Oct-11 2:35am    
Not clear. Elaborate more.
RICKY_Xxx 17-Oct-11 3:52am    
ok I have work to transform c code of Encryption & Decryption into C#.
using function given in advapi.dll.(if u want to see the code of c search in MSDN with SDK platform with function name CryptEncypt())

1 solution

see the below code.
we will use encryption-decryption provide by .Net.
for example i will do encrypt and decrypt password using below code.

XML
public class DataSecurity
    {
        static byte[] bytes = ASCIIEncoding.ASCII.GetBytes("ZeroCool");

        /// <summary>
        /// Encrypt normal string.
        /// </summary>
        /// <param name="originalString">String to be encrypted.</param>
        /// <returns>Encrypted string</returns>
        public string Encrypt(string originalString)
        {
            if (String.IsNullOrEmpty(originalString))
                return string.Empty;

            DESCryptoServiceProvider cryptoProvider = new DESCryptoServiceProvider();
            MemoryStream memoryStream = new MemoryStream();
            CryptoStream cryptoStream = new CryptoStream(memoryStream,
                cryptoProvider.CreateEncryptor(bytes, bytes), CryptoStreamMode.Write);
            StreamWriter writer = new StreamWriter(cryptoStream);
            writer.Write(originalString);
            writer.Flush();
            cryptoStream.FlushFinalBlock();
            writer.Flush();
            return Convert.ToBase64String(memoryStream.GetBuffer(), 0, (int)memoryStream.Length);
        }

        /// <summary>
        /// Decrypt encrypted string to the normal string
        /// </summary>
        /// <param name="cryptedString">Decrypted string</param>
        /// <returns>Normal (Decrypted) string</returns>
        public string Decrypt(string cryptedString)
        {
            if (String.IsNullOrEmpty(cryptedString))
            {
                throw new ArgumentNullException
                   ("The string which needs to be decrypted can not be null.");
            }
            DESCryptoServiceProvider cryptoProvider = new DESCryptoServiceProvider();
            MemoryStream memoryStream = new MemoryStream
                    (Convert.FromBase64String(cryptedString));
            CryptoStream cryptoStream = new CryptoStream(memoryStream,
                cryptoProvider.CreateDecryptor(bytes, bytes), CryptoStreamMode.Read);
            StreamReader reader = new StreamReader(cryptoStream);
            return reader.ReadToEnd();
        }
    }



Using it in any method as per below.


C#
string encryptedPassWord = (new DataSecurity()).Encrypt("YourPassWord");

string decryptedPassWord = (new DataSecurity()).Decrypt(encryptedPassWord);


you can use XML for storing and retrieving this encrypted-decrypted string values.
 
Share this answer
 
v3

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