Example #1
0
 /// <summary>
 /// Decrypts the specified data.
 /// </summary>
 /// <param name="data">The data.</param>
 /// <param name="key">The key.</param>
 /// <param name="salt">The salt.</param>
 /// <param name="hashingAlgorithm">The hashing algorithm.</param>
 /// <param name="passwordIterations">The password iterations.</param>
 /// <param name="initialVector">The initial vector with a length of 16 bytes.</param>
 /// <param name="keySize">Size of the key. (64, 128, 192, 256, etc.)</param>
 /// <returns>The decrypted data.</returns>
 public static byte[] Decrypt(this byte[] data,
                              byte[] key,
                              byte[] salt,
                              HashingAlgorithms hashingAlgorithm,
                              int passwordIterations,
                              byte[] initialVector,
                              int keySize,
                              SymmetricAlgorithms algorithm)
 {
     if (data is null || string.IsNullOrEmpty(algorithm) || key is null)
     {
         return(Array.Empty <byte>());
     }
     initialVector ??= Array.Empty <byte>();
     salt ??= Array.Empty <byte>();
     return(Canister.Builder.Bootstrapper?.Resolve <CryptoManager>()?.Decrypt(
                data,
                key,
                salt,
                hashingAlgorithm,
                passwordIterations,
                initialVector,
                keySize,
                algorithm) ?? Array.Empty <byte>());
 }
Example #2
0
        public static byte[] DecryptData(SymmetricAlgorithms symmetricAlgorithm, byte[] inputBytes, byte[] key)
        {
            byte[] decrypted;

            using (SymmetricAlgorithm algorithm = GetSymmetricAlgorithm(symmetricAlgorithm))
            {
                byte[] salt          = new byte[PBKDF2_SaltSizeBytes];
                byte[] iv            = new byte[algorithm.IV.Length];
                byte[] encryptedData = new byte[inputBytes.Length - salt.Length - iv.Length];

                int maxKeySize = GetLegalKeySizes(algorithm).Max();
                Buffer.BlockCopy(inputBytes, 0, salt, 0, salt.Length);
                Buffer.BlockCopy(inputBytes, salt.Length, iv, 0, iv.Length);
                Buffer.BlockCopy(inputBytes, salt.Length + iv.Length, encryptedData, 0, encryptedData.Length);

                algorithm.IV = iv;
                using (Rfc2898DeriveBytes pbkdf2 = new Rfc2898DeriveBytes(key, salt, PBKDF2_Iterations))
                {
                    algorithm.Key = pbkdf2.GetBytes(maxKeySize);
                }

                using (ICryptoTransform cryptoTransform = algorithm.CreateDecryptor())
                {
                    using (MemoryStream encryptedStream = new MemoryStream(encryptedData))
                    {
                        using (CryptoStream cryptoStream = new CryptoStream(encryptedStream, cryptoTransform, CryptoStreamMode.Read))
                        {
                            decrypted = cryptoStream.ReadToEnd();
                        }
                    }
                }
            }

            return(decrypted);
        }
        private static SymmetricAlgorithm GetSymmetricAlgorithm(SymmetricAlgorithms symmetricAlgorithm)
        {
            switch (symmetricAlgorithm)
            {
            case SymmetricAlgorithms.AES:
                return(new AesCryptoServiceProvider());    // kept for backwords compat

            case SymmetricAlgorithms.AESGCM:
                throw new InvalidOperationException(Resources.UnsupportedSymmetricAlgorithmException);     //it's implemented separately.

            case SymmetricAlgorithms.DES:
                return(new DESCryptoServiceProvider());

            case SymmetricAlgorithms.RC2:
                return(new RC2CryptoServiceProvider());

            case SymmetricAlgorithms.Rijndael:
                return(new RijndaelManaged());

            case SymmetricAlgorithms.TripleDES:
                return(new TripleDESCryptoServiceProvider());    // TODO: Use TripleDESCng after upgrading to .NET Framework 4.6.2

            default:
                throw new InvalidOperationException(Resources.UnsupportedSymmetricAlgorithmException);
            }
        }
        public void DecryptShouldReturnOriginalPlainText()
        {
            var aesEncryption = new SymmetricAlgorithms();
            var decrypted     = aesEncryption.Decrypt(CipherText, IV);

            Assert.That(PlainText, Is.EqualTo(decrypted));
        }
        /// <summary>
        /// Encrypts a string of data and returns the data
        /// </summary>
        /// <param name="Data">The Data to encrypt</param>
        /// <param name="Key">The Key or Password used to encrypt the Data</param>
        /// <param name="Salt">The Data used to hash the Encrypted Data</param>
        /// <param name="Algorithm">A symmetric algorithm used to encrypt the Data</param>
        /// <param name="Base64Encode">True to return the encrypted data as a Base64 Encoded String (for use in Xml files)</param>
        /// <returns>An encrypted String of the Data</returns>
        public static string Encrypt(string Data, byte[] Key, byte[] Salt, SymmetricAlgorithms Algorithm, bool Base64Encode)
        {
            // Determine the Algorithm
            SymmetricAlgorithm symAlgorithm = null;

            switch (Algorithm)
            {
            case SymmetricAlgorithms.DES:
                symAlgorithm = new DESCryptoServiceProvider();
                break;

            case SymmetricAlgorithms.RC2:
                symAlgorithm = new RC2CryptoServiceProvider();
                break;

            case SymmetricAlgorithms.Rijndael:
                symAlgorithm = new RijndaelManaged();
                break;

            case SymmetricAlgorithms.TripleDES:
                symAlgorithm = new TripleDESCryptoServiceProvider();
                break;
            }
            if (symAlgorithm == null)
            {
                return(Data);
            }

            try
            {
                symAlgorithm.IV   = Salt;
                symAlgorithm.Key  = Key;
                symAlgorithm.Mode = CipherMode.CBC;

                byte[] cryptData = Cryptographer.Encrypt(Data, symAlgorithm);
                if (Base64Encode)
                {
                    return(Convert.ToBase64String(cryptData));
                }
                else
                {
                    return(System.Text.Encoding.UTF8.GetString(cryptData));
                }
            }
            catch
            {
                return(Data);
            }
            finally
            {
                if (symAlgorithm != null)
                {
                    symAlgorithm.Clear();
                    symAlgorithm = null;
                }
            }
        }
        /// <summary>
        ///
        /// </summary>
        /// <param name="Data">The Data to decrypt</param>
        /// <param name="Key">The Key or Password used to decrypt the Data</param>
        /// /// <param name="Salt"></param>
        /// <param name="Algorithm">A Symmetric Algorithm used to decrypt the Data</param>
        /// <param name="Base64Encoded">True if the Data is Base64 Encoded</param>
        /// <returns>A decrypted String of the Data</returns>
        public static string Decrypt(string Data, byte[] Key, byte[] Salt, SymmetricAlgorithms Algorithm, bool Base64Encoded)
        {
            // Determine the Algorithm
            SymmetricAlgorithm symAlgorithm = null;

            switch (Algorithm)
            {
            case SymmetricAlgorithms.DES:
                symAlgorithm = new DESCryptoServiceProvider();
                break;

            case SymmetricAlgorithms.RC2:
                symAlgorithm = new RC2CryptoServiceProvider();
                break;

            case SymmetricAlgorithms.Rijndael:
                symAlgorithm = new RijndaelManaged();
                break;

            case SymmetricAlgorithms.TripleDES:
                symAlgorithm = new TripleDESCryptoServiceProvider();
                break;
            }
            if (symAlgorithm == null)
            {
                return(string.Empty);
            }

            try
            {
                symAlgorithm.IV  = Salt;
                symAlgorithm.Key = Key;
                byte[] cryptData;
                if (Base64Encoded)
                {
                    cryptData = Convert.FromBase64String(Data);
                }
                else
                {
                    cryptData = System.Text.Encoding.UTF8.GetBytes(Data);
                }
                return(Cryptographer.Decrypt(cryptData, symAlgorithm));
            }
            catch
            {
                return(string.Empty);
            }
            finally
            {
                if (symAlgorithm != null)
                {
                    symAlgorithm.Clear();
                    symAlgorithm = null;
                }
            }
        }
Example #7
0
 /// <summary>
 /// Encrypts the specified data.
 /// </summary>
 /// <param name="data">The data.</param>
 /// <param name="key">The key.</param>
 /// <param name="initialVector">The initial vector with a length of 16 bytes.</param>
 /// <param name="keySize">Size of the key. (64, 128, 192, 256, etc.)</param>
 /// <param name="algorithm">The algorithm.</param>
 /// <param name="encoding">The encoding of the string (defaults to UTF8).</param>
 /// <returns>The encrypted data.</returns>
 public static byte[] Encrypt(this string data,
                              PasswordDeriveBytes key,
                              byte[] initialVector,
                              int keySize,
                              SymmetricAlgorithms algorithm,
                              Encoding?encoding = null)
 {
     return(data.ToByteArray(encoding)
            .Encrypt(key, initialVector, keySize, algorithm));
 }
Example #8
0
        /// <summary>
        /// Creates a key.
        /// </summary>
        /// <param name="algorithm">The algorithm.</param>
        /// <returns>The key</returns>
        public byte[] CreateRandomKey(SymmetricAlgorithms algorithm)
        {
            var SymmetricAlgorithm = Symmetrics.FirstOrDefault(x => string.Equals(x.Name, algorithm.ToString(), StringComparison.OrdinalIgnoreCase));

            if (SymmetricAlgorithm == null)
            {
                return(Array.Empty <byte>());
            }
            return(SymmetricAlgorithm.CreateKey());
        }
 //---------------------------------------------------------------------
 public static string DecryptString(SymmetricAlgorithms SymmetricAlgorithm_in, string String_in, string Password_in)
 {
     return(System.Text.UTF8Encoding.UTF8.GetString
            (
                DecryptBytes_
                (
                    Create_SymmetricAlgorithm(SymmetricAlgorithm_in, Password_in)
                    , Convert.FromBase64String(String_in)
                )
            ));
 }
 //---------------------------------------------------------------------
 public static string EncryptString(SymmetricAlgorithms SymmetricAlgorithm_in, string String_in, string Password_in)
 {
     return(Convert.ToBase64String
            (
                EncryptBytes_
                (
                    Create_SymmetricAlgorithm(SymmetricAlgorithm_in, Password_in)
                    , System.Text.UTF8Encoding.UTF8.GetBytes(String_in.ToCharArray())
                )
            ));
 }
        public static bool IsFipsCompliant(SymmetricAlgorithms symmetricAlgorithm)
        {
            switch (symmetricAlgorithm)
            {
            case SymmetricAlgorithms.RC2:
            case SymmetricAlgorithms.Rijndael:
                return(false);

            default:
                return(true);
            }
        }
        public static byte[] GenerateIV(SymmetricAlgorithms Algorithm)
        {
            switch (Algorithm)
            {
            case SymmetricAlgorithms.Rijndael:
                // Rijndael needs a 16, 24, or 32 byte Key
                return(new byte[] { 0xF, 0x6F, 0x13, 0x2E, 0x35, 0xC2, 0xCD, 0xF9, 0x5, 0x46, 0x9C, 0xEA, 0xA8, 0x4B, 0x73, 0xCC });

            default:
                return(new byte[] { 0xF, 0x6F, 0x13, 0x2E, 0x35, 0xC2, 0xCD, 0xF9 });
            }
        }
Example #13
0
 /// <summary>
 /// Encrypts the specified data.
 /// </summary>
 /// <param name="data">The data.</param>
 /// <param name="key">The key.</param>
 /// <param name="salt">The salt.</param>
 /// <param name="hashingAlgorithm">The hashing algorithm.</param>
 /// <param name="passwordIterations">The password iterations.</param>
 /// <param name="initialVector">The initial vector with a length of 16 bytes.</param>
 /// <param name="keySize">Size of the key. (64, 128, 192, 256, etc.)</param>
 /// <param name="algorithm">The algorithm.</param>
 /// <param name="encoding">The encoding of the string (defaults to UTF8).</param>
 /// <returns>The encrypted data.</returns>
 public static byte[] Encrypt(this string data,
                              byte[] key,
                              byte[] salt,
                              HashingAlgorithms hashingAlgorithm,
                              int passwordIterations,
                              byte[] initialVector,
                              int keySize,
                              SymmetricAlgorithms algorithm,
                              Encoding?encoding = null)
 {
     return(data.ToByteArray(encoding)
            .Encrypt(key, salt, hashingAlgorithm, passwordIterations, initialVector, keySize, algorithm));
 }
Example #14
0
        public void Encrypt(SymmetricAlgorithms algorithms, int keySize)
        {
            var Key = TestObject2.CreateRandomKey(algorithms);
            var IV  = TestObject2.CreateRandomInitialVector(algorithms);

            Assert.NotNull(TestObject2.Encrypt(
                               new byte[] { 0, 1, 2, 3, 4, 5 },
                               (byte[])Key.Clone(),
                               "Salt".ToByteArray(),
                               HashingAlgorithms.SHA512,
                               2,
                               IV,
                               keySize,
                               algorithms));
        }
Example #15
0
        /// <summary>
        /// Encrypts the specified data.
        /// </summary>
        /// <param name="data">The data.</param>
        /// <param name="key">The key.</param>
        /// <param name="initialVector">The initial vector. 16 ASCII characters long.</param>
        /// <param name="keySize">
        /// Size of the key. Can be 64 (DES only), 128 (AES), 192 (AES and Triple DES), or 256 (AES)
        /// </param>
        /// <param name="algorithm">The algorithm.</param>
        /// <returns>The encrypted data.</returns>
        public byte[] Encrypt(
            byte[] data,
            PasswordDeriveBytes key,
            byte[] initialVector,
            int keySize,
            SymmetricAlgorithms algorithm)
        {
            var SymmetricAlgorithm = Symmetrics.FirstOrDefault(x => string.Equals(x.Name, algorithm.ToString(), StringComparison.OrdinalIgnoreCase));

            if (SymmetricAlgorithm == null)
            {
                return(Array.Empty <byte>());
            }
            return(SymmetricAlgorithm.Encrypt(data, key, initialVector, keySize));
        }
 /// <remarks>
 /// Creates a new instance of the symmetric crypto service for the specified algorithm.
 /// </remarks>
 public SymmetricServices(SymmetricAlgorithms CryptographyAlgorithm)
 {
     // set the internal cryptographic service based on the selected algorithm.
     switch (CryptographyAlgorithm)
     {
         case SymmetricAlgorithms.DES:
             _cryptoservice = new System.Security.Cryptography.DESCryptoServiceProvider();
             break;
         case SymmetricAlgorithms.RC2:
             _cryptoservice = new System.Security.Cryptography.RC2CryptoServiceProvider();
             break;
         case SymmetricAlgorithms.Rijndael:
             _cryptoservice = new System.Security.Cryptography.RijndaelManaged();
             break;
     }
 }
Example #17
0
 /// <summary>
 /// Decrypts the specified data.
 /// </summary>
 /// <param name="data">The data.</param>
 /// <param name="key">The key.</param>
 /// <param name="initialVector">The initial vector with a length of 16 bytes.</param>
 /// <param name="keySize">Size of the key. (64, 128, 192, 256, etc.)</param>
 /// <param name="algorithm">The algorithm.</param>
 /// <returns>The decrypted data.</returns>
 public static byte[] Decrypt(this byte[] data,
                              PasswordDeriveBytes key,
                              byte[] initialVector,
                              int keySize,
                              SymmetricAlgorithms algorithm)
 {
     if (data is null || string.IsNullOrEmpty(algorithm) || key is null)
     {
         return(Array.Empty <byte>());
     }
     initialVector ??= Array.Empty <byte>();
     return(Canister.Builder.Bootstrapper?.Resolve <CryptoManager>()?.Decrypt(
                data,
                key,
                initialVector,
                keySize,
                algorithm) ?? Array.Empty <byte>());
 }
        /// <remarks>
        /// Creates a new instance of the symmetric crypto service for the specified algorithm.
        /// </remarks>
        public SymmetricServices(SymmetricAlgorithms CryptographyAlgorithm)
        {
            // set the internal cryptographic service based on the selected algorithm.
            switch (CryptographyAlgorithm)
            {
            case SymmetricAlgorithms.DES:
                _cryptoservice = new System.Security.Cryptography.DESCryptoServiceProvider();
                break;

            case SymmetricAlgorithms.RC2:
                _cryptoservice = new System.Security.Cryptography.RC2CryptoServiceProvider();
                break;

            case SymmetricAlgorithms.Rijndael:
                _cryptoservice = new System.Security.Cryptography.RijndaelManaged();
                break;
            }
        }
        public static byte[] EncryptData(SymmetricAlgorithms symmetricAlgorithm, byte[] inputBytes, byte[] key)
        {
            byte[] result;

            if (symmetricAlgorithm == SymmetricAlgorithms.AESGCM)
            {
                return(EncryptAesGcm(inputBytes, key));
            }
            else
            {
                using (SymmetricAlgorithm algorithm = GetSymmetricAlgorithm(symmetricAlgorithm))
                {
                    byte[] encrypted;
                    byte[] salt       = new byte[PBKDF2_SaltSizeBytes];
                    int    maxKeySize = GetLegalKeySizes(algorithm).Max();

                    _rng.GetBytes(salt);
                    using (Rfc2898DeriveBytes pbkdf2 = new Rfc2898DeriveBytes(key, salt, PBKDF2_Iterations))
                    {
                        algorithm.Key = pbkdf2.GetBytes(maxKeySize);
                    }

                    using (ICryptoTransform cryptoTransform = algorithm.CreateEncryptor())
                    {
                        using (MemoryStream inputStream = new MemoryStream(inputBytes), transformedStream = new MemoryStream())
                        {
                            using (CryptoStream cryptoStream = new CryptoStream(inputStream, cryptoTransform, CryptoStreamMode.Read))
                            {
                                cryptoStream.CopyTo(transformedStream);
                            }

                            encrypted = transformedStream.ToArray();
                        }
                    }

                    result = new byte[salt.Length + algorithm.IV.Length + encrypted.Length];
                    Buffer.BlockCopy(salt, 0, result, 0, salt.Length);
                    Buffer.BlockCopy(algorithm.IV, 0, result, salt.Length, algorithm.IV.Length);
                    Buffer.BlockCopy(encrypted, 0, result, salt.Length + algorithm.IV.Length, encrypted.Length);
                }

                return(result);
            }
        }
Example #20
0
        /// <summary>
        /// Encrypts the specified data.
        /// </summary>
        /// <param name="data">The data.</param>
        /// <param name="key">The key.</param>
        /// <param name="salt">The salt.</param>
        /// <param name="hashingAlgorithm">The hashing algorithm.</param>
        /// <param name="passwordIterations">The password iterations.</param>
        /// <param name="initialVector">The initial vector. 16 ASCII characters long.</param>
        /// <param name="keySize">
        /// Size of the key. Can be 64 (DES only), 128 (AES), 192 (AES and Triple DES), or 256 (AES)
        /// </param>
        /// <returns>The encrypted data.</returns>
        public byte[] Encrypt(
            byte[] data,
            byte[] key,
            byte[] salt,
            HashingAlgorithms hashingAlgorithm,
            int passwordIterations,
            byte[] initialVector,
            int keySize,
            SymmetricAlgorithms algorithm)
        {
            var SymmetricAlgorithm = Symmetrics.FirstOrDefault(x => string.Equals(x.Name, algorithm.ToString(), StringComparison.OrdinalIgnoreCase));

            if (SymmetricAlgorithm == null)
            {
                return(Array.Empty <byte>());
            }
            key = (byte[])key.Clone();
            return(SymmetricAlgorithm.Encrypt(data, key, salt, hashingAlgorithm, passwordIterations, initialVector, keySize));
        }
        public void Decrypt(SymmetricAlgorithms algorithms, int keySize)
        {
            var Key = new byte[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
            var IV  = new byte[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 1, 2, 3, 4, 5, 6 };

            Assert.Equal("Test String", "Test String".Encrypt((byte[])Key.Clone(),
                                                              "Salt".ToByteArray(),
                                                              HashingAlgorithms.SHA512,
                                                              2,
                                                              IV,
                                                              keySize,
                                                              algorithms)
                         .Decrypt((byte[])Key.Clone(),
                                  "Salt".ToByteArray(),
                                  HashingAlgorithms.SHA512,
                                  2,
                                  IV,
                                  keySize,
                                  algorithms).ToString(Encoding.UTF8));
        }
        public void SymmetricAlgorithmsEncryptionMatches(SymmetricAlgorithms enumValue)
        {
            string toProcess = "`~1234567890-=qwertyuiop[]\\ASDFGHJKL:\"ZXCVBNM<>?ăîșțâ";
            string key       = "{>@#F09\0";

            EncryptText symmetricAlgorithm = new EncryptText
            {
                Algorithm = enumValue,
                Encoding  = new InArgument <Encoding>(ExpressionServices.Convert((env) => System.Text.Encoding.Unicode))
            };
            Dictionary <string, object> arguments = new Dictionary <string, object>();

            arguments.Add(nameof(EncryptText.Input), toProcess);
            arguments.Add(nameof(EncryptText.Key), key);

            WorkflowInvoker invoker        = new WorkflowInvoker(symmetricAlgorithm);
            string          activityString = (string)invoker.Invoke(arguments)[nameof(symmetricAlgorithm.Result)];

            byte[] algorithmBytes = CryptographyHelper.DecryptData(enumValue, Convert.FromBase64String(activityString), Encoding.Unicode.GetBytes(key));

            Assert.Equal(toProcess, Encoding.Unicode.GetString(algorithmBytes));
        }
Example #23
0
        public void SymmetricAlgorithmsDecryptionMatches(SymmetricAlgorithms enumValue)
        {
            string toProcess = "`~1234567890-=qwertyuiop[]\\ASDFGHJKL:\"ZXCVBNM<>?ăîșțâ";
            string key       = "{>@#F09\0";

            byte[] algorithmBytes = CryptographyHelper.EncryptData(enumValue, Encoding.Unicode.GetBytes(toProcess), Encoding.Unicode.GetBytes(key));

            DecryptText symmetricAlgorithm = new DecryptText
            {
                Algorithm = enumValue,
                Encoding  = new VisualBasicValue <Encoding>(typeof(Encoding).FullName + "." + nameof(Encoding.Unicode))
            };

            Dictionary <string, object> arguments = new Dictionary <string, object>();

            arguments.Add(nameof(DecryptText.Input), Convert.ToBase64String(algorithmBytes));
            arguments.Add(nameof(DecryptText.Key), key);

            WorkflowInvokerTest invoker = new WorkflowInvokerTest(symmetricAlgorithm);
            string activityString       = (string)invoker.TestActivity(arguments)[nameof(symmetricAlgorithm.Result)];

            Assert.Equal(toProcess, activityString);
        }
Example #24
0
        private static SymmetricAlgorithm GetSymmetricAlgorithm(SymmetricAlgorithms symmetricAlgorithm)
        {
            switch (symmetricAlgorithm)
            {
            case SymmetricAlgorithms.AES:
                return(new AesCryptoServiceProvider());    // TODO: Use AesCng after upgrading to .NET Framework 4.6.2

            case SymmetricAlgorithms.DES:
                return(new DESCryptoServiceProvider());

            case SymmetricAlgorithms.RC2:
                return(new RC2CryptoServiceProvider());

            case SymmetricAlgorithms.Rijndael:
                return(new RijndaelManaged());

            case SymmetricAlgorithms.TripleDES:
                return(new TripleDESCryptoServiceProvider());    // TODO: Use TripleDESCng after upgrading to .NET Framework 4.6.2

            default:
                throw new InvalidOperationException();
            }
        }
Example #25
0
 public void CreateInitialVector(SymmetricAlgorithms algorithms, int keySize)
 {
     Assert.NotNull(TestObject2.CreateRandomInitialVector(algorithms));
 }
 //---------------------------------------------------------------------
 public static byte[] DecryptBytes(SymmetricAlgorithms SymmetricAlgorithm_in, byte[] Bytes_in, byte[] Password_in)
 {
     return(DecryptBytes_(Create_SymmetricAlgorithm(SymmetricAlgorithm_in, Password_in), Bytes_in));
 }
Example #27
0
 /// <summary>
 /// Initializes a new EncryptionAttribute
 /// </summary>
 public EncryptableAttribute(SymmetricAlgorithms Algorithm, byte[] Key, byte[] Salt)
 {
     this._algorithm = Algorithm;
     this._key       = Key;
     this._salt      = Salt;
 }
 //---------------------------------------------------------------------
 public static SymmetricAlgorithm Create_SymmetricAlgorithm(SymmetricAlgorithms SymmetricAlgorithm_in, string Password_in)
 {
     byte[] KeyText = new System.Text.ASCIIEncoding().GetBytes(Password_in);
     return(Create_SymmetricAlgorithm(SymmetricAlgorithm_in, KeyText));
 }
        //---------------------------------------------------------------------
        public static SymmetricAlgorithm Create_SymmetricAlgorithm(SymmetricAlgorithms SymmetricAlgorithm_in, byte[] PasswordBytes_in)
        {
            HashAlgorithm      HA = Create_HashAlgorithm(HashAlgorithms.SHA_512);
            SymmetricAlgorithm SA = null;

            switch (SymmetricAlgorithm_in)
            {
            case SymmetricAlgorithms.None:
                return(null);

            case SymmetricAlgorithms.Unused_1:
                return(null);

            case SymmetricAlgorithms.RC2_64_40:
                SA           = new RC2CryptoServiceProvider();
                SA.BlockSize = 64;
                SA.KeySize   = 40;
                break;

            case SymmetricAlgorithms.RC2_64_48:
                SA           = new RC2CryptoServiceProvider();
                SA.BlockSize = 64;
                SA.KeySize   = 48;
                break;

            case SymmetricAlgorithms.RC2_64_56:
                SA           = new RC2CryptoServiceProvider();
                SA.BlockSize = 64;
                SA.KeySize   = 56;
                break;

            case SymmetricAlgorithms.RC2_64_64:
                SA           = new RC2CryptoServiceProvider();
                SA.BlockSize = 64;
                SA.KeySize   = 64;
                break;

            case SymmetricAlgorithms.RC2_64_72:
                SA           = new RC2CryptoServiceProvider();
                SA.BlockSize = 64;
                SA.KeySize   = 72;
                break;

            case SymmetricAlgorithms.RC2_64_80:
                SA           = new RC2CryptoServiceProvider();
                SA.BlockSize = 64;
                SA.KeySize   = 80;
                break;

            case SymmetricAlgorithms.RC2_64_88:
                SA           = new RC2CryptoServiceProvider();
                SA.BlockSize = 64;
                SA.KeySize   = 88;
                break;

            case SymmetricAlgorithms.RC2_64_96:
                SA           = new RC2CryptoServiceProvider();
                SA.BlockSize = 64;
                SA.KeySize   = 96;
                break;

            case SymmetricAlgorithms.RC2_64_104:
                SA           = new RC2CryptoServiceProvider();
                SA.BlockSize = 64;
                SA.KeySize   = 104;
                break;

            case SymmetricAlgorithms.RC2_64_112:
                SA           = new RC2CryptoServiceProvider();
                SA.BlockSize = 64;
                SA.KeySize   = 112;
                break;

            case SymmetricAlgorithms.RC2_64_120:
                SA           = new RC2CryptoServiceProvider();
                SA.BlockSize = 64;
                SA.KeySize   = 120;
                break;

            case SymmetricAlgorithms.RC2_64_128:
                SA           = new RC2CryptoServiceProvider();
                SA.BlockSize = 64;
                SA.KeySize   = 128;
                break;

            case SymmetricAlgorithms.DES_64_64:
                SA           = new DESCryptoServiceProvider();
                SA.BlockSize = 64;
                SA.KeySize   = 64;
                break;

            case SymmetricAlgorithms.DES3_64_128:
                SA           = new TripleDESCryptoServiceProvider();
                SA.BlockSize = 64;
                SA.KeySize   = 128;
                break;

            case SymmetricAlgorithms.DES3_64_192:
                SA           = new TripleDESCryptoServiceProvider();
                SA.BlockSize = 64;
                SA.KeySize   = 192;
                break;

            case SymmetricAlgorithms.AES_128_128:
                SA           = new RijndaelManaged();
                SA.BlockSize = 128;
                SA.KeySize   = 128;
                break;

            case SymmetricAlgorithms.AES_128_192:
                SA           = new RijndaelManaged();
                SA.BlockSize = 128;
                SA.KeySize   = 192;
                break;

            case SymmetricAlgorithms.AES_128_256:
                SA           = new RijndaelManaged();
                SA.BlockSize = 128;
                SA.KeySize   = 256;
                break;

            case SymmetricAlgorithms.AES_192_128:
                SA           = new RijndaelManaged();
                SA.BlockSize = 192;
                SA.KeySize   = 128;
                break;

            case SymmetricAlgorithms.AES_192_192:
                SA           = new RijndaelManaged();
                SA.BlockSize = 192;
                SA.KeySize   = 192;
                break;

            case SymmetricAlgorithms.AES_192_256:
                SA           = new RijndaelManaged();
                SA.BlockSize = 192;
                SA.KeySize   = 256;
                break;

            case SymmetricAlgorithms.AES_256_128:
                SA           = new RijndaelManaged();
                SA.BlockSize = 256;
                SA.KeySize   = 128;
                break;

            case SymmetricAlgorithms.AES_256_192:
                SA           = new RijndaelManaged();
                SA.BlockSize = 256;
                SA.KeySize   = 192;
                break;

            case SymmetricAlgorithms.AES_256_256:
                SA           = new RijndaelManaged();
                SA.BlockSize = 256;
                SA.KeySize   = 256;
                break;

            default:
                return(null);
            }
            SA.Key = Create_CryptographicKey(HashAlgorithms.SHA_256, SA.KeySize, PasswordBytes_in);
            SA.IV  = Create_CryptographicIV(HashAlgorithms.SHA_256, SA.BlockSize, PasswordBytes_in);
            return(SA);
        }
Example #30
0
 /// <summary>
 /// Initializes a new EncryptionAttribute
 /// </summary>
 public EncryptableAttribute(SymmetricAlgorithms Algorithm)
 {
     this._algorithm = Algorithm;
 }
 public SymmetricAlgorithm GetSymmetricAlgorithm(SymmetricAlgorithms symmetricAlgorithm)
 {
     return this._symmetricAlgorithmContainer[symmetricAlgorithm].Build();
 }
Example #32
0
 public void CreateKey(SymmetricAlgorithms algorithms, int keySize)
 {
     Assert.NotNull(TestObject2.CreateRandomKey(algorithms));
 }