日期:2014-05-18  浏览次数:20973 次

PHP中用DES加密,为什么用C#DES解密不了
或者说。相同的数据在DES在PHP中和c#中,得到的加密后的数据不一样。

请达人指点。

PHP代码
$key="abcdegfh";  
$iv="12345678";  
message="kjsdfuihyweflgjerogjreg";
echo mcrypt_encrypt(MCRYPT_DES, $key, $message, MCRYPT_MODE_CBC, $iv);

------解决方案--------------------
例子:)
C# code
 
using System;
using System.Collections.Generic;
using System.Security.Cryptography;
using System.IO;
using System.Text;
using System.ComponentModel;

namespace SMSTimer
{
  /// <summary>
  /// 完成加密和解密的功能
  /// </summary>
  public class DES
  {
    /// <summary>
    /// 构造器
    /// </summary>
    public DES()
    {
      //
      // TODO: 在此处添加构造函数逻辑
      //
    }

    //默认密钥初始化向量
    private static byte[] DESIV = { 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF };

    /// <summary>
    /// DES加密字符串
    /// </summary>
    /// <param name="encriptString">待加密的字符串 </param>
    /// <param name="encKey">加密密钥,要求为8字节 </param>
    /// <returns>加密成功返回加密后的字符串,失败返回源串 </returns>
    public static string Encrypt(string encriptString, string encKey)
    {
      if (encKey.Trim().Length != 8)
        encKey = "87654321";

      try
      {
        byte[] bKey = Encoding.GetEncoding("GB2312").GetBytes(encKey.Substring(0, 8));
        byte[] bIV = DESIV;
        byte[] bEncContent = Encoding.GetEncoding("GB2312").GetBytes(encriptString);
        DESCryptoServiceProvider dCSP = new DESCryptoServiceProvider();
        MemoryStream mStream = new MemoryStream();
        //用指定的 Key 和初始化向量 (IV) 创建对称数据加密标准 (DES) 加密器对象
        CryptoStream cStream = new CryptoStream(mStream, dCSP.CreateEncryptor(bKey, bIV), CryptoStreamMode.Write);
        cStream.Write(bEncContent, 0, bEncContent.Length);
        cStream.FlushFinalBlock();
        return Convert.ToBase64String(mStream.ToArray());
        // return Encoding.GetEncoding("GB2312").GetString(mStream.ToArray());
      }
      catch
      {
        return encriptString;
      }
    }

    /// <summary>
    /// DES解密字符串
    /// </summary>
    /// <param name="decryptString">待解密的字符串 </param>
    /// <param name="decryptKey">解密密钥,要求为8字节,和加密密钥相同 </param>
    /// <returns>解密成功返回解密后的字符串,失败返源串 </returns>
    public static string Decrypt(string decryptString, string decryptKey)
    {
      if (decryptKey.Trim().Length != 8)
        decryptKey = "87654321";
      try
      {
        byte[] bKey = Encoding.GetEncoding("GB2312&quo