Exemplo n.º 1
0
        ProtectedKey ProtectKey(byte[] decryptedKey, DataProtectionScope protectionScope)
        {
            ProtectedKey key = ProtectedKey.CreateFromPlaintextKey(decryptedKey, protectionScope);

            CryptographyUtility.ZeroOutBytes(decryptedKey);
            return(key);
        }
Exemplo n.º 2
0
        internal static RijndaelManaged GetAESCryptoProvider(string password)
        {
            RijndaelManaged managed = new RijndaelManaged();

            byte[] bytes = Encoding.UTF8.GetBytes(password);
            managed.KeySize = 0x100;
            using (SHA256 sha = new SHA256Managed())
            {
                managed.Key = sha.ComputeHash(bytes);
            }
            using (SHA1 sha2 = new SHA1Managed())
            {
                byte[] buffer2 = sha2.ComputeHash(bytes);
                byte[] buffer3 = new byte[0x10];
                for (int i = 0; i < 0x10; i++)
                {
                    buffer3[i] = buffer2[i];
                }
                managed.IV = buffer3;
                CryptographyUtility.ZeroOutBytes(buffer3);
                CryptographyUtility.ZeroOutBytes(buffer2);
            }
            CryptographyUtility.ZeroOutBytes(bytes);
            return(managed);
        }
Exemplo n.º 3
0
 internal static string CreateHash(IHashProvider provider, string plaintext)
 {
     byte[] bytes   = Encoding.Unicode.GetBytes(plaintext);
     byte[] inArray = provider.CreateHash(bytes);
     CryptographyUtility.GetRandomBytes(bytes);
     return(Convert.ToBase64String(inArray));
 }
Exemplo n.º 4
0
        byte[] EncryptKeyForArchival(byte[] keyToExport, string passphrase, byte[] salt)
        {
            RijndaelManaged archivalEncryptionAlgorithm = new RijndaelManaged();

            byte[] rgbKey = this.GenerateArchivalKey(archivalEncryptionAlgorithm, passphrase, salt);
            byte[] rgbIV  = new byte[archivalEncryptionAlgorithm.BlockSize / 8];
            return(CryptographyUtility.Transform(archivalEncryptionAlgorithm.CreateEncryptor(rgbKey, rgbIV), keyToExport));
        }
Exemplo n.º 5
0
        internal static bool CompareHash(IHashProvider provider, string plaintext, string hashedText)
        {
            byte[] bytes      = Encoding.Unicode.GetBytes(plaintext);
            byte[] hashedtext = Convert.FromBase64String(hashedText);
            bool   flag       = provider.CompareHash(bytes, hashedtext);

            CryptographyUtility.GetRandomBytes(bytes);
            return(flag);
        }
Exemplo n.º 6
0
        public static string EncryptWithPassword(string plaintext, string password)
        {
            byte[] bytes   = Encoding.UTF8.GetBytes(plaintext);
            byte[] inArray = EncryptWithPassword(bytes, password);
            string str     = Convert.ToBase64String(inArray);

            CryptographyUtility.ZeroOutBytes(bytes);
            CryptographyUtility.ZeroOutBytes(inArray);
            return(str);
        }
Exemplo n.º 7
0
        public static string DecryptWithPassword(string ciphertextBase64, string password)
        {
            byte[] ciphertext = Convert.FromBase64String(ciphertextBase64);
            byte[] bytes      = DecryptWithPassword(ciphertext, password);
            string str        = Encoding.UTF8.GetString(bytes);

            CryptographyUtility.ZeroOutBytes(bytes);
            CryptographyUtility.ZeroOutBytes(ciphertext);
            return(str);
        }
Exemplo n.º 8
0
        byte[] DecryptKeyForRestore(string passphrase, byte[] encryptedKey, byte[] salt)
        {
            RijndaelManaged archivalEncryptionAlgorithm = new RijndaelManaged();

            byte[] rgbKey  = this.GenerateArchivalKey(archivalEncryptionAlgorithm, passphrase, salt);
            byte[] rgbIV   = new byte[archivalEncryptionAlgorithm.BlockSize / 8];
            byte[] buffer3 = CryptographyUtility.Transform(archivalEncryptionAlgorithm.CreateDecryptor(rgbKey, rgbIV), encryptedKey);
            CryptographyUtility.ZeroOutBytes(rgbKey);
            return(buffer3);
        }
Exemplo n.º 9
0
 void AddSaltToPlainText(ref byte[] salt, ref byte[] plaintext)
 {
     if (this.saltEnabled)
     {
         if (salt == null)
         {
             salt = CryptographyUtility.GetRandomBytes(0x10);
         }
         plaintext = CryptographyUtility.CombineBytes(salt, plaintext);
     }
 }
Exemplo n.º 10
0
 internal static string EncryptSymmetric(ISymmetricCryptoProvider provider, string plaintext)
 {
     if (string.IsNullOrEmpty(plaintext))
     {
         throw new ArgumentException("The value can not be a null or empty string.", "plaintext");
     }
     byte[] bytes   = Encoding.Unicode.GetBytes(plaintext);
     byte[] inArray = provider.Encrypt(bytes);
     CryptographyUtility.GetRandomBytes(bytes);
     return(Convert.ToBase64String(inArray));
 }
Exemplo n.º 11
0
 public byte[] Decrypt(byte[] encryptedText)
 {
     byte[] buffer  = null;
     byte[] buffer2 = this.ExtractIV(encryptedText);
     this.algorithm.Key = this.Key;
     using (ICryptoTransform transform = this.algorithm.CreateDecryptor())
     {
         buffer = Transform(transform, buffer2);
     }
     CryptographyUtility.ZeroOutBytes(this.algorithm.Key);
     return(buffer);
 }
Exemplo n.º 12
0
        internal static string DecryptSymmetric(ISymmetricCryptoProvider provider, string ciphertextBase64)
        {
            if (string.IsNullOrEmpty(ciphertextBase64))
            {
                throw new ArgumentException("The value can not be a null or empty string.", "ciphertextBase64");
            }
            byte[] ciphertext = Convert.FromBase64String(ciphertextBase64);
            byte[] bytes      = provider.Decrypt(ciphertext);
            string str        = Encoding.Unicode.GetString(bytes);

            CryptographyUtility.GetRandomBytes(bytes);
            return(str);
        }
Exemplo n.º 13
0
 byte[] GetEncryptedKey(ProtectedKey keyToBeArchived, string passphrase, byte[] salt)
 {
     byte[] buffer2;
     byte[] decryptedKey = keyToBeArchived.DecryptedKey;
     try
     {
         buffer2 = this.EncryptKeyForArchival(decryptedKey, passphrase, salt);
     }
     finally
     {
         CryptographyUtility.ZeroOutBytes(decryptedKey);
     }
     return(buffer2);
 }
Exemplo n.º 14
0
 public byte[] Encrypt(byte[] plaintext)
 {
     byte[] dst = null;
     byte[] src = null;
     this.algorithm.Key = this.Key;
     using (ICryptoTransform transform = this.algorithm.CreateEncryptor())
     {
         src = Transform(transform, plaintext);
     }
     dst = new byte[this.IVLength + src.Length];
     Buffer.BlockCopy(this.algorithm.IV, 0, dst, 0, this.IVLength);
     Buffer.BlockCopy(src, 0, dst, this.IVLength, src.Length);
     CryptographyUtility.ZeroOutBytes(this.algorithm.Key);
     return(dst);
 }
Exemplo n.º 15
0
        ProtectedKey GenerateKey(SymmetricAlgorithm algorithm, DataProtectionScope dataProtectionScope)
        {
            ProtectedKey key;

            byte[] plaintextKey = this.GenerateUnprotectedKey(algorithm);
            try
            {
                key = ProtectedKey.CreateFromPlaintextKey(plaintextKey, dataProtectionScope);
            }
            finally
            {
                if (plaintextKey != null)
                {
                    CryptographyUtility.ZeroOutBytes(plaintextKey);
                }
            }
            return(key);
        }
Exemplo n.º 16
0
 byte[] GenerateSalt()
 {
     return(CryptographyUtility.GetRandomBytes(0x10));
 }
Exemplo n.º 17
0
 static byte[] Transform(ICryptoTransform transform, byte[] buffer)
 {
     return(CryptographyUtility.Transform(transform, buffer));
 }