@@ -773,6 +773,22 @@ function phpseclib_mdecrypt_generic(Base $td, $data)
773
773
return phpseclib_mcrypt_generic_helper ($ td , $ data , 'decrypt ' );
774
774
}
775
775
776
+ /**
777
+ * This function terminates encryption
778
+ *
779
+ * Alias of mcrypt_generic_deinit()
780
+ *
781
+ * @param Base $td
782
+ * @return bool
783
+ * @access public
784
+ */
785
+ function phpseclib_mcrypt_generic_end (Base $ td )
786
+ {
787
+ // https://web.archive.org/web/20180106174656/https://www.php.net/manual/en/function.mcrypt-generic-end.php
788
+
789
+ return phpseclib_mcrypt_generic_deinit ($ td );
790
+ }
791
+
776
792
/**
777
793
* This function deinitializes an encryption module
778
794
*
@@ -962,6 +978,47 @@ function phpseclib_mcrypt_module_self_test($algorithm, $lib_dir = '')
962
978
return in_array ($ algorithm , phpseclib_mcrypt_list_algorithms ());
963
979
}
964
980
981
+ /**
982
+ * Encrypt / decrypt data using pre PHP 5.6.0 behavior
983
+ *
984
+ * Performs checks common to both mcrypt_encrypt and mcrypt_decrypt
985
+ *
986
+ * @param string $cipher
987
+ * @param string $key
988
+ * @param string $data
989
+ * @param string $mode
990
+ * @param string $iv
991
+ * @param string $op
992
+ * @return string|bool
993
+ * @access private
994
+ */
995
+ function phpseclib_mcrypt_helper_old ($ cipher , $ key , $ data , $ mode , $ iv , $ op )
996
+ {
997
+ $ td = @phpseclib_mcrypt_module_open ($ cipher , '' , $ mode , '' );
998
+ phpseclib_set_key ($ td , $ key );
999
+
1000
+ $ iv_size = phpseclib_mcrypt_enc_get_iv_size ($ td );
1001
+ if ($ iv_size && phpseclib_mcrypt_module_is_iv_mode ($ mode )) {
1002
+ if (!isset ($ iv )) {
1003
+ trigger_error (
1004
+ 'mcrypt_ ' . $ op . '(): Attempt to use an empty IV, which is NOT recommended ' ,
1005
+ E_USER_WARNING
1006
+ );
1007
+ $ iv = str_repeat ("\0" , $ iv_size );
1008
+ } elseif (strlen ($ iv ) != $ iv_size ) {
1009
+ trigger_error (
1010
+ 'mcrypt_ ' . $ op . '(): The IV parameter must be as long as the blocksize ' ,
1011
+ E_USER_WARNING
1012
+ );
1013
+ $ iv = str_repeat ("\0" , $ iv_size );
1014
+ }
1015
+ } else {
1016
+ $ iv = null ;
1017
+ }
1018
+ phpseclib_mcrypt_generic_init ($ td , $ key , $ iv );
1019
+ return $ op == 'encrypt ' ? phpseclib_mcrypt_generic ($ td , $ data ) : phpseclib_mdecrypt_generic ($ td , $ data );
1020
+ }
1021
+
965
1022
/**
966
1023
* Encrypt / decrypt data
967
1024
*
@@ -1027,6 +1084,85 @@ function phpseclib_mcrypt_helper($cipher, $key, $data, $mode, $iv, $op)
1027
1084
return $ op == 'encrypt ' ? phpseclib_mcrypt_generic ($ td , $ data ) : phpseclib_mdecrypt_generic ($ td , $ data );
1028
1085
}
1029
1086
1087
+ /**
1088
+ * Encrypts/decrypts data in CFB mode
1089
+ *
1090
+ * @param string $cipher
1091
+ * @param string $key
1092
+ * @param string $data
1093
+ * @param int $mode
1094
+ * @param string $iv optional
1095
+ * @return string|bool
1096
+ * @access public
1097
+ */
1098
+ function phpseclib_mcrypt_cfb ($ cipher , $ key , $ data , $ mode , $ iv = null )
1099
+ {
1100
+ // https://web.archive.org/web/20180106174656/https://www.php.net/manual/en/function.mcrypt-cfb.php
1101
+ return $ mode == MCRYPT_ENCRYPT ?
1102
+ phpseclib_mcrypt_encrypt ($ cipher , $ key , $ data , MCRYPT_MODE_CFB , $ iv ) :
1103
+ phpseclib_mcrypt_decrypt ($ cipher , $ key , $ data , MCRYPT_MODE_CFB , $ iv );
1104
+ }
1105
+
1106
+ /**
1107
+ * Encrypts/decrypts data in OFB mode
1108
+ *
1109
+ * @param string $cipher
1110
+ * @param string $key
1111
+ * @param string $data
1112
+ * @param int $mode
1113
+ * @param string $iv optional
1114
+ * @return string|bool
1115
+ * @access public
1116
+ */
1117
+ function phpseclib_mcrypt_ofb ($ cipher , $ key , $ data , $ mode , $ iv = null )
1118
+ {
1119
+ // https://web.archive.org/web/20180106174656/https://www.php.net/manual/en/function.mcrypt-ofb.php
1120
+ return $ mode == MCRYPT_ENCRYPT ?
1121
+ phpseclib_mcrypt_encrypt ($ cipher , $ key , $ data , MCRYPT_MODE_OFB , $ iv ) :
1122
+ phpseclib_mcrypt_decrypt ($ cipher , $ key , $ data , MCRYPT_MODE_OFB , $ iv );
1123
+ }
1124
+
1125
+ /**
1126
+ * Encrypts/decrypts data in CBC mode
1127
+ *
1128
+ * @param string $cipher
1129
+ * @param string $key
1130
+ * @param string $data
1131
+ * @param int $mode
1132
+ * @param string $iv optional
1133
+ * @return string|bool
1134
+ * @access public
1135
+ */
1136
+ function phpseclib_mcrypt_cbc ($ cipher , $ key , $ data , $ mode , $ iv = null )
1137
+ {
1138
+ // https://web.archive.org/web/20180106174656/https://www.php.net/manual/en/function.mcrypt-cbc.php
1139
+ return $ mode == MCRYPT_ENCRYPT ?
1140
+ phpseclib_mcrypt_encrypt ($ cipher , $ key , $ data , MCRYPT_MODE_CBC , $ iv ) :
1141
+ phpseclib_mcrypt_decrypt ($ cipher , $ key , $ data , MCRYPT_MODE_CBC , $ iv );
1142
+ }
1143
+
1144
+ /**
1145
+ * Encrypts/decrypts data in ECB mode
1146
+ *
1147
+ * @param string $cipher
1148
+ * @param string $key
1149
+ * @param string $data
1150
+ * @param int $mode
1151
+ * @param string $iv optional
1152
+ * @return string|bool
1153
+ * @access public
1154
+ */
1155
+ function phpseclib_mcrypt_ecb ($ cipher , $ key , $ data , $ mode , $ iv = null )
1156
+ {
1157
+ // idk why mcrypt_ecb had an $iv parameter when ECB mode doesn't use an IV
1158
+ // but whatever
1159
+
1160
+ // https://web.archive.org/web/20180106174656/https://www.php.net/manual/en/function.mcrypt-ecb.php
1161
+ return $ mode == MCRYPT_ENCRYPT ?
1162
+ phpseclib_mcrypt_encrypt ($ cipher , $ key , $ data , MCRYPT_MODE_ECB , $ iv ) :
1163
+ phpseclib_mcrypt_decrypt ($ cipher , $ key , $ data , MCRYPT_MODE_ECB , $ iv );
1164
+ }
1165
+
1030
1166
/**
1031
1167
* Encrypts plaintext with given parameters
1032
1168
*
@@ -1042,7 +1178,9 @@ function phpseclib_mcrypt_helper($cipher, $key, $data, $mode, $iv, $op)
1042
1178
*/
1043
1179
function phpseclib_mcrypt_encrypt ($ cipher , $ key , $ data , $ mode , $ iv = null )
1044
1180
{
1045
- return phpseclib_mcrypt_helper ($ cipher , $ key , $ data , $ mode , $ iv , 'encrypt ' );
1181
+ return defined ('PHPSECLIB_MCRYPT_TARGET_VERSION ' ) && version_compare (PHPSECLIB_MCRYPT_TARGET_VERSION , '5.6.0 ' , '>= ' ) ?
1182
+ phpseclib_mcrypt_helper ($ cipher , $ key , $ data , $ mode , $ iv , 'encrypt ' ) :
1183
+ phpseclib_mcrypt_helper_old ($ cipher , $ key , $ data , $ mode , $ iv , 'encrypt ' );
1046
1184
}
1047
1185
1048
1186
/**
@@ -1060,7 +1198,9 @@ function phpseclib_mcrypt_encrypt($cipher, $key, $data, $mode, $iv = null)
1060
1198
*/
1061
1199
function phpseclib_mcrypt_decrypt ($ cipher , $ key , $ data , $ mode , $ iv = null )
1062
1200
{
1063
- return phpseclib_mcrypt_helper ($ cipher , $ key , $ data , $ mode , $ iv , 'decrypt ' );
1201
+ return defined ('PHPSECLIB_MCRYPT_TARGET_VERSION ' ) && version_compare (PHPSECLIB_MCRYPT_TARGET_VERSION , '5.6.0 ' , '>= ' ) ?
1202
+ phpseclib_mcrypt_helper ($ cipher , $ key , $ data , $ mode , $ iv , 'decrypt ' ) :
1203
+ phpseclib_mcrypt_helper_old ($ cipher , $ key , $ data , $ mode , $ iv , 'decrypt ' );
1064
1204
}
1065
1205
1066
1206
/**
@@ -1266,6 +1406,33 @@ public function onClose()
1266
1406
1267
1407
// define
1268
1408
if (!function_exists ('mcrypt_list_algorithms ' )) {
1409
+ if (defined ('PHPSECLIB_MCRYPT_TARGET_VERSION ' ) && version_compare (PHPSECLIB_MCRYPT_TARGET_VERSION , '7.0.0 ' , '< ' )) {
1410
+ function mcrypt_generic_end ($ td )
1411
+ {
1412
+ return phpseclib_mcrypt_generic_end ($ td );
1413
+ }
1414
+
1415
+ function mcrypt_ecb ($ cipher , $ key , $ data , $ mode , $ iv = null )
1416
+ {
1417
+ return phpseclib_mcrypt_ecb ($ cipher , $ key , $ data , $ mode , $ iv );
1418
+ }
1419
+
1420
+ function mcrypt_cbc ($ cipher , $ key , $ data , $ mode , $ iv = null )
1421
+ {
1422
+ return phpseclib_mcrypt_cbc ($ cipher , $ key , $ data , $ mode , $ iv );
1423
+ }
1424
+
1425
+ function mcrypt_cfb ($ cipher , $ key , $ data , $ mode , $ iv = null )
1426
+ {
1427
+ return phpseclib_mcrypt_cfb ($ cipher , $ key , $ data , $ mode , $ iv );
1428
+ }
1429
+
1430
+ function mcrypt_ofb ($ cipher , $ key , $ data , $ mode , $ iv = null )
1431
+ {
1432
+ return phpseclib_mcrypt_ofb ($ cipher , $ key , $ data , $ mode , $ iv );
1433
+ }
1434
+ }
1435
+
1269
1436
function mcrypt_list_algorithms ($ lib_dir = '' )
1270
1437
{
1271
1438
return phpseclib_mcrypt_list_algorithms ($ lib_dir );
0 commit comments