日期:2014-05-20  浏览次数:20750 次

加密算法
请问这个是什么加密算法,谁可以帮我翻译成c#,我翻译老不对,里面有些函数c#里没有,如asc

Function   crypt(Action   As   String,   Key   As   String,   Src   As   String)   As   String
Dim   Count   As   Integer,   KeyPos   As   Integer,   KeyLen   As   Integer,   SrcAsc   As   Integer,   dest   As   String,   offset   As   Integer,   TmpSrcAsc,   SrcPos
KeyLen   =   Len(Key)
If   Action   =   "E "   Then
Randomize
offset   =   (Rnd   *   10000   Mod   255)   +   1
dest   =   Hex$(offset)
If   Len(dest)   =   1   Then
dest   =   "0 "   +   dest
End   If
For   SrcPos   =   1   To   Len(Src)
SrcAsc   =   (Asc(Mid$(Src,   SrcPos,   1))   +   offset)   Mod   255
If   KeyPos   <   KeyLen   Then   KeyPos   =   KeyPos   +   1   Else   KeyPos   =   1
SrcAsc   =   SrcAsc   Xor   Asc(Mid$(Key,   KeyPos,   1))
dest   =   dest   +   Format$(Hex$(SrcAsc),   "@@ ")
offset   =   SrcAsc
Next
ElseIf   Action   =   "D "   Then
offset   =   Val( "&H "   +   Left$(Src,   2))
For   SrcPos   =   3   To   Len(Src)   Step   2
SrcAsc   =   Val( "&H "   +   Trim(Mid$(Src,   SrcPos,   2)))
If   KeyPos   <   KeyLen   Then   KeyPos   =   KeyPos   +   1   Else   KeyPos   =   1
TmpSrcAsc   =   SrcAsc   Xor   Asc(Mid$(Key,   KeyPos,   1))
If   TmpSrcAsc   <=   offset   Then
TmpSrcAsc   =   255   +   TmpSrcAsc   -   offset
Else
TmpSrcAsc   =   TmpSrcAsc   -   offset
End   If
dest   =   dest   +   Chr(TmpSrcAsc)
offset   =   SrcAsc
Next
End   If
crypt   =   dest
End   Function


------解决方案--------------------
//考虑字符中会出现汉字的情况,参考如下代码:

private string fCrypt(string Action, string Key, string Src)
{
int vKeyLen = Key.Length;
int vKeyPos = 0;
byte[] vKeyBuffer = Encoding.UTF8.GetBytes(Key);
if (Action == "E ")
{
byte[] vSrcBuffer = Encoding.UTF8.GetBytes(Src);
string vReturn = " ";
Random vRandom = new Random();
byte vOffset = (byte)vRandom.Next(256);
vReturn = vOffset.ToString( "X2 ");
foreach (byte vByte in vSrcBuffer)
{
byte vTemp = (byte)((vByte + vOffset) % 255);
vTemp ^= vKeyBuffer[vKeyPos];
vKeyPos = (vKeyPos + 1) % vKeyLen;
vReturn += vTemp.ToString( "X2 ");
vOffset = vTemp;
}
return vReturn;
}
else if (Action == "D ")
{
byte[] vDest = new byte[(Src.Length - 2) / 2];
int vOffset = int.Parse(Src.Substring(0, 2),
System.Globalization.NumberStyles.HexNumber);