请问如何用C#编写单倍DES加密

闲思暇想 2012-09-24 09:59:53
写了个加密的测试代码,但是加密的结果总是不对,大家帮忙看看那里用错了。
正常情况下:
1、密钥为“1111111111111111”,明文为“1111111111111111”,加密后的密文是:“F40379AB9E0EC533”。
2、密钥为“1111111111111111”,密文为“1111111111111111”,解密后的明文是:“237B2304C393D3AC”。
但我写的代码,第一种加密后的明文是:“5246259CE6678A20A026D7212F2BACF2”
第二种解密时报错。
大家看看是什么原因。

详细代码如下:
静态类,文件名packetchg.cs

using System;
using System.IO;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Security.Cryptography;

namespace wintest
{
//封装十六进制字符串的位运算,以及加解密算法
static class packetchg
{
//将十六进制的字符串转换成字节数组
public static byte[] hextobyte(string hex)
{
byte[] result;
if (hex.Count() % 2 == 1)
{
hex += "0";
}
int i;
int hexlen;
hexlen = hex.Count() / 2;
result = new byte[hexlen];
for (i = 0; i < hexlen; i++)
{
result[i] = (byte)Convert.ToUInt32(hex.Substring(i * 2, 2), 16);
}

return result;
}

//将字节数组转换成十六进制的字符串
public static string bytetohex(byte[] bytes)
{
string result;
result = "";
int lenhex = bytes.Count();
int i;
for (i = 0; i < lenhex; i++)
{
result += Convert.ToString(bytes[i], 16).PadLeft(2, '0');
}
return result;
}

//将字节数组用逗号连接成字符串
public static string bytetostring(byte[] bytes)
{
string result = "";
int bytecount = bytes.Count();
int i;
for (i = 0; i < bytecount; i++)
{
result += bytes[i] + ",";
}
return result;
}

//将字节数组进行异或运算
public static byte[] bytexor(byte[] bytea, byte[] byteb)
{
int lena = bytea.Count();
int lenb = byteb.Count();
int lenresult;
if (lena > lenb)
{
lenresult = lenb;
}
else
{
lenresult = lena;
}
byte[] byteresult = new byte[lenresult];
int i;
for (i = 0; i < lenresult; i++)
{
byteresult[i] = (byte)(bytea[i] ^ byteb[i]);
}
return byteresult;
}

//对十六进制的字符串进行异或运算(要求做异或的字符串必须长度一致,否则按短的字符串标准做异或)
public static string hexxor(string hexa, string hexb)
{
byte[] bytea = hextobyte(hexa);
byte[] byteb = hextobyte(hexb);
byte[] byteresult = bytexor(bytea, byteb);
return bytetohex(byteresult);
}

//DES加解密
public static string desende(string key, string data, int type = 0)
{
DESCryptoServiceProvider des = new DESCryptoServiceProvider();
CryptoStream desStream;
byte[] bytekey = hextobyte(key);
byte[] bytedata = hextobyte(data);
des.Key = bytekey;
// des.IV = bytekey;
des.Mode = CipherMode.ECB;
des.KeySize = 64;
des.FeedbackSize = 64;
MemoryStream streamout = new MemoryStream();

// 如果type为0,则视为加密操作
if (type == 0)
{
desStream = new CryptoStream(streamout, des.CreateEncryptor(), CryptoStreamMode.Write);
}
//解密操作
else
{
desStream = new CryptoStream(streamout, des.CreateDecryptor(), CryptoStreamMode.Write);
}
desStream.Write(bytedata, 0, bytedata.Count());
desStream.Close();
byte[] byteresult = streamout.ToArray();
des.Dispose();
return bytetohex(byteresult).ToUpper();
//return Convert.ToBase64String(streamout.ToArray());
}
}
}

测试窗体的代码:
窗体上有三个文本框,两个按钮。
文本框txtdeskey表示des的密钥,用16进制字符串表示。
文本框txtdesdata表示需要处理的数据,为明文或密文,也用16进制字符串表示。
文本框txtdesresult表示处理后的数据,为明文或密文,也用16进制字符串表示。
按钮bten表示加密操作。
按钮btde表示解密操作。

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace wintest
{
public partial class frmdes : Form
{
public frmdes()
{
InitializeComponent();
}

private void bten_Click(object sender, EventArgs e)
{
txtdesresult.Text = packetchg.desende(txtdeskey.Text, txtdesdata.Text);
}

private void btde_Click(object sender, EventArgs e)
{
txtdesresult.Text = packetchg.desende(txtdeskey.Text, txtdesdata.Text,1);
}
}
}
...全文
250 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
闲思暇想 2012-09-24
  • 打赏
  • 举报
回复
已经解决。
byte[] bytekey = hextobyte(key);
byte[] bytedata = hextobyte(data);
des.Key = bytekey;
des.Mode = CipherMode.ECB;
des.Padding = PaddingMode.None;

对des的属性做如下设置,即解决了问题。
dalmeeme 2012-09-24
  • 打赏
  • 举报
回复
看来楼主解决问题的能力还是有的。
billlyh 2012-09-24
  • 打赏
  • 举报
回复
DES加密是很常见的一种,网上很多的,搜索一下参考吧,我以前也是在网上找的,
周公 2012-09-24
  • 打赏
  • 举报
回复
刚看到你的留言说过来看的,没想到自己解决了。其实遇到问题自己解决是最靠谱的方式——至少我都是这么解决的。好几次遇到问题在stackoverflow上问,没人回答,最后去google搜,搜到的就是自己刚刚的提问。

110,577

社区成员

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

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

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