最近平台改造,需要写一个PHP和.NET下通用的加密解密类,听说有不少朋友也碰到一些问题,这里和大家分享一下 PHP加解密函数:  function encrypt($string) {
  //加密用的密钥文件
  $key = "xxxxxxxx";
  
  //加密方法
  $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);//转化成16进制
//  return $encrypted_string;
 }
 
 function decrypt($string) {
   $string = base64_decode($string);
   //加密用的密钥文件
   $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;
/**////
/// Crypto3DES 的摘要说明。
///
public class Crypto3DES
{
    public Crypto3DES()
    {
        //
        // TODO: 在此处添加构造函数逻辑
        //
    }
    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) );
    }

}