解密 - 請問這個轉換的php7加密方法,為什麼和c#的結果不一樣呢?

來源:互聯網
上載者:User
謝謝大家.

如下,有c#的加解密方法,我自己寫了個php7的實現.但是加解密的結果和c#的完全不一樣,請問哪裡錯了?本人菜鳥,多謝各位.

c

public static string Decrypt(string Text, string sKey)        {            DESCryptoServiceProvider provider = new DESCryptoServiceProvider();            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);    }    public function decrypt($sStr, $sKey)    {        $decrypted = mcrypt_decrypt(            MCRYPT_DES,            $sKey,            base64_decode($sStr),            MCRYPT_MODE_CBC        );        $dec_s = strlen($decrypted);        $padding = ord($decrypted[$dec_s - 1]);        $decrypted = substr($decrypted, 0, -$padding);        return $decrypted;    }

回複內容:

謝謝大家.

如下,有c#的加解密方法,我自己寫了個php7的實現.但是加解密的結果和c#的完全不一樣,請問哪裡錯了?本人菜鳥,多謝各位.

c

public static string Decrypt(string Text, string sKey)        {            DESCryptoServiceProvider provider = new DESCryptoServiceProvider();            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);    }    public function decrypt($sStr, $sKey)    {        $decrypted = mcrypt_decrypt(            MCRYPT_DES,            $sKey,            base64_decode($sStr),            MCRYPT_MODE_CBC        );        $dec_s = strlen($decrypted);        $padding = ord($decrypted[$dec_s - 1]);        $decrypted = substr($decrypted, 0, -$padding);        return $decrypted;    }

使用PHP內建的OpenSSL不好了?

  • 相關文章

    聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    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.