CryptographyManager.cs :  » Database » NBearLite » System » C# / CSharp Open Source

Home
C# / CSharp Open Source
1.2.6.4 mono .net core
2.2.6.4 mono core
3.Aspect Oriented Frameworks
4.Bloggers
5.Build Systems
6.Business Application
7.Charting Reporting Tools
8.Chat Servers
9.Code Coverage Tools
10.Content Management Systems CMS
11.CRM ERP
12.Database
13.Development
14.Email
15.Forum
16.Game
17.GIS
18.GUI
19.IDEs
20.Installers Generators
21.Inversion of Control Dependency Injection
22.Issue Tracking
23.Logging Tools
24.Message
25.Mobile
26.Network Clients
27.Network Servers
28.Office
29.PDF
30.Persistence Frameworks
31.Portals
32.Profilers
33.Project Management
34.RSS RDF
35.Rule Engines
36.Script
37.Search Engines
38.Sound Audio
39.Source Control
40.SQL Clients
41.Template Engines
42.Testing
43.UML
44.Web Frameworks
45.Web Service
46.Web Testing
47.Wiki Engines
48.Windows Presentation Foundation
49.Workflows
50.XML Parsers
C# / C Sharp
C# / C Sharp by API
C# / CSharp Tutorial
C# / CSharp Open Source » Database » NBearLite 
NBearLite » System » CryptographyManager.cs
using System;
using System.Data;
using System.IO;
using System.Text;
using System.Configuration;
using System.Security;
using System.Security.Cryptography;

namespace System{
    /// <summary>
    /// Common CryptographyManager
    /// </summary>
    public class CryptographyManager
    {
        private readonly string _defaultLegalIV = "E4ghj*Ghg7!rNIfb&95GUY86GfghUb#er57HBh(u%g6HJ($jhWk7&!hg4ui%$hjk";

        #region Constructors

        /// <summary>
        /// Initializes a new instance of the <see cref="CryptographyManager"/> class.
        /// </summary>
        public CryptographyManager()
        {
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="CryptographyManager"/> class.
        /// </summary>
        /// <param name="legalIVKey">The legal IV key.</param>
        public CryptographyManager(string legalIVKey)
        {
            Check.Require(!string.IsNullOrEmpty(legalIVKey), "legalIVKey could not be null or empty.");

            _defaultLegalIV = legalIVKey;
        }

        #endregion

        #region Private Members

        /// <summary>
        /// The default encrypt key.
        /// </summary>
        public const string DEFAULT_KEY = "aslkjkljlsajsuaggasfklrjuisdhaie";

        private static byte[] GetLegalKey(SymmetricAlgorithm mobjCryptoService, string key)
        {
            string sTemp = key;
            mobjCryptoService.GenerateKey();
            byte[] bytTemp = mobjCryptoService.Key;
            int KeyLength = bytTemp.Length;
            if (sTemp.Length > KeyLength)
                sTemp = sTemp.Substring(0, KeyLength);
            else if (sTemp.Length < KeyLength)
                sTemp = sTemp.PadRight(KeyLength, ' ');
            return ASCIIEncoding.ASCII.GetBytes(sTemp);
        }

        private byte[] GetLegalIV(SymmetricAlgorithm mobjCryptoService)
        {
            string sTemp = _defaultLegalIV;
            mobjCryptoService.GenerateIV();
            byte[] bytTemp = mobjCryptoService.IV;
            int IVLength = bytTemp.Length;
            if (sTemp.Length > IVLength)
                sTemp = sTemp.Substring(0, IVLength);
            else if (sTemp.Length < IVLength)
                sTemp = sTemp.PadRight(IVLength, ' ');
            return ASCIIEncoding.ASCII.GetBytes(sTemp);
        }

        #endregion

        #region public Members

        /// <summary>
        /// Symmetrics encrpyt.
        /// </summary>
        /// <param name="str">The STR to encrpyt.</param>
        /// <param name="mobjCryptoService">A concrete symmetric algorithm.</param>
        /// <param name="key">The key.</param>
        /// <returns>The encrpyt str.</returns>
        public string SymmetricEncrpyt(string str, SymmetricAlgorithm mobjCryptoService, string key)
        {
            Check.Require(str != null, "str could not be null!");

            byte[] bytIn = UTF8Encoding.UTF8.GetBytes(str);
            return Convert.ToBase64String(SymmetricEncrpyt(bytIn, mobjCryptoService, key));
        }

        /// <summary>
        /// Symmetrics the encrpyt.
        /// </summary>
        /// <param name="buffer">The buffer.</param>
        /// <param name="mobjCryptoService">The mobj crypto service.</param>
        /// <param name="key">The key.</param>
        /// <returns></returns>
        public byte[] SymmetricEncrpyt(byte[] buffer, SymmetricAlgorithm mobjCryptoService, string key)
        {
            if (buffer == null || buffer.Length == 0)
            {
                return buffer;
            }

            Check.Require(mobjCryptoService != null, "mobjCryptoService could not be null!");

            MemoryStream ms = new MemoryStream();
            mobjCryptoService.Key = GetLegalKey(mobjCryptoService, key);
            mobjCryptoService.IV = GetLegalIV(mobjCryptoService);
            ICryptoTransform encrypto = mobjCryptoService.CreateEncryptor();
            CryptoStream cs = new CryptoStream(ms, encrypto, CryptoStreamMode.Write);
            cs.Write(buffer, 0, buffer.Length);
            cs.FlushFinalBlock();
            ms.Close();
            return ms.ToArray();
        }

        /// <summary>
        /// Symmetrics decrpyt.
        /// </summary>
        /// <param name="str">The STR to decrpyt.</param>
        /// <param name="mobjCryptoService">A concrete symmetric algorithm.</param>
        /// <param name="key">The key.</param>
        /// <returns>The decrpyted str.</returns>
        public string SymmetricDecrpyt(string str, SymmetricAlgorithm mobjCryptoService, string key)
        {
            Check.Require(str != null, "str could not be null!");

            byte[] bytIn = Convert.FromBase64String(str);
            return UTF8Encoding.Unicode.GetString(SymmetricDecrpyt(bytIn, mobjCryptoService, key));
        }

        /// <summary>
        /// Symmetrics the decrpyt.
        /// </summary>
        /// <param name="buffer">The buffer.</param>
        /// <param name="mobjCryptoService">The mobj crypto service.</param>
        /// <param name="key">The key.</param>
        /// <returns></returns>
        public byte[] SymmetricDecrpyt(byte[] buffer, SymmetricAlgorithm mobjCryptoService, string key)
        {
            if (buffer == null || buffer.Length == 0)
            {
                return buffer;
            }

            Check.Require(mobjCryptoService != null, "mobjCryptoService could not be null!");

            MemoryStream ms = new MemoryStream(buffer, 0, buffer.Length);
            MemoryStream msOut = new MemoryStream();
            mobjCryptoService.Key = GetLegalKey(mobjCryptoService, key);
            mobjCryptoService.IV = GetLegalIV(mobjCryptoService);
            ICryptoTransform encrypto = mobjCryptoService.CreateDecryptor();
            byte[] writeData = new byte[4096];
            using (CryptoStream cs = new CryptoStream(ms, encrypto, CryptoStreamMode.Read))
            {
                int n;
                while ((n = cs.Read(writeData, 0, writeData.Length)) > 0)
                {
                    msOut.Write(writeData, 0, n);
                }
            }
            return msOut.ToArray();
        }

        /// <summary>
        /// Computes the hash.
        /// </summary>
        /// <param name="str">The STR to compute hash value.</param>
        /// <returns>The hash value.</returns>
        public string ComputeHash(string str)
        {
            Check.Require(str != null, "Arguments error.", new ArgumentNullException("str"));

            byte[] bytIn = UTF8Encoding.UTF8.GetBytes(str);
            return Convert.ToBase64String(ComputeHash(bytIn));
        }

        /// <summary>
        /// Computes the hash.
        /// </summary>
        /// <param name="buffer">The buffer.</param>
        /// <returns></returns>
        public byte[] ComputeHash(byte[] buffer)
        {
            HashAlgorithm sha = new SHA1CryptoServiceProvider();
            return sha.ComputeHash(buffer);
        }

        #endregion
    }
}
www.java2v.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.