I'm getting the following error after executing code that deals with cryptography:
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'.
A few sources (e.g. http://truncatedcodr.wordpress.com/2012/06/20/fix-sitecore-and-net-framework-4-5/ ) where a similar error occurred say to change a value to the following in Web.config (pardon the space after the "less than" sign):
< setting name=”Login.RememberLastLoggedInUserName” value=”false” />
I have never used a Web.config file before, so this could be the solution. However, my project doesn't have one (I'm not sure if it could have one since I don't think it has a web site name) and I don't know why there were apparent successes in the previous iteration(s). Anyway, here is the portion of my code that contains the problem (I took out some irrelevant code):
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)
In the code above, the error has been firing somewhat randomly between these three lines of code on the second or third iteration:
decryptor2 = encryption2.CreateDecryptor(encryption2.Key, encryption2.IV)
msDecrypt2 = New IO.MemoryStream(userData)
csDecrypt2 = New Security.Cryptography.CryptoStream(msDecrypt2, decryptor2, Security.Cryptography.CryptoStreamMode.Read)
I found the error. It didn't have to do with any cryptography even though the exception was firing around those lines of code. At the BeginInvoke
line, I needed to convert the parameters into UInt32
so that code looked like:
Me.BeginInvoke(del2, {Convert.ToUInt32(i + 1), Convert.ToUInt32(j + 1), Convert.ToUInt32(numPartitions)})
I had to do this even though i
was a UInt32
, j
was a UInt32
, and numPartitions
was a UInt32
.