Ugh, at my whits end and in desperate need of help.

Giganews Newsgroups
Subject: Ugh, at my whits end and in desperate need of help.
Posted by:  John Wright (riley_wrigh…
Date: Thu, 28 Jun 2007

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