NET PGP Library
EncryptStream Method
Example 



KellermanSoftware.NetPgpLibrary Namespace > PgpEncryptorDecryptor Class : EncryptStream Method
The source stream to encrypt
Output to this stream
The stream for the public key
Encyrpt using ASCII Armor
Encrypt a stream
Syntax
'Declaration
 
Public Sub EncryptStream( _
   ByVal sourceStream As Stream, _
   ByVal encryptedOutputStream As Stream, _
   ByVal publicKeyStream As Stream, _
   ByVal armor As Boolean _
) 
'Usage
 
Dim instance As PgpEncryptorDecryptor
Dim sourceStream As Stream
Dim encryptedOutputStream As Stream
Dim publicKeyStream As Stream
Dim armor As Boolean
 
instance.EncryptStream(sourceStream, encryptedOutputStream, publicKeyStream, armor)

Parameters

sourceStream
The source stream to encrypt
encryptedOutputStream
Output to this stream
publicKeyStream
The stream for the public key
armor
Encyrpt using ASCII Armor
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");
 
//Parameters
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);                        
        }
    }
}
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")
 
'Parameters
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)
        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