Throws(GeneralSecurityException::class, EncryptionUnsupportedByProductException::class)
private fun calculateUValue( generalKey: ByteArray, firstDocIdValue: ByteArray?, revision: Int ): ByteArray? { return if (revision == 2) { val rc4: Cipher = createRC4Cipher() val key: SecretKey = createRC4Key(generalKey) initEncryption(rc4, key) crypt(rc4, PW_PADDING) } else if (revision >= 3) { val md5: MessageDigest = createMD5Digest() md5.update(PW_PADDING) if (firstDocIdValue != null) { md5.update(firstDocIdValue) } val hash: ByteArray = md5.digest() val rc4: Cipher = createRC4Cipher() val key: SecretKey = createRC4Key(generalKey) initEncryption(rc4, key) val v: ByteArray = crypt(rc4, hash) rc4shuffle(v, generalKey, rc4) assert(v.size == 16) val entryValue = ByteArray(32) System.arraycopy(v, 0, entryValue, 0, v.size) System.arraycopy(v, 0, entryValue, 16, v.size) entryValue } else { throw EncryptionUnsupportedByProductException("Unsupported standard security handler revision $revision") } }
Calculate what the U value should consist of given a particular key and document configuration . Correponds to Algorithms 3.4 and 3.5 of the PDF Reference version 1.7