gpt4 book ai didi

c# - RSACryptoServiceProvider 从 C# 到 VB 的转换

转载 作者:太空宇宙 更新时间:2023-11-03 13:25:52 25 4
gpt4 key购买 nike

第三方客户端向我发送了以下代码块,以允许我访问其中的某些 Web 服务:

    RSACryptoServiceProvider rsaCryptoServiceProvider = new
RSACryptoServiceProvider(dwKeySize);
rsaCryptoServiceProvider.FromXmlString(xmlString);
int keySize = dwKeySize / 8;
byte[] bytes = Encoding.UTF32.GetBytes(inputString);
int maxLength = keySize - 42;
int dataLength = bytes.Length;
int iterations = dataLength / maxLength;
StringBuilder stringBuilder = new StringBuilder();
for (int i = 0; i <= iterations; i++)
{
byte[] tempBytes = new byte[(dataLength - maxLength * i > maxLength) ?
maxLength : dataLength - maxLength * i];
Buffer.BlockCopy(bytes, maxLength * i, tempBytes, 0, tempBytes.Length);
byte[] encryptedBytes = rsaCryptoServiceProvider.Encrypt(tempBytes, true);
Array.Reverse(encryptedBytes);
stringBuilder.Append(Convert.ToBase64String(encryptedBytes));
}
return stringBuilder.ToString();

我已经将它从 C# 转换为 VB.Net:

    Dim objEncrypter As New RSACryptoServiceProvider(Me.m_intKeySize)
objEncrypter.FromXmlString(m_strEncryptionString)
Dim intKeySize = Me.m_intKeySize / 8
Dim objByte() As Byte = Encoding.UTF32.GetBytes(p_strXMLString.InnerXml)
Dim intMaxLength As Integer = intKeySize - 42
Dim intDataLength As Integer = objByte.Length
Dim intIterations As Integer = intDataLength / intMaxLength
Dim strResult As StringBuilder = New StringBuilder

For intCounter As Integer = 0 To intIterations
Dim tempBytes(IIf(intDataLength - intMaxLength * intCounter > intMaxLength, intMaxLength, intDataLength - intMaxLength * intCounter)) As Byte
Buffer.BlockCopy(objByte, intMaxLength * intCounter, tempBytes, 0, tempBytes.Length)
Dim objEncryptedBytes() As Byte = objEncrypter.Encrypt(tempBytes, True)
Array.Reverse(objEncryptedBytes)
strResult.Append(Convert.ToBase64String(objEncryptedBytes))
Next

Return strResult.ToString

问题是它一直抛出以下异常:

System.ArgumentException: 偏移量和长度超出数组范围或计数大于从索引到源集合末尾的元素数。

我可以看到它在做什么,试图寻址字节数组中不存在的区域,但我不明白为什么。除非 C# 代码不起作用或在翻译中丢失了某些内容。有什么建议吗?

凯文

最佳答案

VB 数组使用上限声明,而不是长度。所以使用:

Dim tempBytes(If(dataLength - maxLength * i > maxLength, maxLength, dataLength - maxLength * i) - 1) As Byte

此外,您应该对以下内容使用 VB 整数除法:

Dim iterations As Integer = dataLength \ maxLength

关于c# - RSACryptoServiceProvider 从 C# 到 VB 的转换,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22484843/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com