@@ -40,14 +40,14 @@ public static function rijndael($text, $key, $operation)
40
40
$ mode = MCRYPT_MODE_CBC ;
41
41
$ ivSize = mcrypt_get_iv_size ($ algorithm , $ mode );
42
42
43
- $ cryptKey = substr ($ key , 0 , 32 );
43
+ $ cryptKey = mb_substr ($ key , 0 , 32 , ' 8bit ' );
44
44
45
45
if ($ operation === 'encrypt ' ) {
46
46
$ iv = mcrypt_create_iv ($ ivSize , MCRYPT_DEV_URANDOM );
47
47
return $ iv . '$$ ' . mcrypt_encrypt ($ algorithm , $ cryptKey , $ text , $ mode , $ iv );
48
48
}
49
- $ iv = substr ($ text , 0 , $ ivSize );
50
- $ text = substr ($ text , $ ivSize + 2 );
49
+ $ iv = mb_substr ($ text , 0 , $ ivSize, ' 8bit ' );
50
+ $ text = mb_substr ($ text , $ ivSize + 2 , null , ' 8bit ' );
51
51
return rtrim (mcrypt_decrypt ($ algorithm , $ cryptKey , $ text , $ mode , $ iv ), "\0" );
52
52
}
53
53
@@ -72,7 +72,7 @@ public static function encrypt($plain, $key)
72
72
$ iv = mcrypt_create_iv ($ ivSize , MCRYPT_DEV_URANDOM );
73
73
74
74
// Pad out plain to make it AES compatible.
75
- $ pad = ($ ivSize - (strlen ($ plain ) % $ ivSize ));
75
+ $ pad = ($ ivSize - (mb_strlen ($ plain, ' 8bit ' ) % $ ivSize ));
76
76
$ plain .= str_repeat (chr ($ pad ), $ pad );
77
77
78
78
return $ iv . mcrypt_encrypt ($ algorithm , $ key , $ plain , $ mode , $ iv );
@@ -92,18 +92,19 @@ public static function decrypt($cipher, $key)
92
92
$ mode = MCRYPT_MODE_CBC ;
93
93
$ ivSize = mcrypt_get_iv_size ($ algorithm , $ mode );
94
94
95
- $ iv = substr ($ cipher , 0 , $ ivSize );
96
- $ cipher = substr ($ cipher , $ ivSize );
95
+ $ iv = mb_substr ($ cipher , 0 , $ ivSize, ' 8bit ' );
96
+ $ cipher = mb_substr ($ cipher , $ ivSize, null , ' 8bit ' );
97
97
$ plain = mcrypt_decrypt ($ algorithm , $ key , $ cipher , $ mode , $ iv );
98
98
99
99
// Remove PKCS#7 padding or Null bytes
100
100
// Newer values will be PKCS#7 padded, while old
101
101
// mcrypt values will be null byte padded.
102
- $ padChar = substr ($ plain , -1 );
102
+ $ padChar = mb_substr ($ plain , -1 , null , ' 8bit ' );
103
103
if ($ padChar === "\0" ) {
104
104
return trim ($ plain , "\0" );
105
105
}
106
106
$ padLen = ord ($ padChar );
107
- return substr ($ plain , 0 , -$ padLen );
107
+ $ result = mb_substr ($ plain , 0 , -$ padLen , '8bit ' );
108
+ return $ result === '' ? false : $ result ;
108
109
}
109
110
}
0 commit comments