---
title: "Encrypting Records using RijndaelManaged method"  
description: "In this blog I will told you that how to encrypt records using RijndaelManaged method. I had created RegendelCryptoServiceProvider class which have En"  
author: "Anonymous User"  
published: 2012-04-15  
updated: 2014-09-18  
canonical: https://www.mindstick.com/blog/297/encrypting-records-using-rijndaelmanaged-method  
category: "c#"  
tags: ["c#"]  
reading_time: 2 minutes  

---

# Encrypting Records using RijndaelManaged method

In this [blog](https://www.mindstick.com/articles/12705/myths-and-misconception-about-blog) I will told you that how to encrypt [records](https://www.mindstick.com/forum/34640/how-to-create-a-stored-procedure-for-display-all-records) using RijndaelManaged [method](https://www.mindstick.com/forum/166/webservice-method). I had created RegendelCryptoServiceProvider [class](https://www.mindstick.com/blog/165/generic-class-in-c-sharp) 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.            cryptoStream.FlushFinalBlock();            // Convert our encrypted data from a memoryStream into a byte array.            byte[] CipherBytes = memoryStream.ToArray();            // Close both streams.            memoryStream.Close();            cryptoStream.Close();            // 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](https://www.mindstick.com/articles/126273/books-commonly-found-on-college-reading-lists) this blog.

---

Original Source: https://www.mindstick.com/blog/297/encrypting-records-using-rijndaelmanaged-method

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
