PHP中用DES加密,为什么用C#DES解密不了

kanyong0551 2009-03-10 01:11:34
或者说。相同的数据在DES在PHP中和c#中,得到的加密后的数据不一样。

请达人指点。

PHP代码
$key="abcdegfh";
$iv="12345678";
message="kjsdfuihyweflgjerogjreg";
echo mcrypt_encrypt(MCRYPT_DES, $key, $message, MCRYPT_MODE_CBC, $iv);
...全文
615 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
kanyong0551 2009-03-10
  • 打赏
  • 举报
回复
UP
kanyong0551 2009-03-10
  • 打赏
  • 举报
回复
是一致的。谢谢各位了。
有做过这一块的达人吗?
huwei001982 2009-03-10
  • 打赏
  • 举报
回复
所使用的key 和 iv 都要一致
kanyong0551 2009-03-10
  • 打赏
  • 举报
回复
会不会是PHP和c#的字符集用的不一样?
请达人指教。
kanyong0551 2009-03-10
  • 打赏
  • 举报
回复
key 和Iv,都是一样。基数是?请详细点。
现在情况是:C#可以解密,但是解密出来的数据不对。怎么办?
liujiayu10 2009-03-10
  • 打赏
  • 举报
回复
基数一致吗?
kanyong0551 2009-03-10
  • 打赏
  • 举报
回复
请问上面两位,你们给的不能解我上面PHP的代码呀?知道原因吗?
我就是在这里不明白。
请你们把正确的PHP,DES加密代码和c#的DES解密代码贴出来,好吗?
OKILOVE 2009-03-10
  • 打赏
  • 举报
回复
例子:)

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").GetBytes(decryptKey);
byte[] bIV = DESIV;
byte[] bDecContent = Convert.FromBase64String(decryptString);
DESCryptoServiceProvider DCSP = new DESCryptoServiceProvider();
MemoryStream mStream = new MemoryStream();
CryptoStream cStream = new CryptoStream(mStream, DCSP.CreateDecryptor(bKey, bIV), CryptoStreamMode.Write);
cStream.Write(bDecContent, 0, bDecContent.Length);
cStream.FlushFinalBlock();
return Encoding.GetEncoding("GB2312").GetString(mStream.ToArray());
}
catch
{
return decryptString;
}
}
}
}
whoami333 2009-03-10
  • 打赏
  • 举报
回复
c#使用字节数组。c#解密应该需要同样的iv。

111,126

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Creator Browser
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧