Decryption-How is the PHP7 encryption method of this conversion different from the results of C #?

Source: Internet
Author: User
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?

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.