Key Creator : 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 
Key Creator
        
using System.Security.Cryptography;
using System.Text;

namespace NHibernateProvider.Util
{
    /// <summary>
    /// Helper class to generate a machine key for use in the application's config file to override the default key used by the provider.
    /// </summary>
    internal static class KeyCreator
    {
        #region Main for Testing
        //public static void main(string[] args)
        //{
        //    string decryptionKey = CreateKey(Convert.ToInt32(args[1]));
        //    string validationKey = CreateKey(Convert.ToInt32(args[2]));
        //    Console.WriteLine("<machinekey validationkey=\"{0}\" decryptionkey=\"{1}\" validation=\"sha1\"/>", validationKey, decryptionKey);
        //}
        #endregion Main for Testing

        #region Operations
        /// <summary>
        /// Creates a key based on the indicated size.
        /// </summary>
        /// <param name="numBytes">size of the key.</param>
        /// <returns>Generated key of the specified length.</returns>
        public static string CreateKey(int numBytes)
        {
            RNGCryptoServiceProvider rng = new RNGCryptoServiceProvider();
            byte[] buff = new byte[numBytes];
            rng.GetBytes(buff);
            return BytesToHexString(buff);
        }
        /// <summary>
        /// Converts the given byte array to a hex string representation.
        /// </summary>
        /// <param name="bytes">the data to convert.</param>
        /// <returns>Hexadecimal string representation of the given byte array./</returns>
        public static string BytesToHexString(byte[] bytes)
        {
            StringBuilder hexString = new StringBuilder(64);
            for (int counter = 0; counter < bytes.Length; counter++)
            {
                hexString.Append(string.Format("{0:X2}", bytes[counter]));
            }
            return hexString.ToString();
        }
        #endregion Operations
    }
}

   
    
    
    
    
    
    
    
  
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.Encrypt a string
11.Crypto Utility
12.Crypto Utilities
13.Encryption Helper
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.