Encrypt a string : 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# / C Sharp » Security » Encrypt DecryptScreenshots 
Encrypt a string
    
#region (C1998-2009 AUTUMOON LAB.
/* *************************************************
* Solution:    Autumoon Code Library - Team Edition.
 
 * Project:     Common Foundation.
 
 * Description: The common tool class.
 
 * Author:      ZeroCool.
 
 * Created:     03/02/2008
 
 * (C) 1998-2009 Autumoon Lab.
 * ************************************************/
#endregion

#region ALL REFERENCE
using System;
using System.Security.Cryptography;
using System.IO;
using System.Text;
#endregion

namespace Autumoon.CodeLibrary.CommonFoundation
{
    public class Utilities
    {
        // Properties and fields.
        #region Properties and fields.
        private static byte[] _bytes = ASCIIEncoding.ASCII.GetBytes("ACLT2008");
        #endregion

        // Constructors.
        #region Constructors
        #endregion

        // Public methods.
        #region Public Methods
        /// <summary>
        /// Encrypt a string.
        /// </summary>
        /// <param name="originalString">The original string.</param>
        /// <returns>The encrypted string.</returns>
        /// <exception cref="ArgumentNullException">This exception will be thrown when the original string is null or empty.</exception>
        public static string Encrypt(string originalString)
        {
            if (String.IsNullOrEmpty(originalString))
            {
                throw new ArgumentNullException("The string which needs to be encrypted can not be null.");
            }

            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 a crypted string.
        /// </summary>
        /// <param name="cryptedString">The crypted string.</param>
        /// <returns>The decrypted string.</returns>
        /// <exception cref="ArgumentNullException">This exception will be thrown when the crypted string is null or empty.</exception>
        public static 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();
        }

        /// <summary>
        /// Handle SQL sensitive charactors in original text.
        /// </summary>
        /// <param name="originalText">The orginal code content.</param>
        /// <returns>The SQL storable text.</returns>
        public static string HandleSQLSensitiveChars(string originalText)
        {
            if (String.IsNullOrEmpty(originalText))
            {
                return String.Empty;
            }

            return originalText.Replace("<""&lt;").Replace(">""&gt;").Replace("\"""&quot;").Replace(" ""&nbsp;").Replace("©""&copy;").Replace("®""&reg;").Replace("&""&amp;").Replace("\'""&apos;");
        }

        /// <summary>
        /// Handle the SQL stored text for resuming SQL sensitive charactors.
        /// </summary>
        /// <param name="storableText">The SQL stored text.</param>
        /// <returns>The orginal code text.</returns>
        public static string ResumeSQLStoredText(string storableText)
        {
            if (String.IsNullOrEmpty(storableText))
            {
                return String.Empty;
            }

            return storableText.Replace("&lt;""<").Replace("&gt;"">").Replace("&quot;""\"").Replace("&nbsp;"" ").Replace("&copy;""©").Replace("&reg;""®").Replace("&amp;""&").Replace("&apos;""\'");
        }
        #endregion
    }
}

   
    
    
    
  
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.Decrypt/Encrypt String AES
8.Encrypt String
9.Encrypt and Decrypt 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.