Getting private key from ECC certificate - c#

How can I get the private key from a certificate I imported with a .p12 file (contains private key too)? I am using .net
I have tried loading the certificate from the store and use .PrivateKey that is only for RSA and DSA because I get an error.
My certificate is ECC which I cannot find much information about. I tried using
certificate.GetECDsaPrivateKey()
but it returns null? What can I do to get the private key from the cert store? I need this private key to form a shared secret with a public key I have. Any help is appreciated. Thanks
edit: i converted the p12 into a .pem and can see that there is a private key in there

Related

How to validate a signature using just the public key of the signing authority? C#

I have an encrypted XML which is signed using the private key of a signing certificate. I have decrypted the XML on another machine, revealing the public key of the same signing cert within it. Before I process the data that I have unwrapped, I need to validate the signature on the encrypted XML. How do I do this using just the public key of the signing cert that was used to sign it in the first place?
I am working with C# for this application.

How to encrypt/decrypt XMl wiith X.509 certificate correctly?

I want to encrypt a XML file by using a X.509 certificate and also decrypt it, too. As far as I know I need to use the public key (inside the certificate) to encrypt the XML and the private key to decrypt the XML. Thus only the guy with private key is able to read the decrypted data.
Microsoft provides some code for encryption/decryption here:
Encryption: https://msdn.microsoft.com/en-us/library/ms229744(v=vs.110).aspx
Decryption: https://msdn.microsoft.com/en-us/library/ms229943(v=vs.110).aspx
As you can see from the first example a X.509 certificate will be loaded to encrypt the file. But the second example does not(!) load a certificate to decrypt the example. It seems that the encrypted file holds all the necessary data to be decrypted? Does this mean that the file can be decrypted by anybody? I think I have a lag of understanding here - why is it not necessary to use a certificate to decrypt the data?
Regards,
Michael
On decryption, the certificate is loaded from the computers certificate store. From your second link:
The code example in this procedure decrypts an XML element using an X.509 certificate from the local certificate store of the current user account.
In that example, the public key used to encrypt the XML is stored in the encrypted data and is used to look up the proper certificate from the store.
So the answer to your question -- "why is not necessary to use a certificate to decrypt the data" -- is: it is necessary. The certificate was loaded automatically.

Using a X509 certificate for decryption

I have some data of an X509v3 certificate that is used at a central licensing station. My question is is the following amount of information enough for me to decrypt data using C# code? And additionally, how are the certificate properties imported into a project? Do I have to create a certificate file in order to go on?
Known to me are:
Subject
Serial Number
Issuer
"root-ca"
Public Key Algorithm: "rsaEncryption"
RSA Modulus, 128 bytes
RSA Public Key Exponent
X509v3 Extended Key Usage: "critical"
Signature Algorithm: "md5WithRSAEncryption", followed by 256 untitled bytes
SHA1 Fingerprint
I do not have any certificate file. Excuse me if a similar question has already been answered, unfortunately I wasn't able to find one like mine.
No, your data is not enough. First of all, this is all public data. It doesn't contain a private key. A private key is used for decryption or signature generation. A public key is used for encryption and signature verification.
The .NET API is peculiar in that you can seemingly use a certificate to decrypt. This is not really the case; the certificate and private key pair are seen as one; only if the private key is included then you can actually decrypt. Personally I see this as a minor design mistake.
In principle you could create a certificate given the information. Basically you would have to generate a certificate with the same information and then replace the issuer and signature fields.
This is however not for the weak of heart; I recommend a few years of experience before you even try. If any information is missing from the list above you won't get a valid certificate / signature, and you won't get any warning what is wrong, just a failure. You've got one advantage though; if the signature verifies or fingerprint is identical to the one you've got then you know that you've succeeded.
You would not be able to decrypt of course; the private key would still be missing.
Note that the signature is the 256 untitled bytes.
This information is not enough. This data is a public key to encrypt data.
RSAParameters

Write P7M file Cades compliant

I'm working with my smartcard and x509 cert through Net.Pkcs11.dll component.
I'm able to sign a file, but I don't know how can I create a P7M structure.
I've already seen many examples (BouncyCastle and so on) but all of theme use digital certificate with private key, while my private key is protected by smartcard.
Can you help me?

How does Bouncy Castle API know which key to encrypt with?

I'm curious about the Bouncy Castle API process for handling multiple public keys to encrypt data. For example, if i have 3 different clients that would like me to encrypt data and send to them using their public key for encryption, if i label each clients public key respectively - how does bouncy castle determine that client 1 should be encrypted with public key 1 and not public key 3 (which would be the public key for client 3)?
it would seem from a decrpytion standpoint, that publicKeyEncryptedData has a keyID tag attached with it that can be used to look up the corresponding private key, but i dont understand how it chooses the correct key to encrypt with.
It doesn't. You have to specify all recipients (i.e. certificates to use for encryption). When you are doing encryption using PKCS#7 the process is:
Generate random symmetric key (i.e. AES256)
encrypt data with symmetric key
encrypt symmetric key with public key of the recipient (if X recipients should be able to decrypt then encrypt the symmetric key X-times)
put it all together in PKCS#7 (encrypted symmetric key is put in a structure with some identification of the recipient. Usually it is serial number and issuer DN of the certificate which was used for encryption of symmetric key)
Decryption process is:
find recipient able to decrypt the message. PKCS#7 contains serial numbers and issuer DNs of all recipients who should be able to decrypt. Now look in crypto store for a certificate with serial number and issuer DN that has a corresponding private key. It does not matter which private key will be used if you have all recipients private keys in crypto store.
use private key to decrypt symmetric key used in the encryption process
use symmetric key to decrypt data

Categories

Resources