2

I was sent the following code block by a third party client to allow me access some of there web services:

 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(); 

And I have converted it from C# to 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 

The problem is it keeps throwing the following exception:

System.ArgumentException: Offset and length were out of bounds for the array or count is greater than the number of elements from index to the end of the source collection.

I can see what it's doing, trying to address areas of the byte array the don't exists but I can't see why. Unless either the C# code doesn't work or something has gotten lost in the translation. Any suggestions?

Kevin

3
  • which line throws the exception? The BlockCopy? Commented Mar 18, 2014 at 16:13
  • as an aside, you don't need hungarian notation in VB either. Commented Mar 18, 2014 at 16:16
  • The line Dim tempBytes(IIf(intDataLength - intMaxLength * intCounter > intMaxLength, intMaxLength, intDataLength - intMaxLength * intCounter)) As Byte And I do if I wan't to keep my job :-) Commented Mar 18, 2014 at 16:23

1 Answer 1

1

VB arrays are declared using the upper bound, not the length. So use:

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

Also, you should use VB integer division for the following:

Dim iterations As Integer = dataLength \ maxLength 
Sign up to request clarification or add additional context in comments.

4 Comments

Ok, now the line is giving me System.OverflowException: Arithmetic operation resulted in an overflow.
@user1708468: Which line?
@user1708468: The 'tempBytes' declaration or the 'iterations' declaration?
@user1708468: You also need to change the 'iterations' declarations to use VB integer division - when I do that I do not get an OverflowException.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.