dhanasekar
New member
- Joined
- Oct 27, 2005
- Messages
- 2
- Programming Experience
- 1-3
Hi,
I have to generate message digest using SHA1 algorithm to produce 160 bit number. Then message digest is encrypted with signer's private key(using RSA ECB and PKCS#1 v1.5 padding) and convert to printable hex format.
For the above specification i have tried the following code, I am getting Invalid PKCS#1 padding: no leading zero error (java.security.SignatureException, javax.crypto.BadPaddingException:) on the receiver end. They are using Java interface to handle these messages.
Is there any other method to create ECB and PKCS#1 v1.5 padding using .NET ?
Imports System.Security
Imports System.Security.Cryptography
Imports Microsoft.Web.Services.Security
Imports Microsoft.Web.Services.Security.X509
Public Function fnDigitalSign1(ByVal strMessage As String) As String
Private Function fnGetCertificate() As X509Certificate
End Function
Private Function getHEXformat(ByVal encrypted() As Byte) As String
Dim b As Byte
Dim hexString As String
For Each b In encrypted
hexString += String.Format("{0:X2}", b)
Next
Return hexString.ToLower
End Function
'And I am trying with RSACryptoService provider also
Public Function fnDigitalSign2(ByVal strMessage As String) As String
Try
End Try
End Function '
I have to generate message digest using SHA1 algorithm to produce 160 bit number. Then message digest is encrypted with signer's private key(using RSA ECB and PKCS#1 v1.5 padding) and convert to printable hex format.
For the above specification i have tried the following code, I am getting Invalid PKCS#1 padding: no leading zero error (java.security.SignatureException, javax.crypto.BadPaddingException:) on the receiver end. They are using Java interface to handle these messages.
Is there any other method to create ECB and PKCS#1 v1.5 padding using .NET ?
Imports System.Security
Imports System.Security.Cryptography
Imports Microsoft.Web.Services.Security
Imports Microsoft.Web.Services.Security.X509
Public Function fnDigitalSign1(ByVal strMessage As String) As String
Dim
rsaCSP As New System.Security.Cryptography.RSACryptoServiceProvider
Dim toEncrypt() As Byte
Dim encrypted() As Byte
Dim digest() As Byte
Dim enc As New UnicodeEncoding
toEncrypt = enc.GetBytes(strMessage)
'compute hash with algorithm specified as here we have SHA1
Dim sha1Algo As New SHA1Managed
digest = sha1Algo.ComputeHash(toEncrypt)
Dim cert As X509Certificate = fnGetCertificate()
'Here i am getting correct certificate only.
If cert.SupportsDigitalSignature() Then
Dim objRSA As RSAPKCS1SignatureFormatter
objRSA = New RSAPKCS1SignatureFormatter(cert.Key)
objRSA.SetHashAlgorithm("SHA1")
encrypted = objRSA.CreateSignature(digest)
End If
Dim strHEX As String = getHEXformat(encrypted)
End FunctionDim toEncrypt() As Byte
Dim encrypted() As Byte
Dim digest() As Byte
Dim enc As New UnicodeEncoding
toEncrypt = enc.GetBytes(strMessage)
'compute hash with algorithm specified as here we have SHA1
Dim sha1Algo As New SHA1Managed
digest = sha1Algo.ComputeHash(toEncrypt)
Dim cert As X509Certificate = fnGetCertificate()
'Here i am getting correct certificate only.
If cert.SupportsDigitalSignature() Then
Dim objRSA As RSAPKCS1SignatureFormatter
objRSA = New RSAPKCS1SignatureFormatter(cert.Key)
objRSA.SetHashAlgorithm("SHA1")
encrypted = objRSA.CreateSignature(digest)
End If
Dim strHEX As String = getHEXformat(encrypted)
Private Function fnGetCertificate() As X509Certificate
Dim store As X509CertificateStore
store = X509CertificateStore.CurrentUserStore(X509CertificateStore.MyStore)
store.OpenRead()
Dim cert As X509Certificate = store.Certificates(0)
Return cert
store = X509CertificateStore.CurrentUserStore(X509CertificateStore.MyStore)
store.OpenRead()
Dim cert As X509Certificate = store.Certificates(0)
Return cert
Private Function getHEXformat(ByVal encrypted() As Byte) As String
Dim b As Byte
Dim hexString As String
For Each b In encrypted
hexString += String.Format("{0:X2}", b)
Next
Return hexString.ToLower
End Function
'And I am trying with RSACryptoService provider also
Public Function fnDigitalSign2(ByVal strMessage As String) As String
Try
Dim cert As X509Certificate = fnGetCertificate()
'Receiver's Public
Dim receiver_public As RSAParameters = cert.Key.ExportParameters(False)
Dim sender_private As RSAParameters = cert.Key.ExportParameters(True)
Dim toEncrypt() As Byte
Dim encrypted() As Byte
Dim cipher() As Byte
Dim digest() As Byte
toEncrypt = fnGetEncodedBytes(strMessage, SelectEncode.ASCII)
'compute hash with algorithm specified as here we have SHA1
digest = fnComputeSHA1Hash(toEncrypt)
Dim rsaCSP As New System.Security.Cryptography.RSACryptoServiceProvider
rsaCSP.ImportParameters(sender_private)
cipher = rsaCSP.Encrypt(digest, False) 'False to create PKCS#1 padding
Return getHEXformat(cipher)
Catch ex As Exception'Receiver's Public
Dim receiver_public As RSAParameters = cert.Key.ExportParameters(False)
Dim sender_private As RSAParameters = cert.Key.ExportParameters(True)
Dim toEncrypt() As Byte
Dim encrypted() As Byte
Dim cipher() As Byte
Dim digest() As Byte
toEncrypt = fnGetEncodedBytes(strMessage, SelectEncode.ASCII)
'compute hash with algorithm specified as here we have SHA1
digest = fnComputeSHA1Hash(toEncrypt)
Dim rsaCSP As New System.Security.Cryptography.RSACryptoServiceProvider
rsaCSP.ImportParameters(sender_private)
cipher = rsaCSP.Encrypt(digest, False) 'False to create PKCS#1 padding
Return getHEXformat(cipher)
End Try