110,567
社区成员
发帖
与我相关
我的任务
分享
For cec As Integer = 1 To CodeStr.Length
Try
NewCode = NewCode & CfsCode(CodeStr.Substring(cec - 1, 3))
Catch ex As Exception
NewCode = NewCode & CfsCode(CodeStr.Substring(cec - 1))
End Try
End Try
Next
你改成这样试下
For cec As Integer = 1 To CodeStr.Length
Try
NewCode = NewCode & CfsCode(CodeStr.Substring(cec - 1, 3))
Catch ex As Exception
NewCode = NewCode & CfsCode(CodeStr.Substring(cec - 1, 1))
End Try
End Try
Next
VB版本出现你那个问题可能是在这里截取字符串的时候,按照你那个算法没有中间截取2这个实现,当然你那个算法的原理我还不清楚,只是根据你的代码转换成C#而已,有这个算法原理解析的链接不妨发我一个
class Program
{
private static String EnPas(String CodeStr)
{
int CodeLen;
int CodeSpace;
double NewCode;
int cecr, cecb, cec;
CodeLen = 30;
CodeSpace = CodeLen - CodeStr.Length;
if (CodeSpace >= 1)
{
for (cecr = 1; cecr <= CodeSpace; cecr++)
CodeStr = CodeStr + (char)21;
}
NewCode = 1;
long Been = 0;
for (cecb = 1; cecb <= CodeLen; cecb++)
{
Been = CodeLen + ((int)Convert.ToChar(CodeStr.Substring(cecb - 1, 1))) * cecb;
NewCode = Convert.ToDouble(NewCode * Been);
}
CodeStr = NewCode.ToString();
string NewCodeStr = "";
for (cec = 1; cec <= CodeStr.Length; cec++)
{
if (CodeStr.Length - cec < 3)
NewCodeStr += CfsCode(CodeStr.Substring(cec - 1));
else
NewCodeStr += CfsCode(CodeStr.Substring(cec - 1, 3));
}
string EnPas = "";
for (cec = 20; cec <= NewCodeStr.Length - 18; cec += 2)
EnPas = EnPas + NewCodeStr.Substring(cec - 1, 1);
return EnPas.ToUpper();
}
static string CfsCode(string Word)
{
string CfsCode = "";
for (int i = 0; i < Word.Length; i++)
{
CfsCode = CfsCode + (int)Convert.ToChar(Word.Substring(i, 1));
}
CfsCode = string.Format("{0:X}", int.Parse(CfsCode));
return CfsCode;
}
static void Main(string[] args)
{
string md5 = "d99e33a642209c61";
Console.Write("Input:" + md5);
Console.WriteLine();
Console.Write("Ouput:" + EnPas("d99e33a642209c61"));
Console.WriteLine();
Console.WriteLine();
Console.Write("Input:" + "123");
Console.WriteLine();
Console.Write("Ouput:" + EnPas("123"));
Console.ReadLine();
}
}
'加密函数
Function EnPas(ByVal CodeStr As String) As String
Dim CodeLen As Integer = 30
Dim CodeSpace, Been, NewCodeInt As Integer
Dim EnPasStr As String = ""
Dim NewCode As String
CodeSpace = CodeLen - CodeStr.Length
If Not CodeSpace < 1 Then
For cecr As Integer = 1 To CodeSpace
CodeStr = CodeStr & Chr(21)
Next
End If
NewCodeInt = 1
NewCode = 1
For cecb As Integer = 1 To CodeLen
Been = CodeLen + Asc(CodeStr.Substring(cecb - 1, 1)) * cecb
NewCode = NewCode * Been
Next
CodeStr = NewCode
NewCode = Nothing
For cec As Integer = 1 To CodeStr.Length
Try
NewCode = NewCode & CfsCode(CodeStr.Substring(cec - 1, 3))
Catch ex As Exception
Try
NewCode = NewCode & CfsCode(CodeStr.Substring(cec - 1, 2))
Catch exs As Exception
NewCode = NewCode & CfsCode(CodeStr.Substring(cec - 1, 1))
End Try
End Try
Next
For cec As Integer = 20 To NewCode.Length - 18 Step 2
EnPasStr = EnPasStr & NewCode.Substring(cec - 1, 1)
Next
Return EnPasStr
End Function
'加密子函数
Function CfsCode(ByVal Word As String) As String
Dim CfsCodeStr As String = ""
For cc As Integer = 1 To Word.Length
CfsCodeStr = CfsCodeStr & Asc(Word.Substring(cc - 1, 1))
Next
CfsCodeStr = Hex(CfsCodeStr)
Return CfsCodeStr
End Function
我用C#调用这个VB函数最后算出来的结果和原来算出来的老是最后5位不一样,而且奇怪的是有时候用其它的字符串加密完会一样,再换别的字符串又不一样,我测试了6个字符串,有两个一样,有4个后5位不一样,蛋疼了!