VB改成C#高手帮忙,能帮忙解译一下也好,多谢
VB改成C#,高手帮忙,能帮忙解译一下也好,谢谢!
Function Encrypt(str, str2)
Dim prand, i, sPos, mult, incr, modu, enc_chr, enc_str, salt
salt = ""
For i = 1 To Len(str2)
prand = prand & Asc(Mid(str2, i, 1))
salt = salt & Right("00" & Hex(Asc(Mid(str2, i, 1))), 2)
Next
sPos = Len(prand) \ 4
mult = Mid(prand, sPos, 1) + Mid(prand, sPos * 2, 1) + Mid(prand, sPos * 3, 1) + Mid(prand, sPos * 4, 1)
incr = CLng(Len(salt) / 2)
modu = 2 ^ 15 - 1
While Len(prand) > 5
prand = "" & (CLng(Mid(prand, 1, 5)) + CLng(Mid(prand, 5, Len(prand))))
Wend
prand = (mult * prand + incr) Mod modu
enc_chr = ""
enc_str = ""
For i = 1 To Len(str)
enc_chr = Right("0000" + Hex(AscW(Mid(str, i, 1)) Xor (prand / modu) * 65535), 4)
enc_str = enc_str + enc_chr
prand = (mult * prand + incr) Mod modu
Next
enc_str = Right("00" & Hex(Len(salt)), 2) & enc_str & salt
Encrypt = enc_str
End Function
------解决思路----------------------
Function Encrypt(str, str2)
Dim prand, i, sPos, mult, incr, modu, enc_chr, enc_str, salt
salt = ""
For i = 1 To Len(str2)
prand = prand & Asc(Mid(str2, i, 1))
salt = salt & Right("00" & Hex(Asc(Mid(str2, i, 1))), 2)
Next
sPos = Len(prand) \ 4
mult = Mid(prand, sPos, 1) + Mid(prand, sPos * 2, 1) + Mid(prand, sPos * 3, 1) + Mid(prand, sPos * 4, 1)
incr = CLng(Len(salt) / 2)
modu = 2 ^ 15 - 1
While Len(prand) > 5
prand = "" & (CLng(Mid(prand, 1, 5)) + CLng(Mid(prand, 5, Len(prand))))
Wend
prand = (mult * prand + incr) Mod modu
enc_chr = ""
enc_str = ""
For i = 1 To Len(str)
enc_chr = Right("0000" + Hex(AscW(Mid(str, i, 1)) Xor (prand / modu) * 65535), 4)
enc_str = enc_str + enc_chr
prand = (mult * prand + incr) Mod modu
Next
enc_str = Right("00" & Hex(Len(salt)), 2) & enc_str & salt
Encrypt = enc_str
End Function
------解决思路----------------------
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine(Encrypt("1234567890", "abc"));
Console.ReadLine();
}
static string Encrypt(string str, string str2)
{
string prand="", salt="";
byte[] str2Bytes = Encoding.ASCII.GetBytes(str2);
for (int i = 0; i < str2Bytes.Length; i++)
{
prand += str2Bytes[i].ToString();
salt += str2Bytes[i].ToString("X2");
}
int sPos = prand.Length / 4;
int mult = int.Parse(new string(new char[]{prand[sPos - 1] , prand[sPos * 2 - 1] , prand[sPos * 3 - 1] , prand[sPos * 4 - 1]}));
int incr = salt.Length / 2;
int modu = 0x7fff;
while (prand.Length >5) {
int v=int.Parse(prand.Substring(0, 5)) + int.Parse(prand.Substring(4));
prand = v.ToString();
}
int iprand = (mult * int.Parse(prand) + incr) % modu;//prand后面都是作为数值进行运算了,用变量iprand代替//
string enc_chr = "", enc_str = "";
byte[] strBytes = Encoding.Unicode.GetBytes(str);
for (int i = 0; i < strBytes.Length; i+=2)
{
int v1 =(int)strBytes[i] + (int)strBytes[i + 1] * 256;
double v2 =(1.0 *iprand / modu) * 65535;
int v3 = v1 ^ (int)Math.Round(v2);
//DEBUG: Console.WriteLine("v1={0} v2={1} v3={2}", v1, v2, v3);
enc_chr = v3.ToString("X4");
enc_str += enc_chr;
iprand = (mult * iprand + incr) % modu;
}
enc_str = salt.Length.ToString("X2")+enc_str +salt;
return enc_str;
}
}
}
064F3B305621E50E9CF66269044F11602C5A011660616263