以下是加密代码。谢谢。
internal static string Encrypt(string key, string strvalue)
{
int num;
int num2;
if (strvalue.Length > 100)
{
return "";
}
strvalue = strvalue.PadLeft(100, ' ');
MemoryStream serializationStream = new MemoryStream(0);
new BinaryFormatter().Serialize(serializationStream, strvalue);
byte[] buffer = serializationStream.GetBuffer();
serializationStream.Close();
serializationStream.Dispose();
serializationStream = null;
byte[,] buffer2 = new byte[0x10, 0x10];
for (num = 0; num < 0x10; num++)
{
num2 = 0;
while (num2 < 0x10)
{
buffer2[num, num2] = buffer[(num * 0x10) + num2];
num2++;
}
}
buffer = null;
byte[] inArray = new byte[0x100];
for (num = 0; num < 0x10; num++)
{
for (num2 = 0; num2 < 0x10; num2++)
{
inArray[(num * 0x10) + num2] = (byte) ((buffer2[15 - num, 15 - num2] + num) + num2);
}
}
string s = Convert.ToBase64String(inArray);
inArray = null;
byte[] bytes = Encoding.ASCII.GetBytes(s);
byte[] buffer5 = Encoding.ASCII.GetBytes(key);
for (int i = 0; i < bytes.Length; i++)
{
for (int j = 0; j < buffer5.Length; j++)
{
if ((i + j) < bytes.Length)
{
short num5 = (byte) (bytes[i + j] + buffer5[j]);
bytes[i + j] = (byte) num5;
}
}
}
return Convert.ToBase64String(bytes);
}
internal static string Encrypt(string key, string strvalue)
{
int num;
int num2;
if (strvalue.Length > 100)
{
return "";
}
strvalue = strvalue.PadLeft(100, ' ');
MemoryStream serializationStream = new MemoryStream(0);
new BinaryFormatter().Serialize(serializationStream, strvalue);
byte[] buffer = serializationStream.GetBuffer();
serializationStream.Close();
serializationStream.Dispose();
serializationStream = null;
byte[,] buffer2 = new byte[0x10, 0x10];
for (num = 0; num < 0x10; num++)
{
num2 = 0;
while (num2 < 0x10)
{
buffer2[num, num2] = buffer[(num * 0x10) + num2];
num2++;
}
}
buffer = null;
byte[] inArray = new byte[0x100];
for (num = 0; num < 0x10; num++)
{
for (num2 = 0; num2 < 0x10; num2++)
{
inArray[(num * 0x10) + num2] = (byte) ((buffer2[15 - num, 15 - num2] + num) + num2);
}
}
string s = Convert.ToBase64String(inArray);
inArray = null;
byte[] bytes = Encoding.ASCII.GetBytes(s);
byte[] buffer5 = Encoding.ASCII.GetBytes(key);
for (int i = 0; i < bytes.Length; i++)
{
for (int j = 0; j < buffer5.Length; j++)
{
if ((i + j) < bytes.Length)
{
short num5 = (byte) (bytes[i + j] + buffer5[j]);
bytes[i + j] = (byte) num5;
}
}
}
return Convert.ToBase64String(bytes);
}
上面是加密代码,能写出相应的解密代码吗?如果能用DELPHI写出最好不过。不然就用C#吧。
谢谢各位了,先祝各位新年快乐。
Encrypt(string key, string strvalue) KEY就是密钥变量。
STRVALUE就是加密字符串。
using System.Text;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;static class EncryptAndRavel
{
internal static string Encrypt(string key, string strvalue)
{
int num;
int num2;
if (strvalue.Length > 100)
{
return "";
}
// 补齐100个字符,左侧补空格
strvalue = strvalue.PadLeft(100, ' ');
// 创建内存流serializationStream
MemoryStream serializationStream = new MemoryStream(0);
// 把字符串strvalue序列化为流serializationStream
new BinaryFormatter().Serialize(serializationStream, strvalue);
// 返回从其创建此流的无符号字节数组。
byte[] buffer = serializationStream.GetBuffer();
// 关闭当前流并释放与之关联的所有资源。
serializationStream.Close();
// 释放由 serializationStream 使用的所有资源。
serializationStream.Dispose();
// 通过更改 serializationStream 的值使原有的流失去引用,而被 GC 回收。这 3 句有些重复。
serializationStream = null;
// 创建 16 x 16 的二维数组
byte[,] buffer2 = new byte[0x10, 0x10];
// 复制一维数组每个元素到二维数组
for (num = 0; num < 0x10; num++)
{
num2 = 0;
while (num2 < 0x10)
{
buffer2[num, num2] = buffer[(num * 0x10) + num2];
num2++;
}
}
// 释放一维数组 buffer 占用的内存
buffer = null;
// 创建 256 的一维数组
byte[] inArray = new byte[0x100];
// 倒行,倒序取值,再加 num,再加 num2,再强制类型转换为 byte,溢出引起的丢失数据可以找回。
for (num = 0; num < 0x10; num++)
{
for (num2 = 0; num2 < 0x10; num2++)
{
inArray[(num * 0x10) + num2] = (byte)((buffer2[15 - num, 15 - num2] + num) + num2);
}
}
// 将 8 位无符号整数数组的值转换为它的等效 String 表示形式(使用 base 64 数字编码)。
string s = Convert.ToBase64String(inArray);
// 释放一维数组 inArray 占用的内存
inArray = null;
// 将 s 中的所有字符编码为一个字节序列 bytes。
byte[] bytes = Encoding.ASCII.GetBytes(s);
// 将 key 中的所有字符编码为一个字节序列 buffer5。
byte[] buffer5 = Encoding.ASCII.GetBytes(key);
// 用 key 的每个字节和 s 的每个字节依次相加,s 最后长度不够的一部分不加
// 强制类型转换为 byte,溢出引起的丢失数据可以找回。
for (int i = 0; i < bytes.Length; i++)
{
for (int j = 0; j < buffer5.Length; j++)
{
if ((i + j) < bytes.Length)
{
short num5 = (byte)(bytes[i + j] + buffer5[j]);
bytes[i + j] = (byte)num5;
}
}
}
// 将 8 位无符号整数数组的值转换为它的等效 String 表示形式(使用 base 64 数字编码)。
return Convert.ToBase64String(bytes);
} internal static string Ravel(string key, string encryptedMessage)
{
int num;
int num2;
byte[] buffer5 = Encoding.ASCII.GetBytes(key);
byte[] bytes = Convert.FromBase64String(encryptedMessage); for (int i = 0; i < bytes.Length; i++)
{
for (int j = 0; j < buffer5.Length; j++)
{
if ((i + j) < bytes.Length)
{
bytes[i + j] = (byte)(bytes[i + j] - buffer5[j]);
}
}
}
string s = Encoding.ASCII.GetString(bytes);
bytes = null;
byte[] inArray = Convert.FromBase64String(s);
s = null;
byte[,] buffer2 = new byte[0x10, 0x10];
for (num = 0; num < 0x10; num++)
{
for (num2 = 0; num2 < 0x10; num2++)
{
buffer2[15 - num, 15 - num2] = (byte)((inArray[(num * 0x10) + num2] - num2) - num);
}
}
inArray = null;
byte[] buffer = new byte[0x100];
for (num = 0; num < 0x10; num++)
{
num2 = 0;
while (num2 < 0x10)
{
buffer[(num * 0x10) + num2] = buffer2[num, num2];
num2++;
}
}
buffer2 = null;
MemoryStream serializationStream = new MemoryStream(buffer);
buffer = null;
string strvalue;
strvalue = (string)(new BinaryFormatter().Deserialize(serializationStream));
serializationStream.Close();
serializationStream.Dispose();
serializationStream = null;
strvalue = strvalue.TrimStart(' ');
return strvalue;
}
}class ExampleClass
{
static void Main()
{
string strvalue = "abc";
string key = "1";
string encryptedMessage = EncryptAndRavel.Encrypt(key, strvalue);
strvalue = EncryptAndRavel.Ravel(key, encryptedMessage);
Console.WriteLine(strvalue);
Console.Read();
}
}
他们之间字符串编码就是难题,解决起来太费事。