X509Certificate2 and en/decryption



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

.



Relevant Pages

  • Re: Cant decrypt w/admin acct
    ... >>First off you need to rule out a permissions problem. ... >>Run mmc and select the certificate snapin for user and go ... >>used to decrypt the files. ... The certificate is a "key pair" in that the certificate is used to encrypt the files ...
    (microsoft.public.win2000.security)
  • RE: Plz help me
    ... When you want to encrypt a message you have to encrypt using the ... your public key (from YOUR certificate) to verify your signature. ... but when i m trying to decrypt and verify that Message using receiver's ...
    (microsoft.public.platformsdk.security)
  • Re: Encryption Problem
    ... the private key associated to that public key to decrypt that message. ... private key associated to the public key you are using to encrypt on the ... > certificate present in the certificate store. ...
    (microsoft.public.dotnet.framework.webservices.enhancements)
  • NTFS Encryption
    ... I recovered files from a formatted NTFS ... stories is that if you are going to use EFS to encrypt ... >>the certificate manager (run mmc.exe and open ... >>have this you can't decrypt. ...
    (microsoft.public.windowsxp.security_admin)
  • Re: efs and "encryption" overall... help?
    ... To be absolutely sure that an attacker can not access EFS encrypted files ... stronger encryption to encrypt EFS files, not that it would be easy to crack ... Pro that more then one user may be able to decrypt the file if the original ... > first encryption a certificate is created that is used to decrypt those ...
    (microsoft.public.windows.server.networking)

Quantcast