Which encryption algorithm and key length does PDFKit use for password-protected PDFs on iOS?

I’m using Apple’s PDFKit to export a password-protected PDF on iOS with PDFDocument.write(to:withOptions:), passing .ownerPasswordOption and .userPasswordOption without setting an encryption method or key length.

Which PDF encryption algorithm, key length, and security-handler revision does PDFKit use for this write path? Is this documented, or can it vary by iOS version or when rewriting an existing PDF? Does PDFKit expose a supported way to select or verify the algorithm and key length? A link to authoritative documentation or guidance on verifying the output of a synthetic test PDF would be helpful.

Answered by DTS Engineer in 918010022

The documentation doesn't say which algorithm PDFKit uses, and in my tests it changed between iOS 26 and iOS 27. I wrote a one-page PDF with write(to:withOptions:), supplying only .ownerPasswordOption and .userPasswordOption as your app does, and read back each file's encryption dictionary:

  • iOS 26.7, on an iPad Pro: the standard security handler with /V 4 /R 4 and a crypt filter whose CFM is /AESV2. That's AES with a 128-bit key.
  • iOS 27.2.1, on an iPhone 18 Pro Max: the standard security handler with /V 2 /R 3, /Length 128, and no crypt filters. That's RC4 with a 128-bit key.

If you encrypt PDFs, set the key length yourself rather than relying on the default. PDFDocument.h says the write methods "may take any CoreGraphics options that are typically passed into CGPDFContextCreate(...)". The write(toFile:withOptions:) documentation points to Auxiliary Dictionary Keys, which lists kCGPDFContextEncryptionKeyLength. That key takes a multiple of 8 from 40 to 128.

In my tests, 128 gave the same result as leaving the key out, so setting 128 keeps the 128-bit key without depending on the default. A value of 40 gave 40-bit RC4. The key length alone doesn't determine the cipher: with 128, iOS 26.7 wrote AES and iOS 27.2.1 wrote RC4. Stay within the documented range. With 256, the write failed in iOS 26.7, and in iOS 27.2.1 it succeeded and wrote a 40-bit key. No public option selects the cipher or AES-256.

There is no API reporting the algorithm used to encrypt a PDF. On PDFDocument, isEncrypted and isLocked tell you whether a document is encrypted and still locked, and unlock(withPassword:) returns whether a password unlocks it.

To find out which algorithm and key length a file uses, you need to look into the file's contents. The encryption dictionary isn't itself encrypted, so you can read it directly. Follow the trailer's Encrypt entry and check V, R, and Length. When V is 4, also check the CFM of the crypt filter named by StmF and StrF. ISO 32000-2 defines these values. Revision 4 uses a CFM of /V2, which is RC4, or /AESV2, which is AES-128. Revision 6 uses /AESV3, which is AES-256. PDF 2.0 deprecates revisions 1 through 5.

In my testing, rewriting an encrypted document without supplying passwords didn't always keep its encryption. In one of my iOS 26.7 tests, the rewritten file wasn't encrypted. Supply the passwords, and the key length if you set one, every time you write.

If your app needs a particular algorithm, please file an enhancement request using Feedback Assistant. Ask for a supported way to choose AES-128 or AES-256 when writing, and to query a document's encryption. Describe how your app uses encrypted PDFs, and include test PDFs written in iOS 26 and iOS 27, because they show the default changing.

If you file the enhancement request, please post the Feedback number here so we can make sure it gets routed to the right team.

Please note that filing an enhancement request does not guarantee Apple will add any requested functionality to Apple products in the future. However, with that said, filing an enhancement request is a good way to get your ideas in front of the folks who make decisions about that sort of thing.

Also note that the observations I've offered from taking apart PDF files and looking at their contents are anecdotal, not official documentation. How Apple's APIs compose the contents of PDF files is internal to the implementation and subject to change without notice at any time.

Accepted Answer

The documentation doesn't say which algorithm PDFKit uses, and in my tests it changed between iOS 26 and iOS 27. I wrote a one-page PDF with write(to:withOptions:), supplying only .ownerPasswordOption and .userPasswordOption as your app does, and read back each file's encryption dictionary:

  • iOS 26.7, on an iPad Pro: the standard security handler with /V 4 /R 4 and a crypt filter whose CFM is /AESV2. That's AES with a 128-bit key.
  • iOS 27.2.1, on an iPhone 18 Pro Max: the standard security handler with /V 2 /R 3, /Length 128, and no crypt filters. That's RC4 with a 128-bit key.

If you encrypt PDFs, set the key length yourself rather than relying on the default. PDFDocument.h says the write methods "may take any CoreGraphics options that are typically passed into CGPDFContextCreate(...)". The write(toFile:withOptions:) documentation points to Auxiliary Dictionary Keys, which lists kCGPDFContextEncryptionKeyLength. That key takes a multiple of 8 from 40 to 128.

In my tests, 128 gave the same result as leaving the key out, so setting 128 keeps the 128-bit key without depending on the default. A value of 40 gave 40-bit RC4. The key length alone doesn't determine the cipher: with 128, iOS 26.7 wrote AES and iOS 27.2.1 wrote RC4. Stay within the documented range. With 256, the write failed in iOS 26.7, and in iOS 27.2.1 it succeeded and wrote a 40-bit key. No public option selects the cipher or AES-256.

There is no API reporting the algorithm used to encrypt a PDF. On PDFDocument, isEncrypted and isLocked tell you whether a document is encrypted and still locked, and unlock(withPassword:) returns whether a password unlocks it.

To find out which algorithm and key length a file uses, you need to look into the file's contents. The encryption dictionary isn't itself encrypted, so you can read it directly. Follow the trailer's Encrypt entry and check V, R, and Length. When V is 4, also check the CFM of the crypt filter named by StmF and StrF. ISO 32000-2 defines these values. Revision 4 uses a CFM of /V2, which is RC4, or /AESV2, which is AES-128. Revision 6 uses /AESV3, which is AES-256. PDF 2.0 deprecates revisions 1 through 5.

In my testing, rewriting an encrypted document without supplying passwords didn't always keep its encryption. In one of my iOS 26.7 tests, the rewritten file wasn't encrypted. Supply the passwords, and the key length if you set one, every time you write.

If your app needs a particular algorithm, please file an enhancement request using Feedback Assistant. Ask for a supported way to choose AES-128 or AES-256 when writing, and to query a document's encryption. Describe how your app uses encrypted PDFs, and include test PDFs written in iOS 26 and iOS 27, because they show the default changing.

If you file the enhancement request, please post the Feedback number here so we can make sure it gets routed to the right team.

Please note that filing an enhancement request does not guarantee Apple will add any requested functionality to Apple products in the future. However, with that said, filing an enhancement request is a good way to get your ideas in front of the folks who make decisions about that sort of thing.

Also note that the observations I've offered from taking apart PDF files and looking at their contents are anecdotal, not official documentation. How Apple's APIs compose the contents of PDF files is internal to the implementation and subject to change without notice at any time.