【发布时间】: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 < 256; i++) { test += Convert.ToChar(i); } Console.WriteLine(test);测试它
标签: c# .net vb.net encryption ascii