X509Certificate2 and en/decryption
- From: b.fokke@xxxxxxxxx
- Date: 2 Jun 2006 09:12:09 -0700
Hi all!
I want to use a X509Certificate2 to encrypt and decrypt a message.
// Gets a certificate with KeyUsage.DataEncipherment
X509Certificate2 certificate = getEncryptingCertificate();
byte[] toEncrypt = Encoding.ASCII.GetBytes("Hello world!");
// Retrieve RSA CSP containing public key
RSACryptoServiceProvider rsa =
(RSACryptoServiceProvider)certificate.PublicKey.Key;
byte[] encrypted = rsa.Encrypt(toEncrypt , false);
// Retrieve RSA CSP containing private key
RSACryptoServiceProvider rsa =
(RSACryptoServiceProvider)certificate.PrivateKey;
byte[] decrypted = rsa.Encrypt(encrypted , false);
As far is I know encrypting works fine. A 128-byte block of data is
succesfully created and it looks pretty garbled to me :)
However decrypting doesn't work for me. When I try to read the
PrivateKey property of the certificate, I get a CryptoGraphicException.
The error message is dutch 'Er zijn meer gegevens beschikbaar.', which
means 'There is more data available.'
To me this seems like a pretty strange exception and I'm baffled. Does
anyone have any idea what is going wrong and what I might do to correct
this issue?
Thanks,
Bram Fokke
Utrecht
.
- Follow-Ups:
- Re: X509Certificate2 and en/decryption
- From: Pieter Philippaerts
- Re: X509Certificate2 and en/decryption
- Prev by Date: Vista Beta 2: SCardEstablishContext fails when called from a Service
- Next by Date: Re: padding and buffer size for Cryptoapi CryptEncrypt()
- Previous by thread: Vista Beta 2: SCardEstablishContext fails when called from a Service
- Next by thread: Re: X509Certificate2 and en/decryption
- Index(es):
Relevant Pages
|