public static string passport_encrypt(string txt, string key)
{
// 使用随机数发生器产生 0~32000 的值并 MD5()
Random rnd = new Random(100);
int int_encrypt_key = rnd.Next(0, 32000);
string encrypt_key = int_encrypt_key.ToString();
int ctr = 0;
string tmp = "";
byte[] encodebyte = new byte[txt.Length];
for (int i = 0; i < txt.Length; i++)
{
ctr = ctr == encrypt_key.Length ? 0 : ctr;
char prefix = encrypt_key[ctr];
char left = txt[i];
char right = encrypt_key[ctr++];
encodebyte[i] = (byte)(left ^ right);
char[] block = new char[2];
block[0] = prefix;
block[1] = Convert.ToChar(encodebyte[i]);
tmp += new string(block);
}
//////用base64编码
string complicatedstring = passport_key(tmp, key);/////先和key加一次密?
byte[] inputbytes = System.Text.Encoding.Default.GetBytes(complicatedstring);
string string_64 = Convert.ToBase64String(inputbytes, 0, inputbytes.Length);/////再和随机key加密
return string_64;
}
public static string passport_decrypt(string txt, string key)
{
byte[] array_64 = Convert.FromBase64String(txt);
string txt_64 = System.Text.Encoding.Default.GetString(array_64);
txt = passport_key(txt_64, key);
string temp = "";
byte[] encodebyte = new byte[txt.Length];
for (int i = 0; i < txt.Length; i++)
{
char left = txt[i];
char right = txt[++i];
encodebyte[i] = (byte)(left ^ right);
}
temp = System.Text.Encoding.Default.GetString(encodebyte);
return temp;
}