Tech Off Thread

2 posts

At my wits end. I need some help bad with rsaCryptoProvider and Digital Signatures

Back to Forum: Tech Off
  • User profile image
    katghoti

    I can't believe the run around I am getting on this.  I have to certs I make
    with makecert.  I exported both of them into a folder on the c drive and I
    can get to both.  From the code below, I can create and encrypt a message
    with the public key without a problem, but when I try to decrypt the message
    with the privateCert I get a "bad key" error.  How hard can this be?  I can
    derive the public key from the WsTest.cer file just fine and encrypt, but
    not decrypt it.  What am I doing wrong?  Why can't I use the PFX file to
    decrypt this simple message?

    I am trying to set up the following framework here at work.  I need a
    x509Cert for each web service.  The web service needs to access its cert and
    pass the public key back to the client.  The client will take the public key
    and encrypt both a symmetric key and the payload.  The web service will
    decrypt the symmetric key (using its private key) then using the decrypted
    symmetric key (and IV) decrypt the body.  Then it will continue to use the
    decrypted symmetric key and IV to encrypt the payload and send it back to
    the client which will use its symmetric key to decrypt the payload.

    1.  I can get the public key and encrypt a message
    2.  I cannot get the server to decrypt the payload, I get the "bad key"
    error.

    Any help is much appreciated, I am banging my head against the wall.  Can I
    do this with just the PFX file?

    John


    Code:
    Dim publiccert As New X509Certificate2
    Dim privatecert As New X509Certificate2

    publiccert.Import("c:\certificates\WSTest.cer")

    privatecert.Import("c:\certificates\WSTest.pfx", "password", Nothing)

    Dim rsa As New RSACryptoServiceProvider

    rsa.FromXmlString(publiccert.PublicKey.Key.ToXmlString(False))

    Dim encryptedstr As Byte() =
    rsa.Encrypt(System.Text.Encoding.UTF8.GetBytes(TextBox1.Text), False)

    Dim encryptedString As String =
    System.Text.Encoding.UTF8.GetString(encryptedstr)

    TextBox5.Text = encryptedString

    Dim rsa2 As New RSACryptoServiceProvider

    rsa2.FromXmlString(privatecert.PrivateKey.ToXmlString(False))

    Dim decryptedStr As Byte() =
    rsa2.Decrypt(System.Text.Encoding.UTF8.GetBytes(encryptedString),
    lse)   -->Fails here with "bad key" error

    Dim decryptString As String =
    System.Text.Encoding.UTF8.GetString(decryptedStr)

    TextBox6.Text = decryptString

  • User profile image
    demvin

    Hi, did you find a solution to your problem ? I have the exact same problem.  The only difference is that I have a single X509 certificate which has a public and private key.  I also can encrypt without problem and I get the same error message upon decryption.

    It might be a stupid problem, but without proper documentation from Microsoft, it's hard to solve.  Cryptography is easily the worst documented part of the .NET framework.  It's a real shame.

Comments closed

Comments have been closed since this content was published more than 30 days ago, but if you'd like to continue the conversation, please create a new thread in our Forums, or Contact Us and let us know.