Tech Off Thread

2 posts

Forum Read Only

This forum has been made read only by the site admins. No new threads or comments can be added.

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

Back to Forum: Tech Off
  • User profile image

    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"

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


    Dim publiccert As New X509Certificate2
    Dim privatecert As New X509Certificate2


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

    Dim rsa As New RSACryptoServiceProvider


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

    Dim encryptedString As String =

    TextBox5.Text = encryptedString

    Dim rsa2 As New RSACryptoServiceProvider


    Dim decryptedStr As Byte() =
    lse)   -->Fails here with "bad key" error

    Dim decryptString As String =

    TextBox6.Text = decryptString

  • User profile image

    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.

Conversation locked

This conversation has been locked by the site admins. No new comments can be made.