NET PGP Library
DecryptStream Method
Example 



KellermanSoftware.NetPgpLibrary Namespace > PgpEncryptorDecryptor Class : DecryptStream Method
The encrypted data stream
A stream to output the data
The private key as a string
The password for the private key
Optional parameter, the default is UTF8
Decrypt a stream
Syntax
'Declaration
 
Public Sub DecryptStream( _
   ByVal encryptedData As Stream, _
   ByVal decryptedData As Stream, _
   ByVal armoredPrivateKey As String, _
   ByVal password As String, _
   Optional ByVal armorEncoding As Encoding _
) 
'Usage
 
Dim instance As PgpEncryptorDecryptor
Dim encryptedData As Stream
Dim decryptedData As Stream
Dim armoredPrivateKey As String
Dim password As String
Dim armorEncoding As Encoding
 
instance.DecryptStream(encryptedData, decryptedData, armoredPrivateKey, password, armorEncoding)

Parameters

encryptedData
The encrypted data stream
decryptedData
A stream to output the data
armoredPrivateKey
The private key as a string
password
The password for the private key
armorEncoding
Optional parameter, the default is UTF8
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");
string outputFilePath = Path.Combine(directoryForFiles, "output.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))
        {
            pgpEncryptorDecryptor.EncryptStream(clearStream, cryptoStream, publicKeyStream, armor);
 
            using (FileStream outStream = new FileStream(outputFilePath, FileMode.Create))
            {
                // decrypt the stream
                pgpEncryptorDecryptor.DecryptStream(cryptoStream, outStream, armoredPrivateKey, "password");
            }
        }
    }
}
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")
Dim outputFilePath As String = Path.Combine(directoryForFiles, "output.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)
            pgpEncryptorDecryptor.EncryptStream(clearStream, cryptoStream, publicKeyStream, armor)
 
            Using outStream As New FileStream(outputFilePath, FileMode.Create)
                ' decrypt the stream
                pgpEncryptorDecryptor.DecryptStream(cryptoStream, outStream, 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