Beispiel #1
0
        public static byte[] GetLegalKey(SymmetricProvider netSelected)
        {
            SymmetricAlgorithm symmetricCryptoService = GetSymmetricCryptoService(netSelected);

            symmetricCryptoService.GenerateKey();
            return(symmetricCryptoService.Key);
        }
Beispiel #2
0
        /// <summary>
        /// Initializes a new instance of the Symmetric class based on the specified provider.
        /// </summary>
        public Symmetric(SymmetricProvider provider, bool useDefaultInitializationVector)
        {
            _crypto = SymmetricAlgorithmFactory.Create(provider);

             // make sure key and IV are always set, no matter what
             Key = RandomKey();
             IntializationVector = useDefaultInitializationVector ? new SymmetricKeyData(DefaultIntializationVector) : RandomInitializationVector();
        }
        /// <summary>
        /// Initializes a new instance of the Symmetric class based on the specified provider.
        /// </summary>
        public Symmetric(SymmetricProvider provider, bool useDefaultInitializationVector)
        {
            _crypto = SymmetricFactory.Create(provider);

            // make sure key and IV are always set, no matter what
            Key = RandomKey();
            IntializationVector = useDefaultInitializationVector ? new SymmetricKeyData(DefaultIntializationVector) : RandomInitializationVector();
        }
Beispiel #4
0
        public static SymmetricAlgorithm GetSymmetricCryptoService(SymmetricProvider netSelected)
        {
            SymmetricAlgorithm algorithm = new DESCryptoServiceProvider();

            switch (netSelected)
            {
            case SymmetricProvider.Default:
                return(SymmetricAlgorithm.Create());

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

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

            case SymmetricProvider.Rijndael:
                return(new RijndaelManaged());
            }
            return(algorithm);
        }
Beispiel #5
0
        public SymmetricCryptography(SymmetricProvider symetricProvider)
        {
            switch (symetricProvider)
            {
            case SymmetricProvider.DES:
                symetricCryptoService = new DESCryptoServiceProvider();
                break;

            case SymmetricProvider.RC2:
                symetricCryptoService = new RC2CryptoServiceProvider();
                break;

            case SymmetricProvider.Rijndael:
                symetricCryptoService = new RijndaelManaged();
                break;

            case SymmetricProvider.TripleDES:
                symetricCryptoService = new TripleDESCryptoServiceProvider();
                break;
            }
        }
Beispiel #6
0
        public static string EncryptSymmetric(this string CryptString, SymmetricProvider netSelected, byte[] Key, byte[] IV)
        {
            string             str;
            SymmetricAlgorithm symmetricCryptoService = GetSymmetricCryptoService(netSelected);

            try
            {
                symmetricCryptoService.Key = Key;
                symmetricCryptoService.IV  = IV;
                byte[] bytes = Encoding.UTF8.GetBytes(CryptString);
                return(Convert.ToBase64String(symmetricCryptoService.CreateEncryptor(Key, IV).TransformFinalBlock(bytes, 0, bytes.Length)));
            }
            catch
            {
                str = string.Empty;
            }
            finally
            {
                symmetricCryptoService.Clear();
            }
            return(str);
        }
        /// <summary>
        /// Creates a new instance of the SymmetricAlgorithm class based on the specified provider.
        /// </summary>
        /// <param name="provider">Provides the type of symmetric algorithm to create.</param>
        /// <returns>The SymmetricAlgorithm object.</returns>
        /// <exception cref="T:System.ArgumentException">The provider is unknown.</exception>
        public static SymmetricAlgorithm Create(SymmetricProvider provider, CipherMode cipherMode = CipherMode.ECB, PaddingMode padding = PaddingMode.PKCS7)
        {
            SymmetricAlgorithm algorithm;

            switch (provider)
            {
            case SymmetricProvider.DES:
                algorithm = DES.Create();
                break;

            case SymmetricProvider.RC2:
                algorithm = RC2.Create();
                break;

            case SymmetricProvider.Rijndael:
                algorithm = Rijndael.Create();
                break;

            case SymmetricProvider.TripleDES:
                algorithm = TripleDES.Create();
                break;

            case SymmetricProvider.AES:
                algorithm = Aes.Create();
                break;

            default:
                throw new ArgumentOutOfRangeException(nameof(provider), provider, null);
            }
            if (algorithm == null)
            {
                throw new NullReferenceException(nameof(provider) + provider);
            }
            algorithm.Mode    = cipherMode;
            algorithm.Padding = padding;
            return(algorithm);
            //     throw new ArgumentException("Unknown SymmetricProvider.", nameof(provider));
        }
Beispiel #8
0
        public static string DecryptSymmetric(this string DecryptString, SymmetricProvider netSelected, byte[] Key, byte[] IV)
        {
            string             str;
            SymmetricAlgorithm symmetricCryptoService = GetSymmetricCryptoService(netSelected);

            try
            {
                symmetricCryptoService.Key = Key;
                symmetricCryptoService.IV  = IV;
                byte[]           inputBuffer = Convert.FromBase64String(DecryptString);
                ICryptoTransform transform   = symmetricCryptoService.CreateDecryptor(Key, IV);
                return(Encoding.UTF8.GetString(transform.TransformFinalBlock(inputBuffer, 0, inputBuffer.Length)));
            }
            catch
            {
                str = string.Empty;
            }
            finally
            {
                symmetricCryptoService.Clear();
            }
            return(str);
        }
        private static string SymmetricFilePrivate(SymmetricProvider p, string fileName, string key)
        {
            string encryptedFilePath = Path.GetFileNameWithoutExtension(fileName) + ".encrypted";
             string decryptedFilePath = Path.GetFileNameWithoutExtension(Path.GetFileNameWithoutExtension(fileName)) + "-decrypted" + Path.GetExtension(fileName);

             // encrypt the file to memory
             Data encryptedData;
             using (var sym = new Symmetric(p))
             {
            sym.Key = new SymmetricKeyData(key);
            using (var stream = File.OpenRead(fileName))
            {
               encryptedData = sym.Encrypt(stream);
            }
             }

             // write encrypted data to a new binary file
             using (var stream = File.Open(encryptedFilePath, FileMode.Create))
             using (var bw = new BinaryWriter(stream))
             {
            bw.Write(encryptedData.Bytes);
             }

             // decrypt this binary file
             Data decryptedData;
             using (var sym2 = new Symmetric(p))
             {
            sym2.Key = new SymmetricKeyData(key);
            using (var stream = File.OpenRead(encryptedFilePath))
            {
               decryptedData = sym2.Decrypt(stream);
            }
             }

             // write decrypted data to a new binary file
             using (var stream = File.Open(decryptedFilePath, FileMode.Create))
             using (var bw = new BinaryWriter(stream))
             {
            bw.Write(decryptedData.Bytes);
             }

             // get the MD5 hash of the returned data
             using (var h = new Hash(HashProvider.MD5))
             {
            return h.Calculate(decryptedData).Bytes.ToHex();
             }
        }
Beispiel #10
0
 /// <summary>
 /// Initializes a new instance of the Symmetric class based on the specified provider.
 /// </summary>
 public Symmetric(SymmetricProvider provider)
     : this(provider, true)
 {
 }
Beispiel #11
0
        /// <summary>
        /// Creates a new instance of the SymmetricAlgorithm class based on the specified provider.
        /// </summary>
        /// <param name="provider">Provides the type of symmetric algorithm to create.</param>
        /// <returns>The SymmetricAlgorithm object.</returns>
        /// <exception cref="T:System.ArgumentException">The provider is unknown.</exception>
        public static SymmetricAlgorithm Create(SymmetricProvider provider)
        {
            switch (provider)
             {
            case SymmetricProvider.DES:
               return DES.Create();
            case SymmetricProvider.RC2:
               return RC2.Create();
            case SymmetricProvider.Rijndael:
               return Rijndael.Create();
            case SymmetricProvider.TripleDES:
               return TripleDES.Create();
             }

             throw new ArgumentException("Unknown SymmetricProvider.", "provider");
        }
Beispiel #12
0
 /// <summary>
 /// Gets the symmetric crypthography.
 /// </summary>
 /// <param name="prov">The symmetric provider.</param>
 /// <param name="usr">The s user id.</param>
 /// <returns></returns>
 private static SymmetricCryptography GetSymmetricCrypthography(
     SymmetricProvider prov,
     string usr)
 {
     return new SymmetricCryptography(prov)
                {
                    Key =
                        GenerateKey(usr),
                    Salt = "17j4n0e41r1t4h0en2o09"
                };
 }
Beispiel #13
0
 public static string DecryptSymmetric(this string DecryptString, SymmetricProvider netSelected, string Key, string IV)
 {
     return(DecryptString.DecryptSymmetric(netSelected, Key.HexStringToByteArray(), IV.HexStringToByteArray()));
 }
Beispiel #14
0
 public static string GenerateKey(SymmetricProvider netSelected)
 {
     return(CryptogramHelper.ByteArrayToHexString(GetLegalKey(netSelected)));
 }
        /// <summary>
        /// test using auto-generated keys
        /// </summary>
        private static string SymmetricLoopback(SymmetricProvider p, string targetString)
        {
            Data decryptedData;
             using (var sym = new Symmetric(p))
             {
            Data encryptedData = sym.Encrypt(new Data(targetString));
            using (var sym2 = new Symmetric(p))
            {
               decryptedData = sym2.Decrypt(encryptedData, sym.Key);
            }
             }

             ////-- the data will be padded to the encryption blocksize, so we need to trim it back down.
             //return decryptedData.ToString().Substring(0, _TargetData.Bytes.Length);

             return decryptedData.ToString();
        }
        /// <summary>
        /// test using user-provided keys and init vectors
        /// </summary>
        private static string SymmetricWithKey(SymmetricProvider p, string targetString)
        {
            var keyData = new SymmetricKeyData("MySecretPassword");
             var ivData = new SymmetricKeyData("MyInitializationVector");

             Data encryptedData;
             using (var sym = new Symmetric(p, false))
             {
            sym.IntializationVector = ivData;
            encryptedData = sym.Encrypt(new Data(targetString), keyData);
             }

             Data decryptedData;
             using (var sym2 = new Symmetric(p, false))
             {
            sym2.IntializationVector = ivData;
            decryptedData = sym2.Decrypt(encryptedData, keyData);
             }

             ////-- the data will be padded to the encryption blocksize, so we need to trim it back down.
             //return decryptedData.ToString().Substring(0, _TargetData.Bytes.Length);

             return decryptedData.ToString();
        }
 /// <summary>
 /// Initializes a new instance of the Symmetric class based on the specified provider.
 /// </summary>
 public Symmetric(SymmetricProvider provider)
     : this(provider, true)
 {
 }