首页 新闻 会员 周边 捐助

求3EDS算法

0
悬赏园豆:60 [已解决问题] 解决于 2012-05-13 22:26

求C# 和 PHP 相互兼容的3DES 算法

请附上C# 和 PHP  代码 谢谢

Coder Boy的主页 Coder Boy | 初学一级 | 园豆:105
提问于:2012-05-12 00:11
< >
分享
最佳答案
0
<?php

if(!isset($_POST['op'])) {
    ?>
<form id="form1" name="form1" method="post" action="">
  enter text
  <input name="data" type="text" />
  <input type="hidden" value="op" name="op" />
  <input type="submit" name="Submit" value="Submit" />
</form>
    <?php
}else {
    $buffer = $_POST['data']; 
    // get the amount of bytes to pad
    $extra = 8 - (strlen($buffer) % 8);
    // add the zero padding
    if($extra > 0) {
        for($i = 0; $i < $extra; $i++) {
            $buffer .= "\0";
        }
    }
    // very simple ASCII key and IV
    $key = "passwordDR0wSS@P6660juht";
    $iv = "password";
    // hex encode the return value
    echo "Result: ".bin2hex(mcrypt_cbc(MCRYPT_3DES, $key, $buffer, MCRYPT_ENCRYPT, $iv));    
}
?>

 

using System;
using System.Security.Cryptography;
using System.Text;
using System.IO;

namespace TestBed {
    class Program {
        static void Main(string[] args) {
            byte[] key = Encoding.ASCII.GetBytes("passwordDR0wSS@P6660juht");
            byte[] iv = Encoding.ASCII.GetBytes("password");
            byte[] data = Encoding.ASCII.GetBytes("Test");
            byte[] enc = new byte[0];
            TripleDES tdes = TripleDES.Create();
            tdes.IV = iv;
            tdes.Key = key;
            tdes.Mode = CipherMode.CBC;
            tdes.Padding = PaddingMode.Zeros;
            ICryptoTransform ict = tdes.CreateEncryptor();
            enc = ict.TransformFinalBlock(data, 0, data.Length);
            Console.WriteLine(Bin2Hex(enc));
            Console.ReadLine();
        }
        // my bin2hex implementation        
        static string Bin2Hex(byte[] bin) {
            StringBuilder sb = new StringBuilder(bin.Length * 2);
            foreach(byte b in bin) {
                sb.Append(b.ToString("x").PadLeft(2, '0'));
            }
            return sb.ToString();
        }
    }
}

 

另一种方法:

PHP代码:

function
encrypt($string) { //Key $key = "xxxxxxxx"; //Encryption $cipher_alg = MCRYPT_TRIPLEDES; $iv = mcrypt_create_iv(mcrypt_get_iv_size($cipher_alg,MCRYPT_MODE_ECB), MCRYPT_RAND); $encrypted_string = mcrypt_encrypt($cipher_alg, $key, $string, MCRYPT_MODE_ECB, $iv); return base64_encode($encrypted_string); return $encrypted_string; } function decrypt($string) { $string = base64_decode($string); //key $key = "xxxxxxxx"; $cipher_alg = MCRYPT_TRIPLEDES; $iv = mcrypt_create_iv(mcrypt_get_iv_size($cipher_alg,MCRYPT_MODE_ECB), MCRYPT_RAND); $decrypted_string = mcrypt_decrypt($cipher_alg, $key, $string, MCRYPT_MODE_ECB, $iv); return trim($decrypted_string); }

 

C#代码:
using System;
using System.Security.Cryptography;
using System.Text;

public class Crypto3DES
{
    public Crypto3DES()
    {

    }

    private System.Text.Encoding encoding;

    public string Key
    {
        get
        {
            return "xxxxxxxx";
        }
    }

    public System.Text.Encoding Encoding
    {
        get
        {
            if( encoding == null )
            {
                encoding = System.Text.Encoding.UTF8;
            }
            return encoding;
        }

        set
        {
            encoding = value;
        }
    }

    public string Encrypt3DES( string strString )
    {
        DESCryptoServiceProvider DES = new DESCryptoServiceProvider();

        DES.Key = Encoding.GetBytes( this.Key );
        DES.Mode = CipherMode.ECB;
        DES.Padding = PaddingMode.Zeros;

        ICryptoTransform DESEncrypt = DES.CreateEncryptor();

        byte[] Buffer = encoding.GetBytes(strString);

        return Convert.ToBase64String(DESEncrypt.TransformFinalBlock(Buffer, 0, Buffer.Length));
    }

    public string Decrypt3DES( string strString )
    {
        DESCryptoServiceProvider  DES = new DESCryptoServiceProvider ();

        DES.Key = Encoding.UTF8.GetBytes( this.Key );
        DES.Mode = CipherMode.ECB;
        DES.Padding = PaddingMode.Zeros;
        ICryptoTransform DESDecrypt = DES.CreateDecryptor();

        byte[] Buffer = Convert.FromBase64String(strString);
        return UTF8Encoding.UTF8.GetString( DESDecrypt.TransformFinalBlock(Buffer, 0, Buffer.Length) );
    }
}

 



收获园豆:60
悟行 | 专家六级 |园豆:12559 | 2012-05-12 11:14

非常 感谢

Coder Boy | 园豆:105 (初学一级) | 2012-05-13 22:26
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册