NET PGP Library
DecryptStreamAsync Method
Example 



KellermanSoftware.NetPgpLibrary Namespace > PgpEncryptorDecryptor Class : DecryptStreamAsync Method
The encrypted source stream
The decrypted destination stream
Decrypt a stream asynchronously
Syntax
'Declaration
 
<AsyncStateMachineAttribute(KellermanSoftware.NetPgpLibrary.PgpEncryptorDecryptor/d__19)> 
<DebuggerStepThroughAttribute()> 
Public Function DecryptStreamAsync( _ 
   ByVal encryptedSourceStream As Stream, _ 
   ByVal decryptedOutStream As Stream, _ 
   ByVal armoredPrivateKey As String, _ 
   ByVal privateKeyPassword As String, _ 
   Optional ByVal armorEncoding As Encoding _ 
) As Task
'Usage
 
Dim instance As PgpEncryptorDecryptor
Dim encryptedSourceStream As Stream
Dim decryptedOutStream As Stream
Dim armoredPrivateKey As String
Dim privateKeyPassword As String
Dim armorEncoding As Encoding
Dim value As Task
 
value = instance.DecryptStreamAsync(encryptedSourceStream, decryptedOutStream, armoredPrivateKey, privateKeyPassword, armorEncoding)
[AsyncStateMachine(KellermanSoftware.NetPgpLibrary.PgpEncryptorDecryptor/d__19)] 
[DebuggerStepThrough()] 
public Task DecryptStreamAsync( 
   Stream encryptedSourceStream, 
   Stream decryptedOutStream, 
   string armoredPrivateKey, 
   string privateKeyPassword, 
   Encoding armorEncoding 
)
[AsyncStateMachine(KellermanSoftware.NetPgpLibrary.PgpEncryptorDecryptor/d__19)] 
[DebuggerStepThrough()] 
public: 
Task^ DecryptStreamAsync( 
   Stream^ encryptedSourceStream, 
   Stream^ decryptedOutStream, 
   String^ armoredPrivateKey, 
   String^ privateKeyPassword, 
   Encoding^ armorEncoding 
) 

Parameters

encryptedSourceStream
The encrypted source stream
decryptedOutStream
The decrypted destination stream
armoredPrivateKey
privateKeyPassword
armorEncoding
Example
PgpEncryptorDecryptor pgpEncryptorDecryptor = new PgpEncryptorDecryptor(); //Trial Mode
//PgpEncryptorDecryptor pgpEncryptorDecryptor = new PgpEncryptorDecryptor("place user name here", "place license key here"); //License Mode
 
// we need to generate the keys first if we haven't got them already
string username = "username";
string password = "password";
string directoryForKeys = Directory.GetCurrentDirectory();
string publicKeyFilePath = Path.Combine(directoryForKeys, "public.asc");
string privateKeyFilePath = Path.Combine(directoryForKeys, "private.asc");
 
pgpEncryptorDecryptor.GenerateKeyPairFiles(username, password, publicKeyFilePath, privateKeyFilePath);
 
//Create a test file
string directoryForFiles = Directory.GetCurrentDirectory();
string inputFilePath = Path.Combine(directoryForFiles, "input.txt");
File.WriteAllText(inputFilePath, "This is a test");
 
string armoredPrivateKey = File.ReadAllText(privateKeyFilePath);
 
//encrypt the stream
bool armor = true;
using (var clearStream = File.OpenRead(inputFilePath))
{
    using (var cryptoStream = new MemoryStream())
    {
        using (var publicKeyStream = File.OpenRead(publicKeyFilePath))
        {
            await pgpEncryptorDecryptor.EncryptStreamAsync(clearStream, cryptoStream, publicKeyStream, armor);
 
            // decrypt the stream
            using (var decryptedStream = new MemoryStream())
            {
                await pgpEncryptorDecryptor.DecryptStreamAsync(cryptoStream, decryptedStream, armoredPrivateKey, "password");
            }
        }
    }
}
Option Infer On
 
Dim pgpEncryptorDecryptor As New PgpEncryptorDecryptor() 'Trial Mode
'Dim pgpEncryptorDecryptorLicensed As New PgpEncryptorDecryptor("place user name here", "place license key here") //License Mode
 
' we need to generate the keys first if we haven't got them already
Dim username As String = "username"
Dim password As String = "password"
Dim directoryForKeys As String = Directory.GetCurrentDirectory()
Dim publicKeyFilePath As String = Path.Combine(directoryForKeys, "public.asc")
Dim privateKeyFilePath As String = Path.Combine(directoryForKeys, "private.asc")
 
pgpEncryptorDecryptor.GenerateKeyPairFiles(username, password, publicKeyFilePath, privateKeyFilePath)
 
'Create a test file
Dim directoryForFiles As String = Directory.GetCurrentDirectory()
Dim inputFilePath As String = Path.Combine(directoryForFiles, "input.txt")
File.WriteAllText(inputFilePath, "This is a test")
 
Dim armoredPrivateKey As String = File.ReadAllText(privateKeyFilePath)
 
'encrypt the stream
Dim armor As Boolean = True
Using clearStream = File.OpenRead(inputFilePath)
    Using cryptoStream = New MemoryStream()
        Using publicKeyStream = File.OpenRead(publicKeyFilePath)
            Await pgpEncryptorDecryptor.EncryptStreamAsync(clearStream, cryptoStream, publicKeyStream, armor)
 
            ' decrypt the stream
            Using decryptedStream = New MemoryStream()
                Await pgpEncryptorDecryptor.DecryptStreamAsync(cryptoStream, decryptedStream, armoredPrivateKey, "password")
            End Using
        End Using
    End Using
End Using
Requirements

Target Platforms: Windows 7, Windows Vista SP1 or later, Windows XP SP3, Windows Server 2008 (Server Core not supported), Windows Server 2008 R2 (Server Core supported with SP1 or later), Windows Server 2003 SP2

See Also

Reference

PgpEncryptorDecryptor Class
PgpEncryptorDecryptor Members