public static byte[] Encrypt(this byte[] Data, DeriveBytes Key, SymmetricAlgorithm AlgorithmUsing = null, string InitialVector = "OFRna73m*aze01xY", int KeySize = 256) { Contract.Requires <ArgumentNullException>(!string.IsNullOrEmpty(InitialVector), "InitialVector"); if (Data == null) { return(null); } AlgorithmUsing = AlgorithmUsing.Check(() => new RijndaelManaged()); using (DeriveBytes DerivedPassword = Key) { using (SymmetricAlgorithm SymmetricKey = AlgorithmUsing) { SymmetricKey.Mode = CipherMode.CBC; byte[] CipherTextBytes = null; using (ICryptoTransform Encryptor = SymmetricKey.CreateEncryptor(DerivedPassword.GetBytes(KeySize / 8), InitialVector.ToByteArray())) { using (MemoryStream MemStream = new MemoryStream()) { using (CryptoStream CryptoStream = new CryptoStream(MemStream, Encryptor, CryptoStreamMode.Write)) { CryptoStream.Write(Data, 0, Data.Length); CryptoStream.FlushFinalBlock(); CipherTextBytes = MemStream.ToArray(); } } } SymmetricKey.Clear(); return(CipherTextBytes); } } }
/// <summary> /// Decrypts a byte array /// </summary> /// <param name="Data">Data to be decrypted</param> /// <param name="Key">Password to decrypt with</param> /// <param name="Algorithm">Algorithm to use for decryption</param> /// <param name="InitialVector">Needs to be 16 ASCII characters long</param> /// <param name="KeySize"> /// Can be 64 (DES only), 128 (AES), 192 (AES and Triple DES), or 256 (AES) /// </param> /// <returns>A decrypted byte array</returns> public byte[] Decrypt(byte[] Data, DeriveBytes Key, string Algorithm = "AES", string InitialVector = "OFRna73m*aze01xY", int KeySize = 256) { if (string.IsNullOrEmpty(InitialVector)) { throw new ArgumentNullException(nameof(InitialVector)); } if (Data == null) { return(null); } using (SymmetricAlgorithm SymmetricKey = GetProvider(Algorithm)) { byte[] PlainTextBytes = new byte[0]; if (SymmetricKey != null) { SymmetricKey.Mode = CipherMode.CBC; using (ICryptoTransform Decryptor = SymmetricKey.CreateDecryptor(Key.GetBytes(KeySize / 8), InitialVector.ToByteArray())) { using (MemoryStream MemStream = new MemoryStream(Data)) { using (CryptoStream CryptoStream = new CryptoStream(MemStream, Decryptor, CryptoStreamMode.Read)) { PlainTextBytes = CryptoStream.ReadAllBinary(); } } } SymmetricKey.Clear(); } return(PlainTextBytes); } }
/// <summary> /// Encrypts a byte array /// </summary> /// <param name="Data">Data to be encrypted</param> /// <param name="Key">Password to encrypt with</param> /// <param name="InitialVector">Needs to be 16 ASCII characters long</param> /// <param name="KeySize"> /// Can be 64 (DES only), 128 (AES), 192 (AES and Triple DES), or 256 (AES) /// </param> /// <param name="Algorithm">Algorithm to use</param> /// <returns>The encrypted byte array</returns> public byte[] Encrypt(byte[] Data, DeriveBytes Key, string Algorithm = "AES", string InitialVector = "OFRna73m*aze01xY", int KeySize = 256) { if (string.IsNullOrEmpty(InitialVector)) { throw new ArgumentNullException(nameof(InitialVector)); } if (Data == null) { return(null); } using (SymmetricAlgorithm SymmetricKey = GetProvider(Algorithm)) { byte[] CipherTextBytes = new byte[0]; if (SymmetricKey != null) { SymmetricKey.Mode = CipherMode.CBC; using (ICryptoTransform Encryptor = SymmetricKey.CreateEncryptor(Key.GetBytes(KeySize / 8), InitialVector.ToByteArray())) { using (MemoryStream MemStream = new MemoryStream()) { using (CryptoStream CryptoStream = new CryptoStream(MemStream, Encryptor, CryptoStreamMode.Write)) { CryptoStream.Write(Data, 0, Data.Length); CryptoStream.FlushFinalBlock(); CipherTextBytes = MemStream.ToArray(); } } } SymmetricKey.Clear(); } return(CipherTextBytes); } }
/// <summary> /// Decrypts a byte array /// </summary> /// <param name="Data">Data to encrypt</param> /// <param name="Key">Key to use to encrypt the data (can use PasswordDeriveBytes, Rfc2898DeriveBytes, etc. Really anything that implements DeriveBytes)</param> /// <param name="AlgorithmUsing">Algorithm to use for encryption (defaults to AES)</param> /// <param name="InitialVector">Needs to be 16 ASCII characters long</param> /// <param name="KeySize">Can be 64 (DES only), 128 (AES), 192 (AES and Triple DES), or 256 (AES)</param> /// <returns>An encrypted byte array</returns> public static byte[] Decrypt(this byte[] Data, DeriveBytes Key, SymmetricAlgorithm AlgorithmUsing = null, string InitialVector = "OFRna73m*aze01xY", int KeySize = 256) { if (Data.IsNull()) { return(null); } AlgorithmUsing = AlgorithmUsing.NullCheck(new RijndaelManaged()); InitialVector.ThrowIfNullOrEmpty("InitialVector"); using (DeriveBytes DerivedPassword = Key) { using (SymmetricAlgorithm SymmetricKey = AlgorithmUsing) { SymmetricKey.Mode = CipherMode.CBC; byte[] PlainTextBytes = null; using (ICryptoTransform Decryptor = SymmetricKey.CreateDecryptor(DerivedPassword.GetBytes(KeySize / 8), InitialVector.ToByteArray())) { using (MemoryStream MemStream = new MemoryStream(Data)) { using (CryptoStream CryptoStream = new CryptoStream(MemStream, Decryptor, CryptoStreamMode.Read)) { PlainTextBytes = CryptoStream.ReadAllBinary(); MemStream.Close(); CryptoStream.Close(); } } } SymmetricKey.Clear(); return(PlainTextBytes); } } }
private string AesEncrypt(byte[] clearBytes, DeriveBytes derived) { if (clearBytes == null || clearBytes.Length == 0) { throw new ArgumentException("Nothing to encrypt", "clearBytes"); } if (derived == null) { throw new ArgumentException("Nothing to encrypt", "derived"); } var enc = string.Empty; using (var encryptor = Aes.Create()) { encryptor.Key = derived.GetBytes(KeySize); encryptor.IV = derived.GetBytes(InitVectorSize); encryptor.Mode = CipherMode.CBC; using (var ms = new MemoryStream()) { using (var cs = new CryptoStream(ms, encryptor.CreateEncryptor(), CryptoStreamMode.Write)) { cs.Write(clearBytes, 0, clearBytes.Length); cs.Close(); } enc = Convert.ToBase64String(ms.ToArray()); } } return(enc); }
public static byte[] Decrypt(this byte[] data, DeriveBytes key, SymmetricAlgorithm algorithmUsing = null, string initialVector = "OFRna73m*aze01xY", int keySize = 256) { if (data.IsNull()) { return(null); } algorithmUsing = algorithmUsing.NullCheck(() => new RijndaelManaged()); Guard.NotEmpty(initialVector, "initialVector"); using (DeriveBytes derivedPassword = key) { using (SymmetricAlgorithm symmetricKey = algorithmUsing) { symmetricKey.Mode = CipherMode.CBC; byte[] plainTextBytes; using ( ICryptoTransform decryptor = symmetricKey.CreateDecryptor(derivedPassword.GetBytes(keySize / 8), initialVector.ToByteArray())) { using (var memStream = new MemoryStream(data)) { using (var cryptoStream = new CryptoStream(memStream, decryptor, CryptoStreamMode.Read)) { plainTextBytes = cryptoStream.ReadAllBinary(); } } } symmetricKey.Clear(); return(plainTextBytes); } } }
/// <summary> /// Constructs the serializer. /// </summary> /// <param name="underlyingSerializer">underlying serializer (e.g. XmlStringSerializer or BinarySerializer)</param> /// <param name="passwordBasedKeyGenerator">generator of key from the password</param> /// <param name="keyBitSize">bit size of the key</param> public AesSerializer(ISerializer underlyingSerializer, DeriveBytes passwordBasedKeyGenerator, int keyBitSize) { using (EneterTrace.Entering()) { myCryptoSerializer = new CryptoSerializerProvider(underlyingSerializer, passwordBasedKeyGenerator, keyBitSize); } }
protected SymmetricEncryption( EncryptionOptions options, TSymmetricAlgorithm symmetricAlgorithm, Func <string, byte[], int, DeriveBytes> deriveBytesFactory, string password) : base(options, password) { if (symmetricAlgorithm == null) { throw new ArgumentNullException("symmetricAlgorithm"); } if (deriveBytesFactory == null) { throw new ArgumentNullException("deriveBytesFactory"); } SymmetricAlgorithm = symmetricAlgorithm; int keySizeBytes = SymmetricAlgorithm.KeySize / 8; int blockSizeBytes = SymmetricAlgorithm.BlockSize / 8; DeriveBytes passwordDeriveBytes = deriveBytesFactory(password, options.Salt, options.IterationCount); SymmetricAlgorithm.Key = passwordDeriveBytes.GetBytes(keySizeBytes); DeriveBytes ivDeriveBytes = deriveBytesFactory(options.IvBase, new byte[8], 1); SymmetricAlgorithm.IV = ivDeriveBytes.GetBytes(blockSizeBytes); }
/// <summary> /// Encrypts the given value using the provided symmetric algorithm, password and salt. /// </summary> /// <param name="value">The original value.</param> /// <param name="isCepToken">A value indicating whether the token is from CEP.</param> /// <returns>The encrypted value.</returns> public string Encrypt(string value, bool isCepToken) { DeriveBytes rgb = isCepToken ? new Rfc2898DeriveBytes(Settings.Default.CepKey, Encoding.Unicode.GetBytes(Settings.Default.CepSalt)) : new Rfc2898DeriveBytes(Settings.Default.WrKey, Encoding.Unicode.GetBytes(Settings.Default.WrSalt)); SymmetricAlgorithm algorithm = new RijndaelManaged(); var rgbKey = rgb.GetBytes(algorithm.KeySize >> 3); var rgbIv = rgb.GetBytes(algorithm.BlockSize >> 3); var transform = algorithm.CreateEncryptor(rgbKey, rgbIv); using (var buffer = new MemoryStream()) { using (var stream = new CryptoStream(buffer, transform, CryptoStreamMode.Write)) { using (var writer = new StreamWriter(stream, Encoding.Unicode)) { writer.Write(value); } } return(Convert.ToBase64String(buffer.ToArray())); } }
/// <summary> /// Initializes a new instance of the <see cref="PasswordHash"/> class. /// </summary> /// <param name="keyDerivationFunction">The class that will be used to perform key derivation.</param> private PasswordHash(DeriveBytes keyDerivationFunction) { if (keyDerivationFunction.IsNull()) { throw new ArgumentNullException(paramName: nameof(keyDerivationFunction)); } m_keyDerivationFunction = keyDerivationFunction; }
/// <summary> /// Derives the key with the help of /// PBKDF2 /// </summary> /// <param name="password">Password for the /// key derivation</param> /// <param name="salt">Salt for the key /// derivation</param> /// <returns>Derived key</returns> public byte[] DeriveKey(string password, string salt) { byte[] convertedPassword = Encoding.UTF8.GetBytes(password); byte[] convertedSalt = Convert.FromBase64String(salt); using (DeriveBytes deriveBytes = this.CreatePBKDF2(convertedPassword, convertedSalt)) { return(deriveBytes.GetBytes(KEY_SIZE)); } }
private static RijndaelManaged CreateKey(DeriveBytes deriveBytes) { var result = new RijndaelManaged { Mode = CipherMode.CBC, Padding = PaddingMode.PKCS7 }; result.Key = deriveBytes.GetBytes(result.KeySize / 8); result.IV = deriveBytes.GetBytes(result.BlockSize / 8); return(result); }
public KeyBlock(CipherSuite cipherSuite, byte[] masterSecret, byte[] seed) { DeriveBytes deriveBytes = cipherSuite.PseudoRandomFunction.CreateDeriveBytes(masterSecret, "key expansion", seed); ClientWriteMACKey = deriveBytes.GetBytes(cipherSuite.MACAlgorithm.HashSize); ServerWriteMACKey = deriveBytes.GetBytes(cipherSuite.MACAlgorithm.HashSize); ClientWriteKey = deriveBytes.GetBytes(cipherSuite.BulkCipherAlgorithm.KeySize); ServerWriteKey = deriveBytes.GetBytes(cipherSuite.BulkCipherAlgorithm.KeySize); ClientWriteIV = deriveBytes.GetBytes(cipherSuite.BulkCipherAlgorithm.FixedIVLength); ServerWriteIV = deriveBytes.GetBytes(cipherSuite.BulkCipherAlgorithm.FixedIVLength); }
public static string DecryptBase64(string str) { byte[] initVectorBytes = null; initVectorBytes = Encoding.ASCII.GetBytes(initVector); byte[] saltValueBytes = null; saltValueBytes = Encoding.ASCII.GetBytes(saltValue); byte[] cipherTextBytes = null; str = str.Replace(" ", "+"); cipherTextBytes = Convert.FromBase64String(str); DeriveBytes password = default(DeriveBytes); password = new PasswordDeriveBytes(passPhrase, saltValueBytes, hashAlgorithm, passwordIterations); byte[] keyBytes = null; keyBytes = password.GetBytes(keySize / 8); RijndaelManaged symmetricKey = default(RijndaelManaged); symmetricKey = new RijndaelManaged(); symmetricKey.Mode = CipherMode.CBC; ICryptoTransform decryptor = default(ICryptoTransform); decryptor = symmetricKey.CreateDecryptor(keyBytes, initVectorBytes); MemoryStream memoryStream = default(MemoryStream); memoryStream = new MemoryStream(cipherTextBytes); CryptoStream cryptoStream = default(CryptoStream); cryptoStream = new CryptoStream(memoryStream, decryptor, CryptoStreamMode.Read); byte[] plainTextBytes = null; plainTextBytes = new byte[cipherTextBytes.Length + 1]; int decryptedByteCount = 0; decryptedByteCount = cryptoStream.Read(plainTextBytes, 0, plainTextBytes.Length); memoryStream.Close(); cryptoStream.Close(); string plainText = null; plainText = Encoding.UTF8.GetString(plainTextBytes, 0, decryptedByteCount); return(plainText); }
public void DifferentCases(int a, string password) { var rng = RandomNumberGenerator.Create(); var salt = new byte[16]; DeriveBytes e = a switch { 1 => new Rfc2898DeriveBytes(password, salt), // Noncompliant 2 => new PasswordDeriveBytes(passwordBytes, salt), // Noncompliant _ => null }; var salt2 = new byte[16]; if (a == 1) { rng.GetBytes(salt2); new PasswordDeriveBytes(passwordBytes, salt2); // Compliant } new PasswordDeriveBytes(passwordBytes, salt2); // Noncompliant {{Make this salt unpredictable.}} var noncompliantSalt = new byte[16]; var compliantSalt = new byte[16]; rng.GetBytes(compliantSalt); var salt3 = a == 2 ? compliantSalt : noncompliantSalt; new PasswordDeriveBytes(passwordBytes, salt3); // Noncompliant var salt4 = compliantSalt; new PasswordDeriveBytes(passwordBytes, salt4); var salt5 = noncompliantSalt; new PasswordDeriveBytes(passwordBytes, salt5); // Noncompliant noncompliantSalt = compliantSalt; new PasswordDeriveBytes(passwordBytes, noncompliantSalt); new PasswordDeriveBytes(passwordBytes, new byte[16]); // Noncompliant var rnd = new Random(); var saltCustom = new byte[16]; for (int i = 0; i < saltCustom.Length; i++) { saltCustom[i] = (byte)rnd.Next(255); } new PasswordDeriveBytes(passwordBytes, saltCustom); // Noncompliant }
public static string EncryptBase64(string str) { byte[] initVectorBytes = null; initVectorBytes = Encoding.ASCII.GetBytes(initVector); byte[] saltValueBytes = null; saltValueBytes = Encoding.ASCII.GetBytes(saltValue); byte[] plainTextBytes = null; plainTextBytes = Encoding.UTF8.GetBytes(str); DeriveBytes password = default(DeriveBytes); password = new PasswordDeriveBytes(passPhrase, saltValueBytes, hashAlgorithm, passwordIterations); byte[] keyBytes = null; keyBytes = password.GetBytes(keySize / 8); RijndaelManaged symmetricKey = default(RijndaelManaged); symmetricKey = new RijndaelManaged(); symmetricKey.Mode = CipherMode.CBC; ICryptoTransform encryptor = default(ICryptoTransform); encryptor = symmetricKey.CreateEncryptor(keyBytes, initVectorBytes); MemoryStream memoryStream = default(MemoryStream); memoryStream = new MemoryStream(); CryptoStream cryptoStream = default(CryptoStream); cryptoStream = new CryptoStream(memoryStream, encryptor, CryptoStreamMode.Write); cryptoStream.Write(plainTextBytes, 0, plainTextBytes.Length); cryptoStream.FlushFinalBlock(); byte[] cipherTextBytes = null; cipherTextBytes = memoryStream.ToArray(); memoryStream.Close(); cryptoStream.Close(); string cipherText = null; cipherText = Convert.ToBase64String(cipherTextBytes); return(cipherText); }
/// <summary> /// Encrypts a byte array /// </summary> /// <param name="Data">Data to encrypt</param> /// <param name="Key"> /// Key to use to encrypt the data (can use PasswordDeriveBytes, Rfc2898DeriveBytes, etc. /// Really anything that implements DeriveBytes) /// </param> /// <param name="AlgorithmUsing">Algorithm to use for encryption (defaults to AES)</param> /// <param name="InitialVector">Needs to be 16 ASCII characters long</param> /// <param name="KeySize"> /// Can be 64 (DES only), 128 (AES), 192 (AES and Triple DES), or 256 (AES) /// </param> /// <returns>An encrypted byte array</returns> public static byte[] Encrypt(this byte[] Data, DeriveBytes Key, string AlgorithmUsing = "AES", string InitialVector = "OFRna73m*aze01xY", int KeySize = 256) { var TempManager = IoC.Manager.Bootstrapper.Resolve <Manager>(); if (TempManager == null) { return(new byte[0]); } return(TempManager.Encrypt(Data, Key, AlgorithmUsing, InitialVector, KeySize)); }
/// <summary> /// Encrypts a byte array /// </summary> /// <param name="Data">Data to be encrypted</param> /// <param name="Key">Password to encrypt with</param> /// <param name="InitialVector">Needs to be 16 ASCII characters long</param> /// <param name="KeySize"> /// Can be 64 (DES only), 128 (AES), 192 (AES and Triple DES), or 256 (AES) /// </param> /// <param name="Algorithm">Algorithm</param> /// <returns>An encrypted byte array</returns> public byte[] Encrypt(byte[] Data, DeriveBytes Key, string Algorithm = "AES", string InitialVector = "OFRna73m*aze01xY", int KeySize = 256) { Contract.Requires <NullReferenceException>(SymmetricAlgorithms != null, "SymmetricAlgorithms"); var Found = SymmetricAlgorithms.FirstOrDefault(x => x.CanHandle(Algorithm)); if (Found == null) { throw new ArgumentException(Algorithm + " not found"); } return(Found.Encrypt(Data, Key, Algorithm, InitialVector, KeySize)); }
public byte[] SaveToBytes(LicenseData licenseData) { DeriveBytes deriveBytes = (DeriveBytes) new PasswordDeriveBytes(Password._password, new byte[13] { (byte)73, (byte)118, (byte)97, (byte)110, (byte)32, (byte)77, (byte)101, (byte)100, (byte)118, (byte)101, (byte)100, (byte)101, (byte)118 }); Rijndael rijndael = Rijndael.Create(); rijndael.Key = deriveBytes.GetBytes(32); rijndael.IV = deriveBytes.GetBytes(16); using (MemoryStream memoryStream1 = new MemoryStream()) { using (CryptoStream cryptoStream = new CryptoStream((Stream)memoryStream1, rijndael.CreateEncryptor(), CryptoStreamMode.Write)) { using (BinaryWriter binaryWriter = new BinaryWriter((Stream)cryptoStream)) { using (MemoryStream memoryStream2 = new MemoryStream()) { new XmlSerializer(typeof(LicenseData)).Serialize((Stream)memoryStream2, (object)licenseData); byte[] buffer1 = memoryStream2.GetBuffer(); byte[] hash = SHA256.Create().ComputeHash(buffer1); binaryWriter.Write(buffer1.Length + hash.Length); binaryWriter.Write(buffer1); binaryWriter.Write(hash); binaryWriter.Flush(); cryptoStream.FlushFinalBlock(); memoryStream1.Position = 0L; byte[] buffer2 = new byte[memoryStream1.Length]; memoryStream1.Read(buffer2, 0, buffer2.Length); return(buffer2); } } } } }
private static RijndaelManaged CreateCipher(DeriveBytes key) { var cipher = new RijndaelManaged { KeySize = 256, BlockSize = 128, Mode = CipherMode.CFB, Padding = PaddingMode.PKCS7 }; cipher.Key = key.GetBytes(cipher.KeySize / 8); cipher.IV = key.GetBytes(cipher.BlockSize / 8); return(cipher); }
/// <summary> /// Decrypts a string /// </summary> /// <param name="Data">Text to be decrypted (Base 64 string)</param> /// <param name="Key"> /// Key to use to encrypt the data (can use PasswordDeriveBytes, Rfc2898DeriveBytes, etc. /// Really anything that implements DeriveBytes) /// </param> /// <param name="EncodingUsing"> /// Encoding that the output string should use (defaults to UTF8) /// </param> /// <param name="AlgorithmUsing">Algorithm to use for decryption (defaults to AES)</param> /// <param name="InitialVector">Needs to be 16 ASCII characters long</param> /// <param name="KeySize"> /// Can be 64 (DES only), 128 (AES), 192 (AES and Triple DES), or 256 (AES) /// </param> /// <returns>A decrypted string</returns> public static string Decrypt(this string Data, DeriveBytes Key, Encoding EncodingUsing = null, string AlgorithmUsing = "AES", string InitialVector = "OFRna73m*aze01xY", int KeySize = 256) { if (string.IsNullOrEmpty(Data)) { return(""); } return(Data.FromBase64() .Decrypt(Key, AlgorithmUsing, InitialVector, KeySize) .ToString(EncodingUsing)); }
/// <summary> /// Encrypts a byte array /// </summary> /// <param name="Data">Data to encrypt</param> /// <param name="Key">Key to use to encrypt the data (can use PasswordDeriveBytes, Rfc2898DeriveBytes, etc. Really anything that implements DeriveBytes)</param> /// <param name="AlgorithmUsing">Algorithm to use for encryption (defaults to AES)</param> /// <param name="InitialVector">Needs to be 16 ASCII characters long</param> /// <param name="KeySize">Can be 64 (DES only), 128 (AES), 192 (AES and Triple DES), or 256 (AES)</param> /// <param name="EncodingUsing">Encoding that the original string is using (defaults to UTF8)</param> /// <returns>An encrypted byte array</returns> public static string Encrypt(this string Data, DeriveBytes Key, Encoding EncodingUsing = null, SymmetricAlgorithm AlgorithmUsing = null, string InitialVector = "OFRna73m*aze01xY", int KeySize = 256) { if (Data.IsNullOrEmpty()) { return(""); } return(Data.ToByteArray(EncodingUsing) .Encrypt(Key, AlgorithmUsing, InitialVector, KeySize) .ToBase64String()); }
/// <summary> /// Encrypts a byte array /// </summary> /// <param name="Data">Data to encrypt</param> /// <param name="Key"> /// Key to use to encrypt the data (can use PasswordDeriveBytes, Rfc2898DeriveBytes, etc. /// Really anything that implements DeriveBytes) /// </param> /// <param name="AlgorithmUsing">Algorithm to use for encryption (defaults to AES)</param> /// <param name="InitialVector">Needs to be 16 ASCII characters long</param> /// <param name="KeySize"> /// Can be 64 (DES only), 128 (AES), 192 (AES and Triple DES), or 256 (AES) /// </param> /// <param name="EncodingUsing"> /// Encoding that the original string is using (defaults to UTF8) /// </param> /// <returns>An encrypted byte array</returns> public static string Encrypt(this string Data, DeriveBytes Key, Encoding EncodingUsing = null, string AlgorithmUsing = "AES", string InitialVector = "OFRna73m*aze01xY", int KeySize = 256) { if (string.IsNullOrEmpty(Data)) { return(""); } return(Data.ToByteArray(EncodingUsing) .Encrypt(Key, AlgorithmUsing, InitialVector, KeySize) .ToString(Base64FormattingOptions.None)); }
/// <summary> /// Encrypts a byte array /// </summary> /// <param name="data">Data to encrypt</param> /// <param name="key">Key to use to encrypt the data (can use PasswordDeriveBytes, Rfc2898DeriveBytes, etc. Really anything that implements DeriveBytes)</param> /// <param name="algorithmUsing">Algorithm to use for encryption (defaults to AES)</param> /// <param name="initialVector">Needs to be 16 ASCII characters long</param> /// <param name="keySize">Can be 64 (DES only), 128 (AES), 192 (AES and Triple DES), or 256 (AES)</param> /// <param name="encodingUsing">Encoding that the original string is using (defaults to UTF8)</param> /// <returns>An encrypted byte array</returns> public static string Encrypt(this string data, DeriveBytes key, Encoding encodingUsing = null, SymmetricAlgorithm algorithmUsing = null, string initialVector = "OFRna73m*aze01xY", int keySize = 256) { if (data.IsNullOrEmpty()) { return(""); } return(data.ToByteArray(encodingUsing) .Encrypt(key, algorithmUsing, initialVector, keySize) .ToBase64String()); }
/// <summary> /// Decrypts a string /// </summary> /// <param name="data">Text to be decrypted (Base 64 string)</param> /// <param name="key">Key to use to encrypt the data (can use PasswordDeriveBytes, Rfc2898DeriveBytes, etc. Really anything that implements DeriveBytes)</param> /// <param name="encodingUsing">Encoding that the output string should use (defaults to UTF8)</param> /// <param name="algorithmUsing">Algorithm to use for decryption (defaults to AES)</param> /// <param name="initialVector">Needs to be 16 ASCII characters long</param> /// <param name="keySize">Can be 64 (DES only), 128 (AES), 192 (AES and Triple DES), or 256 (AES)</param> /// <returns>A decrypted string</returns> public static string Decrypt(this string data, DeriveBytes key, Encoding encodingUsing = null, SymmetricAlgorithm algorithmUsing = null, string initialVector = "OFRna73m*aze01xY", int keySize = 256) { if (data.IsNullOrEmpty()) { return(""); } return(Convert.FromBase64String(data) .Decrypt(key, algorithmUsing, initialVector, keySize) .ToEncodedString(encodingUsing)); }
public static byte[] Encrypt(byte[] data, DeriveBytes rgb) { var algorithm = new AesManaged(); byte[] rgbKey = rgb.GetBytes(algorithm.KeySize >> 3); byte[] rgbIV = rgb.GetBytes(algorithm.BlockSize >> 3); using (ICryptoTransform transform = algorithm.CreateEncryptor(rgbKey, rgbIV)) using (MemoryStream encrypted = new MemoryStream()) using (CryptoStream crypto = new CryptoStream(encrypted, transform, CryptoStreamMode.Write)) { crypto.Write(data, 0, data.Length); crypto.FlushFinalBlock(); return(encrypted.ToArray()); } }
private HashedPassword Compute(string password, byte[] salt, byte complexity, ALGORITHM algorithm) { if (string.IsNullOrWhiteSpace(password)) { throw new ArgumentNullException("Plain text password cannot be null, empty or whitespace"); } HashedPassword hashedPassword = new HashedPassword(salt, complexity, algorithm); using (DeriveBytes provider = GetHashingProvider(password, hashedPassword.Salt, hashedPassword.Iterations, hashedPassword.Algorithm)) { hashedPassword.Content = provider.GetBytes(hashedPassword.Content.Length); } return(hashedPassword); }
public static byte[] Decrypt(byte[] data, DeriveBytes rgb) { var algorithm = new AesManaged(); byte[] rgbKey = rgb.GetBytes(algorithm.KeySize >> 3); byte[] rgbIV = rgb.GetBytes(algorithm.BlockSize >> 3); using (ICryptoTransform transform = algorithm.CreateDecryptor(rgbKey, rgbIV)) using (MemoryStream encrypted = new MemoryStream(data)) using (CryptoStream crypto = new CryptoStream(encrypted, transform, CryptoStreamMode.Read)) using (MemoryStream decrypted = new MemoryStream()) { crypto.CopyTo(decrypted); return(decrypted.ToArray()); } }
private DeriveBytes GetHashingProvider(string password, byte[] salt, int iterations, ALGORITHM algorithm) { DeriveBytes provider = null; switch (algorithm) { case ALGORITHM.PBKDF1: byte[] passwordBytes = Encoding.UTF8.GetBytes(password); provider = new PasswordDeriveBytes(passwordBytes, salt, "SHA1", iterations); break; default: case ALGORITHM.PBKDF2: provider = new Rfc2898DeriveBytes(password, salt, iterations); break; } return(provider); }
public static byte[] Encrypt(this byte[] data, DeriveBytes key, SymmetricAlgorithm algorithmUsing = null, string initialVector = "OFRna73m*aze01xY", int keySize = 256) { if (data.IsNull()) { return(null); } algorithmUsing = algorithmUsing.NullCheck(() => new RijndaelManaged()); Guard.NotEmpty(initialVector, "initialVector"); using (DeriveBytes derivedPassword = key) { using (SymmetricAlgorithm symmetricKey = algorithmUsing) { symmetricKey.Mode = CipherMode.CBC; byte[] cipherTextBytes; using ( ICryptoTransform encryptor = symmetricKey.CreateEncryptor(derivedPassword.GetBytes(keySize / 8), initialVector.ToByteArray())) { using (var memStream = new MemoryStream()) { using (var cryptoStream = new CryptoStream(memStream, encryptor, CryptoStreamMode.Write)) { cryptoStream.Write(data, 0, data.Length); cryptoStream.FlushFinalBlock(); cipherTextBytes = memStream.ToArray(); } } } symmetricKey.Clear(); return(cipherTextBytes); } } }