namespace Encryption
{
using System;
using System.Text; public static class DES
{
private const string DESKey = "e!f@b#o$"; public static string DESEncrypKey(string Src, string Key)
{
string CS$1$0000;
try
{
byte[] b1 = Encoding.Default.GetBytes(Src);
int KeyLen = Key.Length;
if (KeyLen == 0)
{
Key = "e!f@b#o$";
}
int KeyPos = -1;
int SrcAsc = 0;
int Range = 0x100;
int offset = new Random().Next(0, Range);
string dest = string.Format("{0:X2}", offset);
foreach (byte b in b1)
{
SrcAsc = (b + offset) % 0xff;
if (KeyPos < (KeyLen - 1))
{
KeyPos++;
}
else
{
KeyPos = 0;
}
SrcAsc ^= Convert.ToByte(Key[KeyPos]);
dest = dest + string.Format("{0:X2}", SrcAsc);
offset = SrcAsc;
}
CS$1$0000 = dest;
}
catch
{
throw new Exception("加密错误");
}
return CS$1$0000;
} public static string DESUncrypKey(string Src, string Key)
{
string CS$1$0000;
try
{
int KeyLen = Key.Length;
if (KeyLen == 0)
{
Key = "e!f@b#o$";
}
int KeyPos = -1;
int SrcAsc = 0;
int offset = Convert.ToByte(Src.Substring(0, 2), 0x10);
int SrcPos = 2;
int i = 0;
byte[] dest = new byte[(Src.Length / 2) - 1];
while (SrcPos < Src.Length)
{
SrcAsc = Convert.ToByte(Src.Substring(SrcPos, 2), 0x10);
if (KeyPos < (KeyLen - 1))
{
KeyPos++;
}
else
{
KeyPos = 0;
}
int TmpSrcAsc = SrcAsc ^ Convert.ToByte(Key[KeyPos]);
if (TmpSrcAsc <= offset)
{
TmpSrcAsc = (0xff + TmpSrcAsc) - offset;
}
else
{
TmpSrcAsc -= offset;
}
dest[i] = (byte) TmpSrcAsc;
offset = SrcAsc;
SrcPos += 2;
i++;
}
CS$1$0000 = Encoding.Default.GetString(dest);
}
catch
{
throw new Exception("解密错误");
}
return CS$1$0000;
} public static string StringToBytes(string Src)
{
string s = string.Empty;
byte[] buffer = Encoding.Unicode.GetBytes(Src);
foreach (byte b in buffer)
{
s = s + b.ToString();
}
return s;
} public static string StringToBytes(string src, Encoding encode)
{
byte[] buffer = encode.GetBytes(src);
string strBuffer = string.Empty;
foreach (byte b in buffer)
{
strBuffer = strBuffer + b.ToString();
}
return strBuffer;
}
}
}加密过后的密文是554A1B76C45E278DD4951984790A6B83这种格式的,求大牛帮忙写一个解密的程序。
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货