-
Notifications
You must be signed in to change notification settings - Fork 2
/
Encryption.php
161 lines (142 loc) · 4.64 KB
/
Encryption.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
<?php
/**
* Encryption allows to do the following operations:
* - encrypt pages and/or sections
* - decrypt pages and/or sections
*
* PHP version 5
*
* @category Encryption
* @package PageProtectionPlus
* @author Pawel Wilk <pw@gnu.org>
* @copyright 2006, 2007 Pawel Wilk
* @license http://www.gnu.org/licenses/gpl.html General Public License version 2 or higher
* @version 2.3b
* @link http://www.mediawiki.org/wiki/Extension:PPP
*/
require_once("Cipher.php");
require_once("KeyHeap.php");
require_once("CryptoParser.php");
require_once("engines/RandKeyGen.php");
/**
* Handles encryption.
*/
class Encryption extends Cipher
{
private $mPEMKeys;
private $mParser;
/**
* Constructor. Prepares ciphersuite, parser
* and reads keypairs from files.
*/
function Encryption()
{
$this->Cipher();
$this->mPEMKeys = new KeyHeap('', $this);
$this->mParser = new CryptoParser();
}
/**
* Decrypts a text according to its headers.
* @param text Encrypted text.
* @return Plaintext.
*/
public function Decrypt($text) {
$key_id = false;
$this->mParser->LoadCryptogram($text);
switch ($this->mParser->mHeaderType) {
case HEADER_CORRUPTED:
return false;
case HEADER_NOT_FOUND:
$keypair = $this->mPEMKeys->GetKeyPair();
return $this->Decipher($this->mParser->mText, 'rsa', $keypair);
case HEADER_NATIVE:
if ($this->type($this->mParser->mTextpartCipherName) === 'symmetric') {
$key_id = $this->mParser->mKeypartCipherKeyID;
$keypair = $this->mPEMKeys->GetKeyPair($key_id);
if ($this->mParser->mTextpartCipherMode !== 'cbc') {
throw new Exception("Decrypt: Unsupported symmetric cipher mode: " .
$this->mParser->mTextpartCipherMode);
return false;
}
$keydata = $this->Decipher (
$this->mParser->mTextpartKeyData,
$this->mParser->mKeypartCipherName,
$keypair
);
$key = substr($keydata, 0, $this->mParser->mTextpartKeySize);
$iv = substr($keydata, $this->mParser->mTextpartKeySize, $this->mParser->mTextpartIVSize);
return $this->Decipher (
$this->mParser->mText,
$this->mParser->mTextpartCipherName,
$key,
$iv
);
} else {
/* pure-RSA encoded text */
$keypair_id = $this->mParser->mKeypartCipherKeyID;
$keypair = $this->mPEMKeys->GetKeyPair($keypair_id);
return $this->Decipher (
$this->mParser->mText,
$this->mParser->mTextpartCipherName,
$keypair);
}
}
throw new Exception("Decrypt: unknown header type");
return false;
}
/**
* Encrypts a text using given cipher or default.
* @param text Plaintext.
* @return Encrypted text with RSA-encrypted key in the header.
*/
function Encrypt($text, $cipher='')
{
if ($cipher !== '') {
$cipher = $this->correct_cipher_name($cipher);
if ( !$this->VerifyCipher($cipher, true) ) {
$cipher = '';
}
}
$sym_mode = 'cbc';
/* check whether it is symmetric cipher */
if ($cipher === '' || $this->type($cipher) !== 'asymmetric') {
/* encrypt text using choosen cipher */
$text = $this->Encipher($text, $cipher);
if (!$text) {
return false;
}
$salt = sha1(base64_encode(microtime()) . mt_rand(0,111111));
$salt = substr($salt, 0, mt_rand(7,35));
$keypart_data = $this->mLastKey . $this->mLastIV . $salt;
$textpart_cipher = $this->mLastCipher;
$textpart_key_size = $this->mLastKeySize;
$textpart_iv_size = $this->mLastIVSize;
/* encrypt symmetric key using asymmetric cipher */
$key_pair = $this->mPEMKeys->GetDefaultKeyPair();
$key_pair_id = $this->mPEMKeys->GetDefaultKeyID();
$keypart_data = $this->Encipher($keypart_data, 'rsa', $key_pair);
$keypart_cipher = $this->mLastCipher;
/* create cryptogram */
if (!$key_pair) { throw new Exception("Encrypt: no asymmetric key found"); }
if (!$keypart_data) { throw new Exception("Encrypt: symmetric key encryption error"); }
}
/* check whether it's a pure-asymmetric cipher */
if ($this->type($cipher) === 'asymmetric') {
$sym_mode = '';
$keypart_data = '';
$keypart_cipher = 'NULL';
$textpart_cipher = 'rsa';
$key_pair = $this->mPEMKeys->GetLiteKeyPair();
$key_pair_id = $this->mPEMKeys->GetLiteKeyID();
$text = $this->Encipher($text, $textpart_cipher, $key_pair);
if (!$text) { return false; }
$textpart_key_size = $this->mLastKeySize;
$textpart_iv_size = $this->mLastIVSize;
}
$ret = $this->mParser->CreateCryptogram($text, $keypart_data, $keypart_cipher, $key_pair_id,
$textpart_cipher, $sym_mode, $textpart_key_size,
$textpart_iv_size);
return $ret;
}
}
?>