1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* BaconPdf |
4
|
|
|
* |
5
|
|
|
* @link http://github.com/Bacon/BaconPdf For the canonical source repository |
6
|
|
|
* @copyright 2015 Ben Scholzen (DASPRiD) |
7
|
|
|
* @license http://opensource.org/licenses/BSD-2-Clause Simplified BSD License |
8
|
|
|
*/ |
9
|
|
|
|
10
|
|
|
namespace Bacon\Pdf\Encryption; |
11
|
|
|
|
12
|
|
|
use Bacon\Pdf\Writer\ObjectWriter; |
13
|
|
|
|
14
|
|
|
class Pdf16Encryption extends Pdf14Encryption |
15
|
|
|
{ |
16
|
|
|
/** |
17
|
|
|
* {@inheritdoc} |
18
|
|
|
*/ |
19
|
|
|
protected function writeAdditionalEncryptDictionaryEntries(ObjectWriter $objectWriter) |
20
|
|
|
{ |
21
|
|
|
parent::writeAdditionalEncryptDictionaryEntries($objectWriter); |
22
|
|
|
|
23
|
|
|
$objectWriter->writeName('CF'); |
24
|
|
|
$objectWriter->startDictionary(); |
25
|
|
|
|
26
|
|
|
$objectWriter->writeName('StdCF'); |
27
|
|
|
$objectWriter->startDictionary(); |
28
|
|
|
|
29
|
|
|
$objectWriter->writeName('Type'); |
30
|
|
|
$objectWriter->writeName('CryptFilter'); |
31
|
|
|
|
32
|
|
|
$objectWriter->writeName('CFM'); |
33
|
|
|
$objectWriter->writeName('AESV2'); |
34
|
|
|
|
35
|
|
|
$objectWriter->writeName('Length'); |
36
|
|
|
$objectWriter->writeNumber(128); |
37
|
|
|
|
38
|
|
|
$objectWriter->endDictionary(); |
39
|
|
|
$objectWriter->endDictionary(); |
40
|
|
|
|
41
|
|
|
$objectWriter->writeName('StrF'); |
42
|
|
|
$objectWriter->writeName('StdCF'); |
43
|
|
|
|
44
|
|
|
$objectWriter->writeName('StmF'); |
45
|
|
|
$objectWriter->writeName('StdCF'); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* {@inheritdoc} |
50
|
|
|
*/ |
51
|
|
|
public function encrypt($plaintext, $objectNumber, $generationNumber) |
52
|
|
|
{ |
53
|
|
View Code Duplication |
if (function_exists('random_bytes')) { |
|
|
|
|
54
|
|
|
// As of PHP 7 |
55
|
|
|
$initializationVector = random_bytes(16); |
56
|
|
|
} else { |
57
|
|
|
$initializationVector = ''; |
58
|
|
|
mt_srand(); |
59
|
|
|
|
60
|
|
|
for ($i = 0; $i < 16; ++$i) { |
61
|
|
|
$initializationVector .= chr(mt_rand(0, 255)); |
62
|
|
|
} |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
return $initializationVector . openssl_encrypt( |
66
|
|
|
$plaintext, |
67
|
|
|
'aes-128-cbc', |
68
|
|
|
$this->computeIndividualEncryptionKey($objectNumber, $generationNumber) . "\x73\x41\x6c\x54", |
69
|
|
|
'', |
70
|
|
|
$initializationVector |
71
|
|
|
); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* {@inheritdoc} |
76
|
|
|
*/ |
77
|
|
|
protected function getRevision() |
78
|
|
|
{ |
79
|
|
|
return 4; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* {@inheritdoc} |
84
|
|
|
*/ |
85
|
|
|
protected function getAlgorithm() |
86
|
|
|
{ |
87
|
|
|
return 4; |
88
|
|
|
} |
89
|
|
|
} |
90
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.