/// <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> /// Encrypts the passed byte array using the ARC4 cipher. /// </summary> public byte[] Encrypt(byte[] data, PdfObjectId objectId) { Arc4 arc4 = new Arc4(ComputeEncryptionKey31(masterKey, objectId)); arc4.Encrypt(data, data); return(data); }
/// <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> /// Performs the actual checking of the user password. /// </summary> private static bool CheckUserPassword( byte[] paddedPassword, byte[] userEntry, byte[] ownerEntry, int permissions, byte[] fileId) { // Compute an encryption key from the supplied information. byte[] key = ComputeEncryptionKey32( paddedPassword, ownerEntry, permissions, fileId); // Decrpt the User entry using the key. Arc4 arc4 = new Arc4(key); byte[] decryptedUserEntry = new byte[32]; arc4.Encrypt(userEntry, decryptedUserEntry); // Compare the two arrays, if they match then the // supplied password is correct. return(CompareArray(Padding, decryptedUserEntry)); }
/// <summary> /// Checks the owner password. /// </summary> internal static bool CheckOwnerPassword( string password, byte[] userEntry, byte[] ownerEntry, int permissions, byte[] fileId) { // Compute an encryption key from the supplied information. MD5 md5 = MD5.Create(); byte[] hash = md5.ComputeHash(PadPassword(password)); // Decrpt the Owner entry using the key. Arc4 arc4 = new Arc4(hash, 0, 5); byte[] decryptedOwnerEntry = new byte[32]; arc4.Encrypt(ownerEntry, decryptedOwnerEntry); // The decryped owner entry is the correct user password, // then the owner password is also correct. return(CheckUserPassword(decryptedOwnerEntry, userEntry, ownerEntry, permissions, fileId)); }
/// <summary> /// Checks the owner password. /// </summary> internal static bool CheckOwnerPassword( string password, byte[] userEntry, byte[] ownerEntry, int permissions, byte[] fileId) { // Compute an encryption key from the supplied information. MD5 md5 = MD5.Create(); byte[] hash = md5.ComputeHash(PadPassword(password)); // Decrpt the Owner entry using the key. Arc4 arc4 = new Arc4(hash, 0, 5); byte[] decryptedOwnerEntry = new byte[32]; arc4.Encrypt(ownerEntry, decryptedOwnerEntry); // The decryped owner entry is the correct user password, // then the owner password is also correct. return CheckUserPassword(decryptedOwnerEntry, userEntry, ownerEntry, permissions, fileId); }
/// <summary> /// Performs the actual checking of the user password. /// </summary> private static bool CheckUserPassword( byte[] paddedPassword, byte[] userEntry, byte[] ownerEntry, int permissions, byte[] fileId) { // Compute an encryption key from the supplied information. byte[] key = ComputeEncryptionKey32( paddedPassword, ownerEntry, permissions, fileId); // Decrpt the User entry using the key. Arc4 arc4 = new Arc4(key); byte[] decryptedUserEntry = new byte[32]; arc4.Encrypt(userEntry, decryptedUserEntry); // Compare the two arrays, if they match then the // supplied password is correct. return CompareArray(Padding, decryptedUserEntry); }
/// <summary> /// Encrypts the passed byte array using the ARC4 cipher. /// </summary> public byte[] Encrypt(byte[] data, PdfObjectId objectId) { Arc4 arc4 = new Arc4(ComputeEncryptionKey31(masterKey, objectId)); arc4.Encrypt(data, data); return data; }