/// <summary> /// Computes the O(owner) value in the encryption dictionary. /// </summary> /// <remarks> /// Corresponds to algorithm 3.3 on page 69 of the PDF specficiation. /// </remarks> /// <param name="options"> /// The user supplied PDF options that provides access to the passwords. /// </param> private void CreateOwnerEntry(SecurityOptions options) { // Pad or truncate the owner password string. // If there is no owner password use the user password instead. string password = options.OwnerPassword; if (password == null) { password = options.UserPassword; } byte[] paddedPassword = PadPassword(password); // Create an MD5 hash from the padded password. // The first 5 bytes of this hash will be used as an ARC4 key. MD5 md5 = MD5.Create(); byte[] hash = md5.ComputeHash(paddedPassword); // Pad or truncate the user password string. byte[] paddedUserPassword = PadPassword(options.UserPassword); // Encrypt the padded user password using the key generated above. Arc4 arc4 = new Arc4(hash, 0, 5); ownerEntry = new byte[32]; arc4.Encrypt(paddedUserPassword, ownerEntry); }
/// <summary> /// Computes the master key that is used to encrypt string and stream data /// in the PDF document. /// </summary> /// <param name="options"> /// The user supplied PDF options that provides access to the passwords and /// the access permissions. /// </param> /// <param name="fileId"> /// The PDF document's file identifier (see section 8.3 of PDF specification). /// </param> private void CreateMasterKey(SecurityOptions options, FileIdentifier fileId) { masterKey = ComputeEncryptionKey32( PadPassword(options.UserPassword), ownerEntry, options.Permissions, fileId.CreatedPart); }
/// <summary> /// Computes the U(user) value in the encryption dictionary. /// </summary> /// <remarks> /// Corresponds to algorithm 3.4 on page 70 of the PDF specficiation. /// </remarks> /// <param name="options"> /// The user supplied PDF options that provides access to the passwords. /// </param> private void CreateUserEntry(SecurityOptions options) { // Encrypt the 32-byte padding string using the master key. Arc4 arc4 = new Arc4(masterKey); userEntry = new byte[32]; arc4.Encrypt(Padding, userEntry); }
/// <summary> /// Constructs a new standard security manager. /// </summary> /// <param name="options"> /// The user supplied PDF options that provides access to the passwords and /// the access permissions. /// </param> /// <param name="fileId"> /// The PDF document's file identifier (see section 8.3 of PDF specification). /// </param> public SecurityManager(SecurityOptions options, FileIdentifier fileId) { // note: The order that these keys are created is important. CreateOwnerEntry(options); CreateMasterKey(options, fileId); // requires the owner entry CreateUserEntry(options); // requires the master key this.permissions = options.Permissions; }
public void SetOptions(PdfRendererOptions options) { // Configure the /Info dictionary. info = new PdfInfo(doc.NextObjectId()); if (options.Title != null) { info.Title = new PdfString(options.Title); } if (options.Author != null) { info.Author = new PdfString(options.Author); } if (options.Subject != null) { info.Subject = new PdfString(options.Subject); } if (options.Keywords != String.Empty) { info.Keywords = new PdfString(options.Keywords); } if (options.Creator != null) { info.Creator = new PdfString(options.Creator); } if (options.Producer != null) { info.Producer = new PdfString(options.Producer); } info.CreationDate = new PdfString(PdfDate.Format(DateTime.Now)); this.objects.Add(info); // Configure the security options. if (options.UserPassword != null || options.OwnerPassword != null || options.HasPermissions) { SecurityOptions securityOptions = new SecurityOptions(); securityOptions.UserPassword = options.UserPassword; securityOptions.OwnerPassword = options.OwnerPassword; securityOptions.EnableAdding(options.EnableAdd); securityOptions.EnableChanging(options.EnableModify); securityOptions.EnableCopying(options.EnableCopy); securityOptions.EnablePrinting(options.EnablePrinting); doc.SecurityOptions = securityOptions; encrypt = doc.Writer.SecurityManager.GetEncrypt(doc.NextObjectId()); this.objects.Add(encrypt); } }