Crypto Utilities : Encrypt Decrypt « Security « C# / C Sharp






Crypto Utilities

        
using System;
using System.Collections.Generic;
using System.Text;
using System.Security;
using System.Security.Cryptography;
using System.Runtime.InteropServices;
using System.IO;
using System.Text;

namespace UltraRDC
{
    public class CryptoUtilities
    {
        private CryptoUtilities()
        {
        }

        public static byte[] Decrypt(SecureString key, byte[] data)
        {
            Rijndael encryption = Rijndael.Create();
            encryption.KeySize = 256;

            IntPtr marshalledKeyBytes = Marshal.SecureStringToGlobalAllocAnsi(key);
            byte[] keyBytes = new byte[encryption.KeySize / 8];
            byte[] decryptedData = new byte[data.Length];

            Marshal.Copy(marshalledKeyBytes, keyBytes, 0, Math.Min(keyBytes.Length, key.Length));

            encryption.Key = keyBytes;
            encryption.IV = Convert.FromBase64String("QGWyKbe+W9H0mL2igm73jw==");

            MemoryStream memoryStream = new MemoryStream(data, 0, data.Length);
            CryptoStream cryptoStream = new CryptoStream(memoryStream, encryption.CreateDecryptor(), CryptoStreamMode.Read);

            cryptoStream.Read(decryptedData, 0, decryptedData.Length);
            cryptoStream.Close();
            memoryStream.Close();

            for (int i = 0; i < keyBytes.Length; i++)
                keyBytes[i] = 0;

            Marshal.ZeroFreeGlobalAllocAnsi(marshalledKeyBytes);

            return decryptedData;
        }

        public static byte[] Encrypt(SecureString key, byte[] data)
        {
            Rijndael encryption = Rijndael.Create();
            encryption.KeySize = 256;

            IntPtr marshalledKeyBytes = Marshal.SecureStringToGlobalAllocAnsi(key);
            byte[] keyBytes = new byte[encryption.KeySize / 8];

            Marshal.Copy(marshalledKeyBytes, keyBytes, 0, Math.Min(keyBytes.Length, key.Length));

            encryption.Key = keyBytes;
            encryption.IV = Convert.FromBase64String("QGWyKbe+W9H0mL2igm73jw==");

            MemoryStream memoryStream = new MemoryStream();
            CryptoStream cryptoStream = new CryptoStream(memoryStream, encryption.CreateEncryptor(), CryptoStreamMode.Write);

            cryptoStream.Write(data, 0, data.Length);
            cryptoStream.Close();
            memoryStream.Close();

            for (int i = 0; i < keyBytes.Length; i++)
                keyBytes[i] = 0;

            Marshal.ZeroFreeGlobalAllocAnsi(marshalledKeyBytes);

            return memoryStream.ToArray();
        }

        public static byte[] Encrypt(SecureString key, string data)
        {
            return Encrypt(key, Encoding.ASCII.GetBytes(data));
        }

        public static byte[] Encrypt(SecureString key, SecureString data)
        {
            IntPtr marshalledDataBytes = Marshal.SecureStringToGlobalAllocAnsi(data);
            byte[] dataBytes = new byte[data.Length];

            Marshal.Copy(marshalledDataBytes, dataBytes, 0, dataBytes.Length);

            byte[] encryptedData = Encrypt(key, dataBytes);

            for (int i = 0; i < dataBytes.Length; i++)
                dataBytes[i] = 0;

            Marshal.ZeroFreeGlobalAllocAnsi(marshalledDataBytes);

            return encryptedData;
        }
    }
}

   
    
    
    
    
    
    
    
  








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.Encryption Helper
13.Key Creator
14.S3 Upload Policy