In this blog I will told you that how to encrypt records using RijndaelManaged method. I had created RegendelCryptoServiceProvider class which have EncryptRecords method is used to encrypt records.

/// <summary>
    /// This class is used to encrypt records
    /// </summary>
    public class RegendelCryptoServiceProvider
        //This will work as a private key which is used to encrypt.
        private static string Key = "84B63809-D26F-4C45-B22E-9414AB51B018";
        //This will work as a password which is used to encrypt.
        private static byte[] Password = { 0, 1, 0x12, 0xAF, 0x23, 4, 6 };
        /// <summary>
        /// EncryptRecords method accept records which needs to
        /// be encrypted and return an encrypted records.
        /// </summary>
        /// <param name="recordsToBeEncrypted"></param>
        /// <returns></returns>
        public static string EncryptRecords(string recordsToBeEncrypted)
            //Create an object of RijndaelManaged class.
            RijndaelManaged rijendalManaged = new RijndaelManaged();
            //Retrive input records in byte format.
            byte[] convertedText = Encoding.ASCII.GetBytes(recordsToBeEncrypted);
            //Create a strong key so it is hard to detect.
            byte[] securityKey = Encoding.ASCII.GetBytes(Key.Length.ToString());
            //Create a password for encryption.
            PasswordDeriveBytes secretKey = new PasswordDeriveBytes(Password, securityKey);
            //Create a 16 bit encrypter from existing secret key.
            ICryptoTransform Encryptor = rijendalManaged.CreateEncryptor(secretKey.GetBytes(32), secretKey.GetBytes(16));
            // Create a MemoryStream that is going to hold the encrypted bytes
            MemoryStream memoryStream = new MemoryStream();
            // Create a CryptoStream through which we are going to be processing our data.
            // CryptoStreamMode.Write means that we are going to be writing data
            // to the stream and the output will be written in the MemoryStream
            // we have provided. (always use write mode for encryption)
            CryptoStream cryptoStream = new CryptoStream(memoryStream, Encryptor, CryptoStreamMode.Write);
            // Start the encryption process.
            cryptoStream.Write(convertedText, 0, convertedText.Length);
            // Finish encrypting.
            // Convert our encrypted data from a memoryStream into a byte array.
            byte[] CipherBytes = memoryStream.ToArray();
            // Close both streams.
            // Convert encrypted data into a base64-encoded string.
            // A common mistake would be to use an Encoding class for that.
            // It does not work, because not all byte values can be
            // represented by characters. We are going to be using Base64 encoding
            // That is designed exactly for what we are trying to do.
            string EncryptedData = Convert.ToBase64String(CipherBytes);
            // Return encrypted string.
            return EncryptedData;           

Thanks for reading this blog.

  Modified On Sep-18-2014 01:24:02 PM

Leave Comment