为什么 System.ArgumentException 出现在密码代码行的第二次或第三次迭代中?

Why is a System.ArgumentException occurring on the second or third iteration of a cryptographic line of code?

执行处理密码学的代码后出现以下错误:

An unhandled exception of type 'System.ArgumentException' occurred in mscorlib.dll Additional information: Object of type 'System.Int64' cannot be converted to type 'System.UInt32'.

一些发生类似错误的来源(例如 http://truncatedcodr.wordpress.com/2012/06/20/fix-sitecore-and-net-framework-4-5/ )说要将 Web.config 中的值更改为以下值(请原谅 "less than" 之后的 space符号):

< setting name=”Login.RememberLastLoggedInUserName” value=”false” />

我以前从未使用过 Web.config 文件,所以这可能是解决方案。但是,我的项目没有一个(我不确定它是否可以有一个,因为我认为它没有网站名称)而且我不知道为什么在上一次迭代中取得了明显的成功(s ).无论如何,这是我的代码中包含问题的部分(我删除了一些不相关的代码):

暗流为 IO.FileStream 将 dataOffset 调暗为 Int64 将数据大小调暗为 Int64 Dim del2 作为新的 ChangeProgress2Delegate(AddressOf ChangeProgress2)</p> <pre><code>Private Sub Form1_Load(sender As Object, e As EventArgs) Handles Me.Load Dim fileDialog As New OpenFileDialog() fileDialog.ShowDialog() stream = New IO.FileStream(fileDialog.FileName, IO.FileMode.Open, IO.FileAccess.ReadWrite) Dim t As New Threading.Tasks.Task(AddressOf Perform) t.Start() End Sub Public Sub Perform() Dim Key(15) As [Byte] Dim Bytes(15) As [Byte] Dim IV(15) As [Byte] Dim userData(31743) As [Byte] Dim encryption2 As Security.Cryptography.Aes Dim decryptor2 As Security.Cryptography.ICryptoTransform Dim msDecrypt2 As IO.MemoryStream Dim csDecrypt2 As Security.Cryptography.CryptoStream For i As UInt32 = 0 To 3 . . . For j As UInt32 = 0 To (numPartitions - 1) . . . dataOffset = Convert.ToUInt32((Convert.ToUInt64(stream.ReadByte()) << 24) + (Convert.ToUInt64(stream.ReadByte()) << 16) + (Convert.ToUInt64(stream.ReadByte()) << 8) + Convert.ToUInt64(stream.ReadByte())) >> 2 dataSize = Convert.ToUInt32((Convert.ToUInt64(stream.ReadByte()) << 24) + (Convert.ToUInt64(stream.ReadByte()) << 16) + (Convert.ToUInt64(stream.ReadByte()) << 8) + Convert.ToUInt64(stream.ReadByte())) >> 2 stream.Position = dataOffset For k = 0 To ((dataSize / 32768) - 1) stream.Position += 976 stream.Read(IV, 0, 16) stream.Position += 32 stream.Read(userData, 0, 31744) encryption2 = Security.Cryptography.Aes.Create() encryption2.Key = Key encryption2.IV = IV encryption2.Padding = Security.Cryptography.PaddingMode.None decryptor2 = encryption2.CreateDecryptor(encryption2.Key, encryption2.IV) msDecrypt2 = New IO.MemoryStream(userData) csDecrypt2 = New Security.Cryptography.CryptoStream(msDecrypt2, decryptor2, Security.Cryptography.CryptoStreamMode.Read) csDecrypt2.Read(userData, 0, 31744) stream.Position -= 31744 stream.Write(userData, 0, 31744) Debugger.Break() Me.BeginInvoke(del2, {i + 1, j + 1, numPartitions}) Next Next Next End Sub Public Sub ChangeProgress2(ByVal partitionOrder As UInt32, ByVal partitionNum As UInt32, ByVal numPartitions As UInt32) Progress.Value = ((stream.Position - dataOffset) / dataSize) * 100 End Sub Public Delegate Sub ChangeProgress2Delegate(ByVal partitionOrder As UInt32, ByVal partitionNum As UInt32, ByVal numPartitions As UInt32)

在上面的代码中,错误在第二次或第三次迭代的这三行代码之间随机触发:

decryptor2 = encryption2.CreateDecryptor(encryption2.Key, encryption2.IV) msDecrypt2 = New IO.MemoryStream(userData) csDecrypt2 = New Security.Cryptography.CryptoStream(msDecrypt2, decryptor2, Security.Cryptography.CryptoStreamMode.Read)

我发现了错误。它与任何密码学无关,即使异常是围绕这些代码行触发的。在 BeginInvoke 行,我需要将参数转换为 UInt32 以便代码看起来像:

Me.BeginInvoke(del2, {Convert.ToUInt32(i + 1), Convert.ToUInt32(j + 1), Convert.ToUInt32(numPartitions)})

我不得不这样做,即使 iUInt32jUInt32numPartitionsUInt32.