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
BlockCopy?