@@ -19,9 +19,9 @@ public function testAESBasicSuccess()
19
19
// a plaintext / ciphertext of length 1 is of an insufficient length for cbc mode
20
20
$ plaintext = str_repeat ('a ' , 16 );
21
21
22
- $ mcrypt = bin2hex ( mcrypt_encrypt ('rijndael-128 ' , $ key , $ plaintext , 'cbc ' , $ iv) );
23
- $ compat = bin2hex ( phpseclib_mcrypt_encrypt ('rijndael-128 ' , $ key , $ plaintext , 'cbc ' , $ iv) );
24
- $ this ->assertEquals ($ mcrypt, $ compat );
22
+ $ mcrypt = mcrypt_encrypt ('rijndael-128 ' , $ key , $ plaintext , 'cbc ' , $ iv );
23
+ $ compat = phpseclib_mcrypt_encrypt ('rijndael-128 ' , $ key , $ plaintext , 'cbc ' , $ iv );
24
+ $ this ->assertEquals (bin2hex ( $ mcrypt), bin2hex ( $ compat) );
25
25
26
26
$ ciphertext = $ mcrypt ;
27
27
@@ -112,26 +112,59 @@ public function testNullPadding()
112
112
public function testStream ()
113
113
{
114
114
$ passphrase = 'My secret ' ;
115
+ $ plaintext = 'Secret secret secret data ' ;
116
+
117
+ $ iv = substr (md5 ('iv ' . $ passphrase , true ), 0 , 8 );
118
+ $ key = substr (md5 ('pass1 ' . $ passphrase , true ) .
119
+ md5 ('pass2 ' . $ passphrase , true ), 0 , 24 );
120
+ $ opts = array ('iv ' => $ iv , 'key ' => $ key );
115
121
116
- $ iv = substr (md5 ('iv ' .$ passphrase , true ), 0 , 8 );
117
- $ key = substr (md5 ('pass1 ' .$ passphrase , true ) .
118
- md5 ('pass2 ' .$ passphrase , true ), 0 , 24 );
119
- $ opts = array ('iv ' =>$ iv , 'key ' =>$ key );
122
+ $ expected = substr ($ plaintext . $ plaintext , 0 , 48 );
120
123
121
124
$ fp = fopen ('php://memory ' , 'wb+ ' );
122
125
stream_filter_append ($ fp , 'mcrypt.tripledes ' , STREAM_FILTER_WRITE , $ opts );
123
- fwrite ($ fp , 'Secret secret secret data ' );
126
+ fwrite ($ fp , $ plaintext . $ plaintext );
127
+ rewind ($ fp );
128
+ $ reference = bin2hex (fread ($ fp , 1024 ));
129
+ fclose ($ fp );
130
+
131
+ $ fp = fopen ('php://memory ' , 'wb+ ' );
132
+ stream_filter_append ($ fp , 'mcrypt.tripledes ' , STREAM_FILTER_WRITE , $ opts );
133
+ fwrite ($ fp , $ plaintext );
134
+ fwrite ($ fp , $ plaintext );
124
135
rewind ($ fp );
125
136
$ mcrypt = bin2hex (fread ($ fp , 1024 ));
137
+ stream_filter_append ($ fp , 'mdecrypt.tripledes ' , STREAM_FILTER_READ , $ opts );
138
+ rewind ($ fp );
139
+ $ decrypted = fread ($ fp , 1024 );
126
140
fclose ($ fp );
127
141
142
+ // this demonstrates that streams operate in continuous mode
143
+ $ this ->assertEquals ($ reference , $ mcrypt );
144
+
145
+ // this demonstrates how to decrypt encrypted data
146
+ $ this ->assertEquals ($ expected , $ decrypted );
147
+
128
148
$ fp = fopen ('php://memory ' , 'wb+ ' );
129
149
stream_filter_append ($ fp , 'phpseclib.mcrypt.tripledes ' , STREAM_FILTER_WRITE , $ opts );
130
- fwrite ($ fp , 'Secret secret secret data ' );
150
+ fwrite ($ fp , $ plaintext );
151
+ fwrite ($ fp , $ plaintext );
131
152
rewind ($ fp );
132
153
$ compat = bin2hex (fread ($ fp , 1024 ));
154
+ stream_filter_append ($ fp , 'phpseclib.mdecrypt.tripledes ' , STREAM_FILTER_READ , $ opts );
155
+ rewind ($ fp );
156
+ $ decrypted = fread ($ fp , 1024 );
133
157
fclose ($ fp );
134
158
159
+ // this demonstrates that mcrypt's stream and phpseclib's stream's have identical output
135
160
$ this ->assertEquals ($ mcrypt , $ compat );
161
+
162
+ // this demonstrates that phpseclib's stream successfully decrypts the encrypted string
163
+ // since both mcrypt and phpseclib successfully decrypt to the same thing the outputs can be assumed to be matching
164
+ $ this ->assertEquals ($ expected , $ decrypted );
165
+
166
+ // in the case of cbc the length is a multiple of the block size. extra characters are added
167
+ // when enough are present for another block to be added
168
+ $ this ->assertNotEquals (strlen ($ mcrypt ), strlen ($ plaintext ) * 2 );
136
169
}
137
170
}
0 commit comments