希望将此JAVA / PHP加密代码转换为C#

时间:2013-12-11 10:47:20

标签: php encryption

我在PHP工作并希望将一段代码从JAVA / PHP转换为与加密相关的C#,我是C#的新手可以帮助我吗?

http://www.androidsnippets.com/encrypt-decrypt-between-android-and-php

中挑选代码

下面给出 - 请帮帮我:

=============================================== =====

/ * *** / /的 PHP /

    <?php 

    class MCrypt
    {
            private $iv = 'fedcba9876543210'; #Same as in JAVA
            private $key = '0123456789abcdef'; #Same as in JAVA


            function __construct()
            {
            }

            function encrypt($str) {

              //$key = $this->hex2bin($key);    
              $iv = $this->iv;

              $td = mcrypt_module_open('rijndael-128', '', 'cbc', $iv);

              mcrypt_generic_init($td, $this->key, $iv);
              $encrypted = mcrypt_generic($td, $str);

              mcrypt_generic_deinit($td);
              mcrypt_module_close($td);

              return bin2hex($encrypted);
            }

            function decrypt($code) {
              //$key = $this->hex2bin($key);
              $code = $this->hex2bin($code);
              $iv = $this->iv;

              $td = mcrypt_module_open('rijndael-128', '', 'cbc', $iv);

              mcrypt_generic_init($td, $this->key, $iv);
              $decrypted = mdecrypt_generic($td, $code);

              mcrypt_generic_deinit($td);
              mcrypt_module_close($td);

              return utf8_encode(trim($decrypted));
            }

            protected function hex2bin($hexdata) {
              $bindata = '';

              for ($i = 0; $i < strlen($hexdata); $i += 2) {
                    $bindata .= chr(hexdec(substr($hexdata, $i, 2)));
              }

              return $bindata;
            }

    }

1 个答案:

答案 0 :(得分:0)

查看MSDN(MicroSoft开发者网络), 在页面的末尾有一些名为RijndaelManaged_Example的例子,展示了如何加密和解密

http://msdn.microsoft.com/en-us/library/f9df14hc(v=vs.110).aspx