EncryptedPackageEnvelope.CreateFromPackage Method (Stream, Stream, PublishLicense, CryptoProvider)


The .NET API Reference documentation has a new home. Visit the .NET API Browser on docs.microsoft.com to see the new experience.

Creates and returns an EncryptedPackageEnvelope (on the specified stream) that uses the specified unencrypted package as its content.

Namespace:   System.IO.Packaging
Assembly:  WindowsBase (in WindowsBase.dll)

Public Shared Function CreateFromPackage (
	envelopeStream As Stream,
	packageStream As Stream,
	publishLicense As PublishLicense,
	cryptoProvider As CryptoProvider
) As EncryptedPackageEnvelope


Type: System.IO.Stream

The stream on which to create the file.

Type: System.IO.Stream

The stream representing the existing unencrypted package.

Type: System.Security.RightsManagement.PublishLicense

The publish license that is embedded in the compound file.

Type: System.Security.RightsManagement.CryptoProvider

An object that determines what operations the current user is allowed to perform on the encrypted content.

Exception Condition

envelopeStream or packageStream is null.


envelopeStream does not provide read/write access.

The current user has read/write access to the new file.

The following example shows how to use the CreateFromPackage method to instantiate an EncryptedPackageEnvelope.

WriteStatus("   Signing the UnsignedPublishLicense" & vbLf & "       to build the PublishLicense.")
            Dim authorsUseLicense As UseLicense = Nothing
Dim publishLicense As PublishLicense = unsignedLicense.Sign(_secureEnv, authorsUseLicense)

WriteStatus("   Binding the author's UseLicense and")
WriteStatus("       obtaining the CryptoProvider.")
Dim cryptoProvider As CryptoProvider = authorsUseLicense.Bind(_secureEnv)

WriteStatus("   Creating the EncryptedPackage.")
Dim packageStream As Stream = File.OpenRead(packageFile)
Dim ePackage As EncryptedPackageEnvelope = EncryptedPackageEnvelope.CreateFromPackage(encryptedFile, packageStream, publishLicense, cryptoProvider)

WriteStatus("   Adding an author's UseLicense.")
Dim rmi As RightsManagementInformation = ePackage.RightsManagementInformation
rmi.SaveUseLicense(author, authorsUseLicense)

WriteStatus("   Done - Package encryption complete.")

WriteStatus("Verifying package encryption.")
If EncryptedPackageEnvelope.IsEncryptedPackageEnvelope(encryptedFile) Then
	WriteStatus("   Confirmed - '" & encryptedFilename & "' is encrypted.")
	MessageBox.Show("ERROR: '" & encryptedFilename & "' is NOT ENCRYPTED.", "Encryption Error", MessageBoxButton.OK, MessageBoxImage.Error)
	WriteStatus("ERROR: '" & encryptedFilename & "' is NOT ENCRYPTED." & vbLf)
	Return False
End If

.NET Framework
Available since 3.0
Return to top