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…@hotmail.com)
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"
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

Replies