/// <summary> /// Initializes a new instance of the SessionKey class. /// </summary> /// <param name="keyData">Delegate that procures key data on demand, cannot be null.</param> /// <exception cref="ArgumentNullException">The keyData argument is null.</exception> /// <exception cref="CryptographicException">The encryption key could not be created, probably due to malformed key data.</exception> public SessionKey(Func <string> keyData) : this() { // omitting arg validation since this type in private this.keyData = keyData; this.algorithm = AsymmetricDataEncryptor.InstantiateAlgorithm(this.keyData); }
/// <summary> /// Decrypts a cipher into the original data. /// </summary> /// <param name="key">The key to the data to be used as entropy, cannot be null but can be empty.</param> /// <param name="cipher">The cipher to decrypt, cannot be null but can be empty.</param> /// <returns>The resulting data, cannot be null but can be empty.</returns> /// <exception cref="ArgumentNullException">The key or cipher argument is null.</exception> /// <exception cref="CryptographicException">The encryption operation failed, probably due to malformed key or key mismatch.</exception> public byte[] Decrypt(string key, byte[] cipher) { if (key == null) { throw new ArgumentNullException("key"); } if (cipher == null) { throw new ArgumentNullException("cipher", string.Format(CultureInfo.InvariantCulture, "Object cannot be null for key {0}", key)); } using (RSACryptoServiceProvider algorithm = AsymmetricDataEncryptor.InstantiateAlgorithm(this.keyData)) { return(AsymmetricDataEncryptor.Decrypt(algorithm, cipher)); } }