41
41
use phpseclib3 \Crypt \Random ;
42
42
use phpseclib3 \Crypt \Common \SymmetricKey as Base ;
43
43
use phpseclib3 \Common \Functions \Strings ;
44
+ use phpseclib3 \Exception \InsufficientSetupException ;
44
45
45
46
if (!defined ('MCRYPT_MODE_ECB ' )) {
46
47
/**#@+
@@ -338,8 +339,6 @@ function phpseclib_mcrypt_module_open($algorithm, $algorithm_directory, $mode, $
338
339
339
340
$ cipher ->disablePadding ();
340
341
341
- $ cipher ->key = null ;
342
-
343
342
return $ cipher ;
344
343
}
345
344
@@ -696,15 +695,6 @@ function phpseclib_mcrypt_generic_init(Base $td, $key, $iv)
696
695
*/
697
696
function phpseclib_mcrypt_generic_helper (Base $ td , &$ data , $ op )
698
697
{
699
- // in the orig mcrypt, if mcrypt_generic_init() was called and an empty key was provided you'd get the following error:
700
- // Warning: mcrypt_generic(): supplied resource is not a valid MCrypt resource
701
- // that error doesn't really make a lot of sense in this context since $td is not a resource nor should it be one.
702
- // in light of that we'll just display the same error that you get when you don't call mcrypt_generic_init() at all
703
- if (!isset ($ td ->key )) {
704
- trigger_error ('m ' . $ op . '_generic(): Operation disallowed prior to mcrypt_generic_init(). ' , E_USER_WARNING );
705
- return false ;
706
- }
707
-
708
698
// phpseclib does not currently provide a way to retrieve the mode once it has been set via "public" methods
709
699
if (phpseclib_mcrypt_enc_is_block_mode ($ td )) {
710
700
$ block_length = phpseclib_mcrypt_enc_get_iv_size ($ td );
@@ -714,7 +704,16 @@ function phpseclib_mcrypt_generic_helper(Base $td, &$data, $op)
714
704
}
715
705
}
716
706
717
- return $ op == 'crypt ' ? $ td ->encrypt ($ data ) : $ td ->decrypt ($ data );
707
+ try {
708
+ return $ op == 'crypt ' ? $ td ->encrypt ($ data ) : $ td ->decrypt ($ data );
709
+ } catch (InsufficientSetupException $ e ) {
710
+ // in the orig mcrypt, if mcrypt_generic_init() was called and an empty key was provided you'd get the following error:
711
+ // Warning: mcrypt_generic(): supplied resource is not a valid MCrypt resource
712
+ // that error doesn't really make a lot of sense in this context since $td is not a resource nor should it be one.
713
+ // in light of that we'll just display the same error that you get when you don't call mcrypt_generic_init() at all
714
+ trigger_error ('m ' . $ op . '_generic(): Operation disallowed prior to mcrypt_generic_init(). ' , E_USER_WARNING );
715
+ return false ;
716
+ }
718
717
}
719
718
720
719
/**
@@ -774,15 +773,18 @@ function phpseclib_mdecrypt_generic(Base $td, $data)
774
773
* @return bool
775
774
* @access public
776
775
*/
777
- function phpseclib_mcrypt_generic_deinit (Base $ td )
776
+ function phpseclib_mcrypt_generic_deinit (Base & $ td )
778
777
{
779
- if (!isset ($ td ->key )) {
778
+ $ reflectionObject = new \ReflectionObject ($ td );
779
+ $ reflectionProperty = $ reflectionObject ->getProperty ('key ' );
780
+ $ reflectionProperty ->setAccessible (true ); // can be dropped in PHP 8.1.0+
781
+ if (!strlen ($ reflectionProperty ->getValue ($ td ))) {
780
782
trigger_error ('mcrypt_generic_deinit(): Could not terminate encryption specifier ' , E_USER_WARNING );
781
783
return false ;
782
784
}
783
785
784
- $ td -> disableContinuousBuffer ( );
785
- $ td-> key = null ;
786
+ $ class = get_class ( $ td );
787
+ $ td = new $ class ( $ td -> getMode ()) ;
786
788
return true ;
787
789
}
788
790
@@ -797,7 +799,7 @@ function phpseclib_mcrypt_generic_deinit(Base $td)
797
799
*/
798
800
function phpseclib_mcrypt_module_close (Base $ td )
799
801
{
800
- $ td ->key = null ;
802
+ // $td->key = null;
801
803
return true ;
802
804
}
803
805
@@ -1300,7 +1302,7 @@ function mcrypt_generic(Base $td, $data)
1300
1302
return phpseclib_mcrypt_generic ($ td , $ data );
1301
1303
}
1302
1304
1303
- function mcrypt_generic_deinit (Base $ td )
1305
+ function mcrypt_generic_deinit (Base & $ td )
1304
1306
{
1305
1307
return phpseclib_mcrypt_generic_deinit ($ td );
1306
1308
}
0 commit comments