Decrypt/Encrypt String AES : Encrypt Decrypt « Security « C# / C Sharp

Home
C# / C Sharp
1.2D Graphics
2.Class Interface
3.Collections Data Structure
4.Components
5.Data Types
6.Database ADO.net
7.Date Time
8.Design Patterns
9.Development Class
10.Event
11.File Stream
12.Generics
13.GUI Windows Form
14.Internationalization I18N
15.Language Basics
16.LINQ
17.Network
18.Office
19.Reflection
20.Regular Expressions
21.Security
22.Services Event
23.Thread
24.Web Services
25.Windows
26.Windows Presentation Foundation
27.XML
28.XML LINQ
C# Book
C# / C Sharp by API
C# / CSharp Tutorial
C# / CSharp Open Source
C# / C Sharp » Security » Encrypt DecryptScreenshots 
Decrypt/Encrypt String AES
    

using System;
using System.Text;
using System.Security.Cryptography;
using System.IO;

namespace OpusSuite.Utility
{
    public static class CryptoUtil
    {
       private static byte[] _salt = Encoding.ASCII.GetBytes("o6806642kbM7c5");      
   
        public static string EncryptStringAES(string plainText, string sharedSecret)     
        {         
            if(string.IsNullOrEmpty(plainText))             
                throw new ArgumentNullException("plainText");         
            if(string.IsNullOrEmpty(sharedSecret))             
                throw new ArgumentNullException("sharedSecret");          
            string outStr = null;                       // Encrypted string to return         
            RijndaelManaged aesAlg = null;              // RijndaelManaged object used to encrypt the data.
            try         
            {             
                // generate the key from the shared secret and the salt             
                var key = new Rfc2898DeriveBytes(sharedSecret, _salt);              
                // Create a RijndaelManaged object             
                // with the specified key and IV.             
                aesAlg = new RijndaelManaged();             
                aesAlg.Key = key.GetBytes(aesAlg.KeySize / 8);             
                aesAlg.IV = key.GetBytes(aesAlg.BlockSize / 8);              
                // Create a decrytor to perform the stream transform.             
                ICryptoTransform encryptor = aesAlg.CreateEncryptor(aesAlg.Key, aesAlg.IV);              
                // Create the streams used for encryption.             
                using (var msEncrypt = new MemoryStream())             
                {                 
                    using (var csEncrypt = new CryptoStream(msEncrypt, encryptor, CryptoStreamMode.Write))
                    {                     
                        using (var swEncrypt = new StreamWriter(csEncrypt))                     
                        {                          
                            //Write all data to the stream.                         
                            swEncrypt.Write(plainText);                     
                        }                 
                    }                 
                    outStr = Convert.ToBase64String(msEncrypt.ToArray());             
                }         
            }         
            finally         
            {             
                // Clear the RijndaelManaged object.             
                if (aesAlg != null)                 
                    aesAlg.Clear();         
            }          
            // Return the encrypted bytes from the memory stream.         
            return outStr;     
        }      
        
        public static string DecryptStringAES(string cipherText, string sharedSecret)     
        {         
            if (string.IsNullOrEmpty(cipherText))             
                throw new ArgumentNullException("cipherText");         
            if (string.IsNullOrEmpty(sharedSecret))             
                throw new ArgumentNullException("sharedSecret");          
            // Declare the RijndaelManaged object         
            // used to decrypt the data.         
            RijndaelManaged aesAlg = null;          
            // Declare the string used to hold         
            // the decrypted text.         
            string plaintext = null;          
            try         
            {             
                // generate the key from the shared secret and the salt             
                var key = new Rfc2898DeriveBytes(sharedSecret, _salt);              
                // Create a RijndaelManaged object             
                // with the specified key and IV.             
                aesAlg = new RijndaelManaged();             
                aesAlg.Key = key.GetBytes(aesAlg.KeySize / 8);
                aesAlg.IV = key.GetBytes(aesAlg.BlockSize / 8);              
                // Create a decrytor to perform the stream transform.             
                ICryptoTransform decryptor = aesAlg.CreateDecryptor(aesAlg.Key, aesAlg.IV);
                // Create the streams used for decryption.                             
                byte[] bytes = Convert.FromBase64String(cipherText);             
                using (var msDecrypt = new MemoryStream(bytes))             
                {                 
                    using (var csDecrypt = new CryptoStream(msDecrypt, decryptor, CryptoStreamMode.Read))                 
                    {                     
                        using (var srDecrypt = new StreamReader(csDecrypt))                          
                            // Read the decrypted bytes from the decrypting stream                         
                            // and place them in a string.                         
                            plaintext = srDecrypt.ReadToEnd();                 
                    }             
                }         
            }         
            finally         
            {             
                // Clear the RijndaelManaged object.             
                if (aesAlg != null)                 
                    aesAlg.Clear();         
            }          
            return plaintext;     
        
    
}

   
    
    
    
  
Related examples in the same category
1.Encrypt Utils
2.Decrypt Utils
3.Provides the Unix crypt() encryption algorithm.
4.Encrypts the value by password and salt.
5.Encrypt/Decrypt String To Bytes
6.Encrypt the given string using AES
7.Encrypt String
8.Encrypt and Decrypt String
9.Encrypt a string
10.Crypto Utility
11.Crypto Utilities
12.Encryption Helper
13.Key Creator
14.S3 Upload Policy
java2s.com  |  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.