Thank you.
As below, there is a C # encryption and decryption method, I wrote a PHP7 implementation. But the result of the decryption and C # is completely different, where is wrong? I am a rookie, thank you.
C
public static string Decrypt (String Text, String sKey) {DESCryptoServiceProvider Provider = new DESCRYP Toserviceprovider (); int num = TEXT.LENGTH/2; byte[] buffer = new Byte[num]; for (int i = 0; i < num; i++) {int num3 = Convert.ToInt32 (text.substring (i * 2, 2), 0x10); Buffer[i] = (byte) num3; } provider. Key = Encoding.ASCII.GetBytes (FormsAuthentication.HashPasswordForStoringInConfigFile (SKey, "MD5"). Substring (0, 8)); PROVIDER.IV = Encoding.ASCII.GetBytes (FormsAuthentication.HashPasswordForStoringInConfigFile (SKey, "MD5"). Substring (0, 8)); MemoryStream stream = new MemoryStream (); CryptoStream stream2 = new CryptoStream (stream, provider. CreateDecryptor (), cryptostreammode.write); Stream2. Write (buffer, 0, buffer. Length); Stream2. FlushFinalBlock (); Return Encoding.Default.GetString (stream. ToArray ()); public static string Encrypt (String Text, String sKey) {DESCryptoServiceProvider provider = new DESCryptoServiceProvider (); byte[] bytes = Encoding.Default.GetBytes (Text); Provider. Key = Encoding.ASCII.GetBytes (FormsAuthentication.HashPasswordForStoringInConfigFile (SKey, "MD5"). Substring (0, 8)); PROVIDER.IV = Encoding.ASCII.GetBytes (FormsAuthentication.HashPasswordForStoringInConfigFile (SKey, "MD5"). Substring (0, 8)); MemoryStream stream = new MemoryStream (); string x = Provider. Mode.tostring (); CryptoStream stream2 = new CryptoStream (stream, provider. CreateEncryptor (), cryptostreammode.write); Stream2. Write (bytes, 0, bytes. Length); Stream2. FlushFinalBlock (); StringBuilder builder = new StringBuilder (); foreach (Byte num in stream. ToArray ()) {Builder. AppendFormat ("{0:x2}", num); } return Builder. ToString (); }
PHP7
Public function Encrypt ($input, $key) {$size = Mcrypt_get_block_size (Mcrypt_des, MCRYPT_MODE_CBC); $input = $this->pkcs5_pad ($input, $size); $TD = Mcrypt_module_open (Mcrypt_des, ', MCRYPT_MODE_CBC, '); $iv = Mcrypt_create_iv (Mcrypt_enc_get_iv_size ($TD), Mcrypt_rand); Mcrypt_generic_init ($TD, $key, $IV); $data = Mcrypt_generic ($TD, $input); Mcrypt_generic_deinit ($TD); Mcrypt_module_close ($TD); $data = Base64_encode ($data); return $data; } Private Function Pkcs5_pad ($text, $blocksize) {$pad = $blocksize-(strlen ($text)% $blocksize); Return $text. Str_repeat (Chr ($pad), $pad); The public function decrypt ($SSTR, $sKey) {$decrypted = Mcrypt_decrypt (Mcrypt_des, $sKe Y, Base64_decode ($sStr), MCRYPT_MODE_CBC); $dec _s = strlen ($decrypted); $padding = Ord ($decrypted [$dec _s-1]); $decrypted = substr ($decrypted, 0,-$padding); return $decrypted; }
Reply content:
Thank you.
As below, there is a C # encryption and decryption method, I wrote a PHP7 implementation. But the result of the decryption and C # is completely different, where is wrong? I am a rookie, thank you.
C
public static string Decrypt (String Text, String sKey) {DESCryptoServiceProvider Provider = new DESCRYP Toserviceprovider (); int num = TEXT.LENGTH/2; byte[] buffer = new Byte[num]; for (int i = 0; i < num; i++) {int num3 = Convert.ToInt32 (text.substring (i * 2, 2), 0x10); Buffer[i] = (byte) num3; } provider. Key = Encoding.ASCII.GetBytes (FormsAuthentication.HashPasswordForStoringInConfigFile (SKey, "MD5"). Substring (0, 8)); PROVIDER.IV = Encoding.ASCII.GetBytes (FormsAuthentication.HashPasswordForStoringInConfigFile (SKey, "MD5"). Substring (0, 8)); MemoryStream stream = new MemoryStream (); CryptoStream stream2 = new CryptoStream (stream, provider. CreateDecryptor (), cryptostreammode.write); Stream2. Write (buffer, 0, buffer. Length); Stream2. FlushFinalBlock (); Return Encoding.Default.GetString (stream. ToArray ()); public static string Encrypt (String Text, String sKey) {DESCryptoServiceProvider provider = new DESCryptoServiceProvider (); byte[] bytes = Encoding.Default.GetBytes (Text); Provider. Key = Encoding.ASCII.GetBytes (FormsAuthentication.HashPasswordForStoringInConfigFile (SKey, "MD5"). Substring (0, 8)); PROVIDER.IV = Encoding.ASCII.GetBytes (FormsAuthentication.HashPasswordForStoringInConfigFile (SKey, "MD5"). Substring (0, 8)); MemoryStream stream = new MemoryStream (); string x = Provider. Mode.tostring (); CryptoStream stream2 = new CryptoStream (stream, provider. CreateEncryptor (), cryptostreammode.write); Stream2. Write (bytes, 0, bytes. Length); Stream2. FlushFinalBlock (); StringBuilder builder = new StringBuilder (); foreach (Byte num in stream. ToArray ()) {Builder. AppendFormat ("{0:x2}", num); } return Builder. ToString (); }
PHP7
Public function Encrypt ($input, $key) {$size = Mcrypt_get_block_size (Mcrypt_des, MCRYPT_MODE_CBC); $input = $this->pkcs5_pad ($input, $size); $TD = Mcrypt_module_open (Mcrypt_des, ', MCRYPT_MODE_CBC, '); $iv = Mcrypt_create_iv (Mcrypt_enc_get_iv_size ($TD), Mcrypt_rand); Mcrypt_generic_init ($TD, $key, $IV); $data = Mcrypt_generic ($TD, $input); Mcrypt_generic_deinit ($TD); Mcrypt_module_close ($TD); $data = Base64_encode ($data); return $data; } Private Function Pkcs5_pad ($text, $blocksize) {$pad = $blocksize-(strlen ($text)% $blocksize); Return $text. Str_repeat (Chr ($pad), $pad); The public function decrypt ($SSTR, $sKey) {$decrypted = Mcrypt_decrypt (Mcrypt_des, $sKe Y, Base64_decode ($sStr), MCRYPT_MODE_CBC); $dec _s = strlen ($decrypted); $padding = Ord ($decrypted [$dec _s-1]); $decrypted = substr ($decrypted, 0,-$padding); return $decrypted; }
Is it not good to use PHP's own OpenSSL?