EncryptedPackageEnvelope.CreateFromPackage Method (String, 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 that uses the specified unencrypted package as its content, and gives it the specified file name.

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

public static EncryptedPackageEnvelope CreateFromPackage(
	string envelopeFileName,
	Stream packageStream,
	PublishLicense publishLicense,
	CryptoProvider cryptoProvider


Type: System.String

The name of the OLE compound 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

envelopeFileName or packageStream is null.

If a file with the specified name already exists, it is overwritten.

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

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

WriteStatus("   Signing the UnsignedPublishLicense\n" +
            "       to build the PublishLicense.");
UseLicense authorsUseLicense;
PublishLicense publishLicense =
    unsignedLicense.Sign(_secureEnv, out authorsUseLicense);

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

WriteStatus("   Creating the EncryptedPackage.");
Stream packageStream = File.OpenRead(packageFile);
EncryptedPackageEnvelope ePackage =
        packageStream, publishLicense, cryptoProvider);

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

WriteStatus("   Done - Package encryption complete.");

WriteStatus("Verifying package encryption.");
if (EncryptedPackageEnvelope.IsEncryptedPackageEnvelope(encryptedFile))
    WriteStatus("   Confirmed - '" + encryptedFilename +
                "' is encrypted.");
    MessageBox.Show("ERROR: '" + encryptedFilename +
        "' is NOT ENCRYPTED.", "Encryption Error",
        MessageBoxButton.OK, MessageBoxImage.Error);
    WriteStatus("ERROR: '" + encryptedFilename +
                "' is NOT ENCRYPTED.\n");
    return false;

.NET Framework
Available since 3.0
Return to top