求C# TEA加密算法
解决方案 »
- [.NET技术/C#]InternetGetCookie 259错误(没有可用的数据)
- 文件操作问题。。。。
- 当点击select中的一个option时,fieldset里面就显示这个option对应的一组内容
- 如何使鼠标移动到按钮上,使按钮有框框的效果
- 请问如何在Win2000中让winform控件的外观显示成xp的形式???
- c# winform 如何获取网页浏览器,正在浏览的网页的地址url了?
- c#win下面的难题求解!!!!!!!!!!!
- 装箱和拆箱问题.
- 如何用Newtonsoft序列化不带[Datamenber]的字段
- 请教绑定固话拨号问题
- RTF中的图片字符串怎么变成Image类
- 请问FastReport怎么样才能连到ADO.NET的数据!
{
private static Byte[] Key=System.Text.Encoding.UTF8.GetBytes("123456789123456789");
public static Byte[] Encrypt(Byte[] Data)
{
if (Data.Length == 0)
{
return Data;
}
return ToByteArray(Encrypt(ToUInt32Array(Data, true), ToUInt32Array(Key, false)), false);
}
public static Byte[] Decrypt(Byte[] Data)
{
if (Data.Length == 0)
{
return Data;
}
return ToByteArray(Decrypt(ToUInt32Array(Data, false), ToUInt32Array(Key, false)), true);
} public static string EncryptToString(string Data)
{
return System.Convert.ToBase64String(Encrypt(System.Text.Encoding.UTF8.GetBytes(Data)));
} public static string DecryptFromString(string Data)
{
return System.Text.Encoding.UTF8.GetString(Decrypt(System.Convert.FromBase64String(Data)));
} public static UInt32[] Encrypt(UInt32[] v, UInt32[] k)
{
Int32 n = v.Length - 1;
if (n < 1)
{
return v;
}
if (k.Length < 4)
{
UInt32[] Key = new UInt32[4];
k.CopyTo(Key, 0);
k = Key;
}
UInt32 z = v[n], y = v[0], delta = 0x9E3779B9, sum = 0, e;
Int32 p, q = 6 + 52 / (n + 1);
while (q-- > 0)
{
sum = unchecked(sum + delta);
e = sum >> 2 & 3;
for (p = 0; p < n; p++)
{
y = v[p + 1];
z = unchecked(v[p] += (z >> 5 ^ y << 2) + (y >> 3 ^ z << 4) ^ (sum ^ y) + (k[p & 3 ^ e] ^ z));
}
y = v[0];
z = unchecked(v[n] += (z >> 5 ^ y << 2) + (y >> 3 ^ z << 4) ^ (sum ^ y) + (k[p & 3 ^ e] ^ z));
}
return v;
}
public static UInt32[] Decrypt(UInt32[] v, UInt32[] k)
{
Int32 n = v.Length - 1;
if (n < 1)
{
return v;
}
if (k.Length < 4)
{
UInt32[] Key = new UInt32[4];
k.CopyTo(Key, 0);
k = Key;
}
UInt32 z = v[n], y = v[0], delta = 0x9E3779B9, sum, e;
Int32 p, q = 6 + 52 / (n + 1);
sum = unchecked((UInt32)(q * delta));
while (sum != 0)
{
e = sum >> 2 & 3;
for (p = n; p > 0; p--)
{
z = v[p - 1];
y = unchecked(v[p] -= (z >> 5 ^ y << 2) + (y >> 3 ^ z << 4) ^ (sum ^ y) + (k[p & 3 ^ e] ^ z));
}
z = v[n];
y = unchecked(v[0] -= (z >> 5 ^ y << 2) + (y >> 3 ^ z << 4) ^ (sum ^ y) + (k[p & 3 ^ e] ^ z));
sum = unchecked(sum - delta);
}
return v;
}
private static UInt32[] ToUInt32Array(Byte[] Data, Boolean IncludeLength)
{
Int32 n = (((Data.Length & 3) == 0) ? (Data.Length >> 2) : ((Data.Length >> 2) + 1));
UInt32[] Result;
if (IncludeLength)
{
Result = new UInt32[n + 1];
Result[n] = (UInt32)Data.Length;
}
else
{
Result = new UInt32[n];
}
n = Data.Length;
for (Int32 i = 0; i < n; i++)
{
Result[i >> 2] |= (UInt32)Data[i] << ((i & 3) << 3);
}
return Result;
}
private static Byte[] ToByteArray(UInt32[] Data, Boolean IncludeLength)
{
Int32 n;
if (IncludeLength)
{
n = (Int32)Data[Data.Length - 1];
}
else
{
n = Data.Length << 2;
}
Byte[] Result = new Byte[n];
for (Int32 i = 0; i < n; i++)
{
Result[i] = (Byte)(Data[i >> 2] >> ((i & 3) << 3));
}
return Result;
}
}