Blog
    C#
    ADO.Net
    .NET
    ASP.Net & Web Forms
    Custom Controls
    Web Development
    Exception Handling
    XML
    Database
    Security in .Net
    Testing
    Web Services
    Windows Services
    Windows Controls
    WCF
    AJAX
    WPF
    XAML
    Reporting
    Setup
    VB.Net
    LINQ
    JQuery
    SilverLight
    JavaScript
    HTML5
    Crystal Report
    Cloud Computing
    Share Point
    Visual C++
    MVC
    Android
    PHP
    Java
    HTML
    WordPress
    Joomla
    Products
    Drupal
    Windows Phone
    JSON
    LightSwitch
    iPhone/iPad
    Ruby on Rails
    IIS 7
    Windows 8
    CSS/CSS3
    Excel
    MS Access
    Shortcut Keys
    Visual SourceSafe
    Team Foundation Server
    APIs
Follow Us
Follow _MindStick_ on Twitter View MindStick Software's LinkedIn profile View MindStick Software's Facebook profile
Top Contributor
Advertisement
Advertise with Us
Mindstick
Article Article  Forum Forum  Blog Blog  Quiz Quiz  Beginner Beginner  Careers Careers  Contact Contact  Login Login  
Home | Product | Services | About Us | Interview | DeveloperSection | Submit an Article | Submit Blog
Report Abuse Form
Reason:    
 

Home >> C# >> Encrypting Records using RijndaelManaged method
Encrypting Records using RijndaelManaged method

In this blog I will told you that how to use RijndaelManaged method to encrypt records.
Views: 1104     Comments: 0
by Awadhendra Tiwari on 4/15/2012

Encrypting Records using RijndaelManaged method

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.

            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 this blog.

Title :  
Comment :
Text ColorBackground Color
BoldItalicUnderline
LeftCenterRightJustify
Ordered ListBulleted List
IndentOutdent
Horizontal Rule
SubscriptSuperscript
HyperlinkImage
Design ModeDesign
View HtmlHtml
       
 
Report Abuse Form
Reason:    
 
Latest ArticleRSS Feed
    
    
    
    
    
    
    
    
    
    
More...
Latest Blogs by Awadhendra TiwariRSS Feed
    
    
    
    
    
    
    
    
    
    
More...
Top Viewed ArticlesRSS Feed
    
    
    
    
    
    
    
    
    
    
Top Viewed BlogsRSS Feed
    
    
    
    
    
    
    
    
    
    
Latest Interview QuestionsRSS Feed
    
    
    
    
    
    
    
    
    
    
More...
Total Online Users: 6571
  
Copyright © 2013MindStick. All Rights Reserved.