/// <inheritdoc/> public byte[] Decrypt(byte[] packedCipher, SecureString password) { if (packedCipher == null) { throw new ArgumentNullException("packedCipher"); } CryptoHeader header; byte[] cipher; CryptoHeaderPacker.UnpackHeaderAndCipher(packedCipher, PackageName, out header, out cipher); ISymmetricEncryptionAlgorithm decryptor = new SymmetricEncryptionAlgorithmFactory().CreateAlgorithm(header.AlgorithmName); IKeyDerivationFunction kdf = new KeyDerivationFactory().CreateKdf(header.KdfName); try { int cost = int.Parse(header.Cost); byte[] key = kdf.DeriveKeyFromPassword(password, decryptor.ExpectedKeySize, header.Salt, cost); byte[] message = decryptor.Decrypt(cipher, key, header.Nonce); if (string.Equals(CompressionGzip, header.Compression, StringComparison.InvariantCultureIgnoreCase)) { message = CompressUtils.Decompress(message); } return(message); } catch (Exception ex) { throw new CryptoDecryptionException("Could not decrypt cipher, probably because the key is wrong.", ex); } }
/// <inheritdoc/> public byte[] Decrypt(byte[] packedCipher, byte[] key) { if (packedCipher == null) { throw new ArgumentNullException("packedCipher"); } CryptoHeader header; byte[] cipher; CryptoHeaderPacker.UnpackHeaderAndCipher(packedCipher, PackageName, out header, out cipher); ISymmetricEncryptionAlgorithm decryptor = new SymmetricEncryptionAlgorithmFactory().CreateAlgorithm(header.AlgorithmName); try { byte[] truncatedKey = CryptoUtils.TruncateKey(key, decryptor.ExpectedKeySize); byte[] message = decryptor.Decrypt(cipher, truncatedKey, header.Nonce); if (string.Equals(CompressionGzip, header.Compression, StringComparison.InvariantCultureIgnoreCase)) { message = CompressUtils.Decompress(message); } return(message); } catch (Exception ex) { throw new CryptoDecryptionException("Could not decrypt cipher, probably because the key is wrong.", ex); } }
/// <summary> /// Decrypts a cipher, which was encrypted with <see cref="Encrypt(byte[],string,KeyDerivationCostType,ICryptoRandomSource,string,string)"/>. /// </summary> /// <param name="packedCipher">The cipher containing a header with the /// necessary parameters for decryption.</param> /// <param name="password">The password which was used for encryption.</param> /// <returns>The plain text message.</returns> public byte[] Decrypt(byte[] packedCipher, string password) { if (packedCipher == null) { throw new ArgumentNullException("packedCipher"); } CryptoHeader header; byte[] cipher; CryptoHeaderPacker.UnpackHeaderAndCipher(packedCipher, out header, out cipher); if (_appName != header.AppName) { throw new CryptoExceptionInvalidCipherFormat(); } ISymmetricEncryptionAlgorithm decryptor = new SymmetricEncryptionAlgorithmFactory().CreateAlgorithm(header.AlgorithmName); IKeyDerivationFunction kdf = new KeyDerivationFactory().CreateKdf(header.KdfName); try { int cost = int.Parse(header.Cost); byte[] key = kdf.DeriveKeyFromPassword(password, decryptor.ExpectedKeySize, header.Salt, cost); byte[] message = decryptor.Decrypt(cipher, key, header.Nonce); return(message); } catch (Exception ex) { throw new CryptoDecryptionException("Could not decrypt cipher, probably because the key is wrong.", ex); } }