3 require_once dirname(__FILE__).
'/openpgp.php';
4 @include_once dirname(__FILE__).
'/openpgp_crypt_rsa.php';
5 @include_once dirname(__FILE__).
'/openpgp_mcrypt_wrapper.php';
6 @include_once
'Crypt/AES.php';
7 @include_once
'Crypt/TripleDES.php';
8 require_once
'Crypt/Random.php';
11 public static function encrypt($passphrases_and_keys, $message, $symmetric_algorithm=9) {
12 list($cipher, $key_bytes, $key_block_bytes) = self::getCipher($symmetric_algorithm);
13 if(!$cipher)
throw new Exception(
"Unsupported cipher");
14 $prefix = crypt_random_string($key_block_bytes);
15 $prefix .= substr($prefix, -2);
17 $key = crypt_random_string($key_bytes);
18 $cipher->setKey($key);
20 $to_encrypt = $prefix . $message->to_bytes();
22 $to_encrypt .= $mdc->to_bytes();
25 if(!is_array($passphrases_and_keys) && !($passphrases_and_keys instanceof IteratorAggregate)) {
26 $passphrases_and_keys = (array)$passphrases_and_keys;
29 foreach($passphrases_and_keys as $pass) {
31 if(!in_array($pass->algorithm, array(1,2,3)))
throw new Exception(
"Only RSA keys are supported.");
33 $rsa = $crypt_rsa->public_key();
34 $rsa->setEncryptionMode(CRYPT_RSA_ENCRYPTION_PKCS1);
35 $esk = $rsa->encrypt(chr($symmetric_algorithm) . $key . pack(
'n', self::checksum($key)));
38 }
else if(is_string($pass)) {
40 $cipher->setKey($s2k->make_key($pass, $key_bytes));
41 $esk = $cipher->encrypt(chr($symmetric_algorithm) . $key);
50 $epacket = self::getEncryptedData($m);
54 if(strlen($p->encrypted_data) > 0) {
55 list($cipher, $key_bytes, $key_block_bytes) = self::getCipher($p->symmetric_algorithm);
56 if(!$cipher)
continue;
57 $cipher->setKey($p->s2k->make_key($pass, $key_bytes));
59 $padAmount = $key_block_bytes - (strlen($p->encrypted_data) % $key_block_bytes);
60 $data = substr($cipher->decrypt($p->encrypted_data . str_repeat(
"\0", $padAmount)), 0, strlen($p->encrypted_data));
61 $decrypted = self::decryptPacket($epacket, ord($data{0}), substr($data, 1));
63 list($cipher, $key_bytes, $key_block_bytes) = self::getCipher($p->symmetric_algorithm);
64 $decrypted = self::decryptPacket($epacket, $p->symmetric_algorithm, $p->s2k->make_key($pass, $key_bytes));
67 if($decrypted)
return $decrypted;
75 $packet = clone $packet;
77 list($cipher, $key_bytes, $key_block_bytes) = self::getCipher($packet->symmetric_algorithm);
78 if(!$cipher)
throw new Exception(
"Unsupported cipher");
79 $cipher->setKey($packet->s2k->make_key($pass, $key_bytes));
80 $cipher->setIV(substr($packet->encrypted_data, 0, $key_block_bytes));
81 $material = $cipher->decrypt(substr($packet->encrypted_data, $key_block_bytes));
83 if($packet->s2k_useage == 254) {
84 $chk = substr($material, -20);
85 $material = substr($material, 0, -20);
86 if($chk != hash(
'sha1', $material,
true))
return NULL;
88 $chk = unpack(
'n', substr($material, -2));
90 $material = substr($material, 0, -2);
92 $mkChk = self::checksum($material);
93 if($chk != $mkChk)
return NULL;
96 $packet->s2k_useage = 0;
97 $packet->symmetric_algorithm = 0;
98 $packet->encrypted_data = NULL;
99 $packet->input = $material;
100 $packet->key_from_input();
101 unset($packet->input);
105 public static function decryptPacket($epacket, $symmetric_algorithm, $key) {
106 list($cipher, $key_bytes, $key_block_bytes) = self::getCipher($symmetric_algorithm);
107 if(!$cipher)
return NULL;
108 $cipher->setKey($key);
111 $padAmount = $key_block_bytes - (strlen($epacket->data) % $key_block_bytes);
112 $data = substr($cipher->decrypt($epacket->data . str_repeat(
"\0", $padAmount)), 0, strlen($epacket->data));
113 $prefix = substr($data, 0, $key_block_bytes + 2);
114 $mdc = substr(substr($data, -22, 22), 2);
115 $data = substr($data, $key_block_bytes + 2, -22);
117 $mkMDC = hash(
"sha1", $prefix . $data .
"\xD3\x14",
true);
118 if($mkMDC !== $mdc)
return false;
122 }
catch (Exception $ex) { $msg = NULL; }
123 if($msg)
return $msg;
126 $iv = substr($epacket->data, 2, $key_block_bytes);
127 $edata = substr($epacket->data, $key_block_bytes + 2);
128 $padAmount = $key_block_bytes - (strlen($edata) % $key_block_bytes);
131 $data = substr($cipher->decrypt($edata . str_repeat(
"\0", $padAmount)), 0, strlen($edata));
135 }
catch (Exception $ex) { $msg = NULL; }
136 if($msg)
return $msg;
146 if(class_exists(
'Crypt_TripleDES')) {
147 $cipher =
new Crypt_TripleDES(CRYPT_DES_MODE_CFB);
149 $key_block_bytes = 8;
153 if(defined(
'MCRYPT_CAST_128')) {
154 $cipher =
new MCryptWrapper(MCRYPT_CAST_128);
158 if(class_exists(
'Crypt_AES')) {
159 $cipher =
new Crypt_AES(CRYPT_AES_MODE_CFB);
160 $cipher->setKeyLength(128);
164 if(class_exists(
'Crypt_AES')) {
165 $cipher =
new Crypt_AES(CRYPT_AES_MODE_CFB);
166 $cipher->setKeyLength(192);
170 if(class_exists(
'Crypt_AES')) {
171 $cipher =
new Crypt_AES(CRYPT_AES_MODE_CFB);
172 $cipher->setKeyLength(256);
176 if(!$cipher)
return array(NULL, NULL, NULL);
177 if(!isset($key_bytes)) $key_bytes = $cipher->key_size;
178 if(!isset($key_block_bytes)) $key_block_bytes = $cipher->block_size;
179 return array($cipher, $key_bytes, $key_block_bytes);
186 throw new Exception(
"Can only decrypt EncryptedDataPacket");
191 for($i = 0; $i < strlen($s); $i++) {
192 $mkChk = ($mkChk + ord($s{$i})) % 65536;