【问题标题】:Encryption difference after converting VB to C#VB转C#后的加密差异
【发布时间】:2016-01-21 09:37:55
【问题描述】:

正在尝试将代码从 VB 转换为 C#,请参阅下面的 VB 代码

Dim StrCount As Int16
Dim str1, str2, EncryptedStr As String
EncryptedStr = String.Empty
theString = "Test@1234"

For StrCount = 1 To Len(theString)
    str1 = Asc(Mid(theString, StrCount, 1)) - 1
    str2 = str1 + Asc(Mid(StrCount, 1))
    EncryptedStr = EncryptedStr + Chr(str2)
Next

转换后的 C# 代码

string EncryptedStr = string.Empty;
Encoding encode1 = Encoding.ASCII;
Byte[] encodedBytes = encode1.GetBytes("Test@1234");  

for (int count = 0; count < encodedBytes.Length; count++)
{
    int str1 = encodedBytes[count] - 1;
    Encoding encode2 = Encoding.ASCII;
    Byte[] encodedBytes1 = encode2.GetBytes((count + 1).ToString());  
    int str2 = str1 + (int)encodedBytes1[0];
    EncryptedStr += Convert.ToChar(str2);
}

它工作正常,但我面临的问题是加密密码在 VB 和 C# 中不同

我尝试加密一个字符串“Test@1234”,加密的结果是

VB: „–¥§tfhjl

C#: ¥§tfhjl

我调试并注意到在 C# Convert.ToChar(132) & Convert.ToChar(150) 中给出的是空值。

谁能解释一下,这里出了什么问题

【问题讨论】:

  • 是否仅与不均匀字符数的字符串不同?
  • @FalcoAlexander:不,它不仅对偶数或不偶数字符串有所不同。
  • 我用 LinqPad 检查了两个代码示例,它们都返回相同的字符串...
  • 就个人而言,我确实认为“encryption”这个词在这里有些误导。
  • 取决于您的语言环境/区域设置:可能是您通过 c# sn-p string test="Test:"; for (int i = 1; i &lt; 256; i++) { test += Convert.ToChar(i); } Console.WriteLine(test); 测试它

标签: c# .net vb.net encryption ascii


【解决方案1】:

正如此答案here 的评论中所解释的,VB.NET 在当前 Windows 代码页中返回 ANSI 代码,而不是 ASCII 代码。不要期望得到相同的输出,除非您使用相同的函数,引用Microsoft.VisualBasic 并使用Strings.AscStrings.Chr 以获得相同的结果。

【讨论】:

    【解决方案2】:

    最简单且与原始代码相同的解决方案是使用VisualBasic 命名空间中的AscChr 方法,因为它们具有相同的功能。

    但是

    Asc 使用 ANSI,它可以在不同的语言环境和不同的机器上改变,所以如果你真的很固执,你可以尝试通过显式定义要使用的编码来模拟这一点。

    这会在 my 机器上产生相同的结果(但要小心在其他机器上进行测试):

    Public Function EncryptString(aString As String) As String
        Dim sb As New StringBuilder
        Dim enc = Encoding.GetEncoding("windows-1252")
        For i = 0 To aString.Length - 1
            Dim x = enc.GetBytes(aString.Substring(i, 1))(0) - 1
            Dim y = enc.GetBytes((i + 1).ToString)(0) + x
            Dim b() As Byte = {y}
            sb.Append(enc.GetString(b))
        Next
        Return sb.ToString
    End Function
    

    因此(未经测试的)C# 等效项是:

    public string EncryptString(string aString)
    {
        StringBuilder sb = new StringBuilder();
        var enc = Encoding.GetEncoding("windows-1252");
        for (var i = 0; i < aString.Length; i++)
        {
            var x = enc.GetBytes(aString.Substring(i, 1))[0] - 1;
            var y = enc.GetBytes((i + 1).ToString())[0] + x;
            byte[] b = {y};
            sb.Append(enc.GetString(b));
        }
        return sb.ToString();
    }
    

    【讨论】:

    • 我会尽力让你知道
    • 我进行了更改以明确指定编码,因此应该更接近地匹配 Asc
    • 1) 谢谢 2) 无法将 int 隐式转换为字节,因此修改了行 byte[] b = BitConverter.GetBytes(y); 3)我得到的结果是双引号“我完全按照你说的做了......
    【解决方案3】:

    vb.net 遗留的 Asc 函数,确实是使用 System.Text.Encoding.Default。在您的 C# 版本中,您使用的是 ASCII。检查:

            string EncryptedStr = string.Empty;
            Encoding encode1 = Encoding.Default; //.ASCII;
            Byte[] encodedBytes = encode1.GetBytes("Test@1234");
    
            for (int count = 0; count < encodedBytes.Length; count++)
            {
                int str1 = encodedBytes[count] - 1;
                Encoding encode2 = Encoding.Default;
                Byte[] encodedBytes1 = encode2.GetBytes((count + 1).ToString());
                int str2 = str1 + (int)encodedBytes1[0];
                EncryptedStr += Convert.ToChar(str2);
            }
    

    【讨论】:

    • 不完全正确。 Asc 使用 System.Text.SBCSCodePageEncoding,但在单声道中,在 Ubuntu 12.04 上,System.Text.Encoding.Default 是 System.Text.Encoding.UTF8,而在 Windows 7 上,它似乎是 System.Text.SBCSCodePageEncoding。
    • @MattWilko 好的,但我不是说,我说他在 Vb.net 中使用默认编码(无论哪种),并在 C# 上强制使用 ASCII。
    • @NKhan -1 没有更多关于“它不工作”的信息,这不是改善答案的好动机
    • Encoding.Default 不一定与 Asc(使用 ANSI)相同,因为默认值可以在不同的语言环境和机器上更改
    • @MattWilko aaah 我现在明白你了。同意,谢谢解释
    猜你喜欢
    • 2023-03-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-11-20
    相关资源
    最近更新 更多