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

时间:2018-06-15 22:55:43

标签: vb.net argumentexception

执行处理加密的代码后,我收到以下错误:

  

mscorlib.dll中出现未处理的“System.ArgumentException”类型异常   附加信息:“System.Int64”类型的对象无法转换为“System.UInt32”类型。

发生类似错误的一些来源(例如http://truncatedcodr.wordpress.com/2012/06/20/fix-sitecore-and-net-framework-4-5/)说要在Web.config中将值更改为以下内容(请原谅“小于”符号后面的空格):

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

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

Dim stream As IO.FileStream Dim dataOffset As Int64 Dim dataSize As Int64 Dim del2 As New ChangeProgress2Delegate(AddressOf ChangeProgress2)

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)

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)

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

1 个答案:

答案 0 :(得分:0)

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

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

即使iUInt32jUInt32numPartitionsUInt32,我也必须这样做。