Can another application access a private key stored in a key container using RSACryptoServiceProvider? - compact-framework

I am using RSACryptoServiceProvider to generate public/private key pair and using cspParameters object to store it in a key container.
My problem is that after i store the private key in a key container, can another application access the key container and retrieve the private key i generated?
If yes, the security of the key is compromised isn't it?
How do i avoid this? Should i encrypt the generated private key with a symmetric encryption algorithm?

Without using a Hardware Security Module, your only protection is to set the CspParameters.Flags field:
CspParameters.Flags = CspProviderFlags.UseNonExportableKey | CspProviderFlags.UseUserProtectedKey;
The first flag prevents software from "honestly" exporting the private key. The second requires user interaction with the GUI to perform any private key operations.

Related

Generating RSA public key from its private key counter part in Web Crypto API

I thought this would be a simple matter but it looks like webcrypto api doesn't provide a way to take a private key and generate its public key counterpart.
Is this true? Or am I missing something?
The reason I'm trying to do this is because I want to transfer the keypair and trying to reduce the total size. It would be great if I can just export the private key and later retrieve both of the pairs.
WebCrypto does not provide a mechanism to derive the public key from a private key.
It is technically possible with the RSA cryptosystem but not necessarily others (see Given a private key, is it possible to derive its public key?) you are only missing e which is generally could be brute forced/guessed (it is usually 65537).
Migrating private keys is something you do not want to do often if at all, every place a software key exists it may leave a remnant (page file, etc) that could be used to discover key later.
If you make the private key exportable (you do not want to do this for security reasons -- see above why) then you could write javascript to do this.
In short, the space savings is probably not worth the complexity and security consequences.

Asymmetric cryptography with reversed key roles

I'm trying to implement licensing system for a software to prevent piracy.
For this I need to read a license file within the application, and I need to make sure this file is written by the owner company.
Asymmetric cryptography has good potential for this aim, but in other way around!
Asymmetric cryptography algorithms, like RSA, give you a Public key and a Private key which are used as follow:
Private keys are used to decrypt.
Public keys are used to encrypt.
But I need them like this:
Private keys to be used to encrypt.
Public keys to be used to decrypt.
A simplistic idea would be to switch the role of public and private keys, but there are posts saying that in RSA public keys can be generated from private ones which make this idea impractical.
So here's my question: Is there any asymmetric cryptography with reversed key roles?
If speaking about RSA public/private key pair can be used in both cases you described. When you use private key to encrypt then public key decrypts and vice-versa.
As you said public key can be derived from private key but not the other way.
If you want to prove the origin of licensing file, use RSA in signature mode. In signature mode a hash is computed from the data you want to protect and then encrypted using private key. The result -the digital signature - is appended to the protected data.
Verification process starts by decrypting the signature which gives you the hash. Then compute hash value from the data. Computed and decrypted value shall be the same.
There are a lot of libraries providing comfortable way of signature creation and verification. You can choose depending on the platform you use for your application development.
http://en.wikipedia.org/wiki/Digital_signature

How to avoid public key spoofing?

I am doing an activation server which will enable users to activate their software. The problem is that they own the public key.. the verification method needs the public key to check signature of the license file. My concern is that someone could generate their own license file using two key (public and private) and then change the public key on the client which would spoof his current license.
Is there any way of avoiding this ? Where should the public key be stored ? You have to take into consideration that the checkup is done on client and the signature is created on server.
Thanks.
Don't use a licensing file. Each time the application is started, have it connect to a licensing server to validate it.
So? The whole point of a public key encryption that the decryption can only be done by the key's counterpart. If you app encrypts something with the user's public key and sends it to your server, they can't decrypt that data packet, because only the private key you possess will work.
If the user's license file is encrypted with your private key, only their public key can decrypt it. This way, even if the license file gets shared, you'd be able to track down who shared it because only one user's key would've been able to decrypt it.

How are public and private keys different?

I have a follow up question to Given a private key, is it possible to derive it’s public key?
Are the public and the private keys the 'same' (in the sense that you just choose to make one public) or can you do more with the private key than with the public key?
EDIT - to better state my question:
When the two keys are generated, could I just randomly choose one of them to be the public key?
Some paper descriptions present roles of public and private keys as quite symmetrical but you definitely can't swap roles of private and public key in real world.
Common usage:
the public key must be used for encryption and verifying signature
the private key must be used for decryption and signing
There is several reasons for that:
you don't want to leave a choice to the user as to which key should be published and which not. The public key is published worldwide and you can consider it as your public identity. The private part is needed when you have to prove to someone else that you have more insight than others about this identity: you can read messages sent to it, you are able to sign messages that can be verifyed by anyone who knows your public id. If what part of public/private key to publish were left to the user you'll end end with users publishing both. But that's not the main reason.
when you have private keys, you really have both keys every common implementation I know offer tools to extract public keys from private files. That's true for pgp, gpg, openssl. It means so called private key files store both private and public keys as described in algorithms. That's by design.
For exemple with openssl the sequence of commands to generate a RSA key pair can be:
openssl genrsa -out private.key 2048
openssl rsa -in private.key -pubout -out public.key
It should be clear enough that the first command generate both keys in the private key file and that the public key is merely extracted from it.
The consequence is that if your private key is ever compromized, both your keys would be compromized. The other way around is secure, you can't deduce the private key if you know the public key neither from the file nor from a mathematical attack.
encryption with private key is mathematically weak: well, the previous point is already enough, but some devious users could be considering using asymmetric cryptography
keeping both keys hidden for exchanging data. Don't, use symmetric ciphering if you want to do that kind of exchanges. Yes it is possible to crypt a message using private key and decrypt it using public one (that's basically what is used for signing, but the use case is different as you also have initial message). Internal parameters of the two keys are not the sames and all the strongness of cryptography has been prooved only for the usual direction and common usage.
It really depends on what you call "private key." In almost every practical sitation, the sender knowing the private key also knows the public key. It provides others with its public key so it needs to know it. So in essence, that "private key" will contain "public key" information or at least it can be derived from it.
Generally, you cannot swap private and public keys. In fact, they are not always of the same type (depending on the cryptosystem used). For instance, in ECDSA, your public key is a two-dimensional "point" on an elliptic curve, whereas your private key is a number.
From http://www.webopedia.com/TERM/P/public_key_cryptography.html:
A cryptographic system that uses two
keys -- a public key known to everyone
and a private or secret key known only
to the recipient of the message. When
John wants to send a secure message to
Jane, he uses Jane's public key to
encrypt the message. Jane then uses
her private key to decrypt it.
An important element to the public key
system is that the public and private
keys are related in such a way that
only the public key can be used to
encrypt messages and only the
corresponding private key can be used
to decrypt them. Moreover, it is
virtually impossible to deduce the
private key if you know the public
key.
No. That is the idea of generating a pair of keys in PPK world. You typically encrypt with the public key and decrypt with the private key. So you'd share the public key with your friends and ask them to use it when they send you their bank account number.

Can I store a PGP key ring in a csp parameters key container?

i have been using PGP. I need to safely store our PGP keyring for our application that will be processing some pgp files. Can I believe that the PGP is Asymmetric cryptography and i should be able to store the information for keyring in a key container?
PGP is a hybrid cryptographic system i.e it uses a mix of public/private asymmetric and standard symmetric encyption/decryption. It generates a unique session ID which it uses to encrypt the data it then encrypts the session ID with your public key. It then adds the asymetric encrypted session ID to the symmetric encrypted data to produce the final ciphertext
For decryption, the private key is used to decrypt the session ID and then standard symmetric decryption to retrieve the plain text.
PGP stores two sets of keyrings - public keyrings and private keyrings.
So if you shipped the private keyring with your app then anyone could use the private key to decrypt the pgp files.
Public/Private key security is not a magic bullet it still relies on the user keeping their private key or keyrings safe
Here's a good link on how PGP works in more depth
You should check and see if there is a OS-level keystore to use for that sort of thing. Rolling your own solution is error-prone. Besides, why re-invent the wheel if you don't have to?