Skip to content

Commit 3cf1a5e

Browse files
committed
[dev] Implement MDC-using encryption
see #20
1 parent db6055b commit 3cf1a5e

File tree

1 file changed

+7
-20
lines changed

1 file changed

+7
-20
lines changed

libs/GPG.php

Lines changed: 7 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -53,20 +53,6 @@ private function gpg_encrypt($key, $text) {
5353
$rblock[$i] = GPG_Utility::c_random();
5454
}
5555

56-
57-
$iblock = GPG_AES::encrypt($iblock, $ekey);
58-
for($i = 0; $i < $this->width; $i++) {
59-
$ct[$i] = ($iblock[$i] ^= $rblock[$i]);
60-
}
61-
62-
$iblock = GPG_AES::encrypt($iblock, $ekey);
63-
$ct[$this->width] = ($iblock[0] ^ $rblock[$this->width - 2]);
64-
$ct[$this->width + 1] = ($iblock[1] ^ $rblock[$this->width - 1]);
65-
66-
for($i = 0; $i < $this->width + 2; $i++) $cipher .= chr($ct[$i]);
67-
68-
$iblock = array_slice($ct, 2, $this->width + 2);
69-
7056
for($n = 0; $n < strlen($text); $n += $this->width) {
7157
$iblock = GPG_AES::encrypt($iblock, $ekey);
7258
for($i = 0; $i < $this->width; $i++) {
@@ -75,7 +61,7 @@ private function gpg_encrypt($key, $text) {
7561
}
7662
}
7763

78-
return substr($cipher, 0, $len + $this->width + 2);
64+
return substr($cipher, 0, $len);
7965
}
8066

8167
private function gpg_header($tag, $len)
@@ -160,15 +146,16 @@ private function gpg_literal($text)
160146
if (strpos($text, "\r\n") === false)
161147
$text = str_replace("\n", "\r\n", $text);
162148

163-
return
164-
$this->gpg_header(0xac, strlen($text) + 10) . "t" .
165-
chr(4) . "file\0\0\0\0" . $text;
149+
return chr(11 | 0xC0) . chr(255) . $this->writeNumber(strlen($text) + 10, 4) . "t" . chr(4) . "file\0\0\0\0" . $text;
166150
}
167151

168152
private function gpg_data($key, $text)
169153
{
170-
$enc = $this->gpg_encrypt($key, $this->gpg_literal($text));
171-
return $this->gpg_header(0xa4, strlen($enc)) . $enc;
154+
$prefix = GPG_Utility::s_random($this->width, 0);
155+
$prefix .= substr($prefix, -2);
156+
$mdc="\xD3\x14".hash('sha1', $prefix.$this->gpg_literal($text)."\xD3\x14", true);
157+
$enc = $this->gpg_encrypt($key, $prefix.$this->gpg_literal($text).$mdc);
158+
return chr(0x12 | 0xC0) . chr(255) . $this->writeNumber(1+strlen($enc), 4) . chr(1) . $enc;
172159
}
173160

174161
/**

0 commit comments

Comments
 (0)