Encrypting string gives different results

≡放荡痞女 提交于 2020-03-06 11:06:15

问题


I've been given the task to make two older systems work together.

The first is written in ASP Classic and the second in C#.

The ASP-C system have generated an encrypted string and stored in the database. Now I need the C# to do the same; providing the str and key should give the same result as the ASP-C.

The str: 1234567890

The key: 12$1$doFmp8gD$7Es.78EumklLvtraylRQy034

The ASP-C encryption function:

Function EncryptString(str, key)

    newStr = ""
    strLen = Len(str)
    keyLen = Len(key)
    keyPos = 1
    str = StrReverse(str)

    For x = 1 To strLen
        newStr = newStr & Chr(Asc(Mid(str, x, 1)) + Asc(Mid(key, keyPos, 1)))
        keyPos = keyPos + 1
        If keyPos > keyLen Then keyPos = 1
    Next

    EncryptString = newStr

End Function

The C# function:

public static string EncryptString(string str, string key) {

    var newStr = "";
    var strLen = str.Length;
    var keyLen = key.Length;
    var keyPos = 0;
    str = ReverseString(str);

    for (var x = 0; x < strLen; x++) {
        int i1 = (int)(Convert.ToChar(str.Substring(x, 1)));
        int i2 = (int)(Convert.ToChar(key.Substring(keyPos, 1)));
        newStr += (char)(i1 + i2);
        keyPos += 1;
        if (keyPos > keyLen) keyPos = 0;
    }

    return newStr;

}

public static string ReverseString(string s) {
    char[] array = s.ToCharArray();
    Array.Reverse(array);
    return new string(array);
}

I'm almost there. Providing the same str/key gives me in this example in the ASP-C ak\hZ™£yŸ¡ while the C# gives me ak\hZ£y¡.

I suspect it to be due to the C#'s current encoding (UTF-8), that not all chars can be written correctly and therefore is printed as whitespace, but I'm not sure how to get around this. I've tried all (or at least most) encoding conversions, but I haven't found the solution.

Please help.

来源:https://stackoverflow.com/questions/60123531/encrypting-string-gives-different-results

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!