MD5 with RSA Signing - Java DotNet interoperability

lupefe

New member
Joined
Oct 22, 2010
Messages
1
Programming Experience
10+
Hello,

From my 2.0 .Net Framework App I need to sign a string with a X.509 certificate and the encryption algorithm for signing should be MD5/RSA. The resulting signature must be verified in a remote java app. Here's my class with SignMessage and VerifyMessage methods:


Imports System.Security.Cryptography
Imports System.Text

Public Class MBIMCrypto


Private _Encoding As UTF8Encoding

Public Sub New()


_Encoding = New UTF8Encoding

End Sub



Public Function SignMessage(ByVal Message As String) As Byte()

Try

'----
' Instantiate X509Certificate using file path
'Dim x509 As New X509Certificates.X509Certificate2(My.Settings.CertificatePath, "dfd")
Dim x509 As New X509Certificates.X509Certificate2(My.Settings.CertificatePath)

'Dim Encoding As Encoding.Unicode

'----
' Convert Message to byte array
Dim data As Byte() = _Encoding.GetBytes(Message)

'----
' Instantiate a RSA Algorithm object with Private Key

Dim rsa As RSACryptoServiceProvider = DirectCast(x509.PrivateKey, RSACryptoServiceProvider)

Dim p As New System.Security.Cryptography.CspParameters


'----
' Sign it
' New MD5CryptoServiceProvider -> Instantiate the hash Algorithm to create the hash value.
Dim signature() As Byte = rsa.SignData(data, "MD5")

Console.WriteLine("KeyExchangeAlgorithm {0} :", rsa.KeyExchangeAlgorithm)

'---
' Encode the Signature
Dim Base64EncodededSignatureString As String = Convert.ToBase64String(signature, Base64FormattingOptions.None)

'----
' Return it as byte array
Return _Encoding.GetBytes(Base64EncodededSignatureString)


Catch ex As Exception

Throw ex

End Try

End Function


Public Function VerifyMessage(ByVal Message As String, ByVal signature() As Byte) As Boolean

Try

'---
' Get String form the siganture
Dim strSignatureToVery As String = _Encoding.GetString(signature)

'----
' 64Base Uncode the string signature
Dim DecodededSignature As Byte() = Convert.FromBase64String(strSignatureToVery)

'----
' Convert to byte array the orignal Message string
Dim Data As Byte() = _Encoding.GetBytes(Message)

'----
' Instantiate X509Certificate using file path
Dim x509 As New X509Certificates.X509Certificate2(My.Settings.CertificatePath)

'----
' Instantiate a RSA Algorithm object with Public Key
Dim rsa As RSACryptoServiceProvider = DirectCast(x509.PublicKey.Key, RSACryptoServiceProvider)

'---
' Verify Signature
' New MD5CryptoServiceProvider -> Instantiate the hash Algorithm to create the hash value.
'Return rsa.VerifyData(Data, New MD5CryptoServiceProvider, DecodededSignature)
Return rsa.VerifyData(Data, "MD5", DecodededSignature)

Catch ex As Exception

Return False

End Try

End Function

End Class

Here's an example of the strings I'm trying to sign :"bimusermbim_0300400000000000001CLI00300BIMSMS8240710051013089996019779996019779311720101011T16:30:16+0200"

And the resulting base64 signature is : "F4kFnD6K1AaqlO/AJ+UJd+40EIg+DCmOr9BgASGFSevf5ocr7BaKsr9sS107KdFGN6V+DZur+7ZGaiIsEIOwLph3L28sy/6m+Va0g+zWdcTpg+FAkuFI8MCULuYHNA8qPC+qdwSMnYS9fjAgS1boSyGe4+1dopdPiizyxLbEnE4="

The remote java application is from another company, with which we need to exchange this signatures, and the java side the Encryption algorythm object is instantiated with the folowing Signature.getInstance("MD5withRSA"). And we both share the same X.509 certificate used in the signing mechanism.

I'm able to sign and verify with my previous methods on my 2.0 .Net environment , but when I pass the resulting signature to the Remote Java App it fails.

How can I achieve this interoperability? Is my SignMessage method signing the string correctly?

Any help would be appreciatted,

Luis Pedro Ferreira
 
Back
Top