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



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


.



Relevant Pages

  • Re: More on learning "Public Key Authentication"
    ... let me say that in public key ... >> encrypt the result with Alice's public key. ... >> is sent to Alice who decrypts the message with her private key (which ... > encrypted with my private key and they can then decrypt it with the ...
    (comp.sys.mac.system)
  • Re: how to have a gpg public key?
    ... Having just a public key doesn't do you much good. ... You need both a private key and a public key; ... can encrypt and decrypt your messages and you are just ...
    (Debian-User)
  • [OT] Re: Basic question about Public Private Key Pairs
    ... > and private keys allow me to decrypt, but vice versa is not possible (or ... a public key and a corresponding private key. ... You can encrypt something with each key; ...
    (microsoft.public.dotnet.security)
  • Re: private to public decrypt now working
    ... would have the private key, all vice presidents would have a public key. ... All vice's could encrypt and only president could decrypt. ...
    (microsoft.public.dotnet.security)
  • Re: RSACryptoServiceProvider decrypt with public key
    ... key/decrypt with the private key and encrypt with the private key/decrypt ... encrypt data and send it back to Alice. ... only she can decrypt Bob's data. ... see the public key and the encrypted data, but she could not decrypt Bob's ...
    (microsoft.public.dotnet.security)