/// <summary> /// Initializes a new instance of the <see cref="V2CryptoBase" /> class. /// </summary> /// <param name="key">The key.</param> /// <param name="iv">The iv, or null for none.</param> /// <param name="keyStreamOffset">The key stream offset.</param> /// <exception cref="System.ArgumentNullException">factory /// or /// key /// or /// iv</exception> /// <exception cref="System.ArgumentException">Key length is invalid. /// or /// The IV length must be the same as the algorithm block length.</exception> protected void Initialize(SymmetricKey key, SymmetricIV iv, long keyStreamOffset, SymmetricAlgorithm algorithm) { if (key == null) { throw new ArgumentNullException("key"); } if (algorithm == null) { throw new ArgumentNullException("algorithm"); } if (!algorithm.ValidKeySize(key.Size)) { throw new ArgumentException("Key length is invalid."); } iv = iv ?? new SymmetricIV(new byte[algorithm.BlockSize / 8]); if (iv.Length != algorithm.BlockSize / 8) { throw new ArgumentException("The IV length must be the same as the algorithm block length."); } Key = key; _iv = iv; _blockCounter = keyStreamOffset / iv.Length; _blockOffset = (int)(keyStreamOffset % iv.Length); }
public WrapIterator(Guid cryptoId) { ICryptoFactory factory = Resolve.CryptoFactory.Create(cryptoId); _dummyCrypto = factory.CreateCrypto(factory.CreateDerivedKey(new Passphrase("A dummy passphrase")).DerivedKey, null, 0); _dummySalt = new Salt(_dummyCrypto.Key.Size); _dummyKey = new SymmetricKey(_dummyCrypto.Key.Size); }
/// <summary> /// Initializes a new instance of the <see cref="V2DerivedKey"/> class. /// </summary> /// <param name="passphrase">The passphrase.</param> public V2DerivedKey(Passphrase passphrase, Salt salt, int derivationIterations, int keySize) { if (passphrase == null) { throw new ArgumentNullException("passphrase"); } DerivationSalt = salt; DerivationIterations = derivationIterations; DerivedKey = new SymmetricKey(new Pbkdf2HmacSha512(passphrase.Text, salt, derivationIterations).GetBytes().Reduce(keySize / 8)); }
/// <summary> /// Wrap key data using the AES Key Wrap specification /// </summary> /// <param name="keyToWrap">The key to wrap</param> /// <returns>The wrapped key data, 8 bytes longer than the key</returns> public byte[] Wrap(ICrypto crypto, SymmetricKey keyToWrap) { if (crypto == null) { throw new ArgumentNullException("crypto"); } if (keyToWrap == null) { throw new ArgumentNullException("keyToWrap"); } return(Wrap(crypto, keyToWrap.GetBytes())); }
/// <summary> /// Initializes a new instance of the <see cref="V1DerivedKey"/> class. /// </summary> /// <param name="passphrase">The passphrase.</param> public V1DerivedKey(Passphrase passphrase) { if (passphrase == null) { throw new ArgumentNullException("passphrase"); } HashAlgorithm hashAlgorithm = New <Sha1>(); byte[] ansiBytes = Encoding.GetEncoding("Windows-1252").GetBytes(passphrase.Text); byte[] ansiBytesExtra = ansiBytes.Append(passphrase.Extra()); byte[] hash = hashAlgorithm.ComputeHash(ansiBytesExtra); byte[] derivedKey = new byte[16]; Array.Copy(hash, derivedKey, derivedKey.Length); DerivationSalt = Salt.Zero; DerivationIterations = 0; DerivedKey = new SymmetricKey(derivedKey); }
public ICrypto CreateCrypto(SymmetricKey key, SymmetricIV iv, long keyStreamOffset) { return(new V2AesCrypto(key, iv, keyStreamOffset)); }