sexta-feira, 24 de julho de 2015

CryptoUtils

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

namespace Lapuinka.Security
{
    public class CryptoUtils
    {
        private static readonly byte[] SharedSalt = Encoding.ASCII.GetBytes("YOURSALT");

        public static string CreateSalt(int size)
        {
            var saltBytes = new byte[size];
            var rng = new RNGCryptoServiceProvider();
            rng.GetNonZeroBytes(saltBytes);
            return Convert.ToBase64String(saltBytes);
        }

        public static string EncodePassword(string password)
        {
            return EncodePassword(password, string.Empty);
        }

        public static string EncodePassword(string password, string salt)
        {
            byte[] buffer = Encoding.UTF8.GetBytes(password + salt);

            HashAlgorithm hash = new SHA512Managed();

            byte[] hashBytes = hash.ComputeHash(buffer);

            return Convert.ToBase64String(hashBytes);
        }

        public static string EncryptText(string plainText, string password)
        {
            using (var cipher = new RijndaelManaged())
            {
                using (var key = new Rfc2898DeriveBytes(password, SharedSalt))
                {
                    cipher.Key = key.GetBytes(cipher.KeySize / 8);
                    cipher.IV = key.GetBytes(cipher.BlockSize / 8);
                }

                using (var encryptor = cipher.CreateEncryptor(cipher.Key, cipher.IV))
                {
                    using (var memoryStream = new MemoryStream())
                    {
                        using (var cryptoStream = new CryptoStream(memoryStream, encryptor, CryptoStreamMode.Write))
                        {
                            using (var streamWriter = new StreamWriter(cryptoStream))
                            {
                                streamWriter.Write(plainText);
                            }
                        }

                        return Convert.ToBase64String(memoryStream.ToArray());
                    }
                }
            }
        }

        public static string DecryptText(string cipherText, string password)
        {
            var cipherData = Convert.FromBase64String(cipherText);

            using (var cipher = new RijndaelManaged())
            {
                using (var key = new Rfc2898DeriveBytes(password, SharedSalt))
                {
                    cipher.Key = key.GetBytes(cipher.KeySize / 8);
                    cipher.IV = key.GetBytes(cipher.BlockSize / 8);
                }

                using (var decryptor = cipher.CreateDecryptor(cipher.Key, cipher.IV))
                {
                    using (var memoryStream = new MemoryStream(cipherData))
                    {
                        using (var cryptoStream = new CryptoStream(memoryStream, decryptor, CryptoStreamMode.Read))
                        {
                            using (var streamReader = new StreamReader(cryptoStream, Encoding.UTF8))
                            {
                                return streamReader.ReadToEnd();
                            }
                        }
                    }
                }
            }
        }

        public static byte[] Encrypt(byte[] plainData, string password)
        {
            using (var cipher = new RijndaelManaged())
            {
                using (var key = new Rfc2898DeriveBytes(password, SharedSalt))
                {
                    cipher.Key = key.GetBytes(cipher.KeySize/8);
                    cipher.IV  = key.GetBytes(cipher.BlockSize/8);
                }

                using (var encryptor = cipher.CreateEncryptor(cipher.Key, cipher.IV))
                {
                    using (var memoryStream = new MemoryStream())
                    {
                        using (var cryptoStream = new CryptoStream(memoryStream, encryptor, CryptoStreamMode.Write))
                        {
                            cryptoStream.Write(plainData, 0, plainData.Length);
                        }
                       
                        return memoryStream.ToArray();
                    }
                }
            }
        }

        public static byte[] Decrypt(byte[] cipherData, string password)
        {
            using (var cipher = new RijndaelManaged())
            {
                using (var key = new Rfc2898DeriveBytes(password, SharedSalt))
                {
                    cipher.Key = key.GetBytes(cipher.KeySize/8);
                    cipher.IV = key.GetBytes(cipher.BlockSize/8);
                }

                using (var decryptor = cipher.CreateDecryptor(cipher.Key, cipher.IV))
                {
                    using (var memoryStream = new MemoryStream(cipherData))
                    {
                        using (var cryptoStream = new CryptoStream(memoryStream, decryptor, CryptoStreamMode.Read))
                        {
                            var output = new byte[cipherData.Length];
                            cryptoStream.Read(output, 0, output.Length);
                            return output;
                        }
                    }
                }
            }
        }
    }
}


Postar um comentário