I had a look on
configSection.SectionInformation.ProtectSection("DataProtectionConfigurationProvider");
but I had two questions:
Is there a way to encrypt config file with a key, because any one can decrypt my file if he knows this method, right?
Does the decryption done automatically when retrieving the connection string at run-time? (in any class, or in data-sets)?
thanks
Yes, Encrypting config file is common. DPAPI way uses symmetric key and therefore, if someone gets the key, he can decrypt.
In my case I used an RSA asymmetric key. That way encryption with public key is performed. Decryption can be performed only with private key protected and held by server in SQL database. On application side, users and application don't need the decrypted secret but need to stay encrypted. Simply, public key is to encrypt and private key is to decrypt. And nobody can easily get the private key.
Furthermore, when it comes to cloud platform like Microsoft Azure, we have to use certificate way which is of also RSA.
My case took almost 1 month to understand the complexity. I completed this task just recently.
The decryption is performed automatically by indicating the thumbprint of private key in the config file.
Related
I have a big XML that needs to be encrypted on one server (by one c# application), and needs to be decrypted on another server (by another c# application). The XML contains the critical information for which this encryption needs to be done.
My real need is that my server that does the encryption should only know the public key and the private key is known only to the server which is doing the decryption. Also, this private key should be stored in a safe area on the server such as certificates.
What should be the steps that can help me achieve so?
Can I generate my private key and store that in certificates or the certificates will generate the private key on their own?
Also, if certificates generate the public and private key, can the public key be separated from the certificate and exported to the server where the encryption has to be done?
How the key-pair and certificate are generated depends on the software used.
The private key can be obtained from a certificate.
The general method is to encrypt data with a symmetric encryption algorithm such as AES and encrypting the symmetric key with asymmetric encryption algorithm such as RSA or EC and the public key. This is done for two reasons: 1. Asymmetric encryption is very slow compared to symmetric encryption. 2. The data length for Asymmetric encryption is limited by the key size: a typical key size is 2048-bits and that limits the data length to 245-bytes.
There is in general no way to securely store a private key (or anything) on the server other than the server being secure short of the server having an HSM or access to a TPM. The main step on making the server secure is 2-factor authentication. But if it is on a shared computer beware of root escalation exploits by other users of the server.
HSM - Hardware Encryption Module
TPM - Trusted Platform Module
Asymmetric encryption is very processor intensive and, consequently very slow. For that reason, it is not normally used to encrypt large amounts of data.
What is normally done is symmetric encryption is used to encrypt the bulk of the data and asymmetric encryption (public key) is used to encrypt the keys used in the symmetric encryption.
The encrypted keys are transmitted with the encrypted data. The keys are decrypted (private key), then using the symmetric keys the bulk data is decrypted.
This is how I have achieved it.
On my server 2, I am generating a certificate(.cer) and a private key(.pvk) using the 'makecert.exe' command. Then, using the 'pvk2pfx.exe', I am generating a .pfx file. This is the file which will now house the certificate and private key.
Now I export the certificate with just the public key to the server 1, where I encrypt the data, and on Server 2 using the certificate's private key I decrypt the data.
I have a very specific case for cryptography and I am just not sure what algorithms I need to use to achieve a result I am looking for.
So, it is as follows.
I will distribute an encrypted string to my clients, they will have a password to decrypt it. But I don't want them being able to create such encrypted strings themselves.
So, I need some particular algorithm that would allow me, and only me to encrypt something, and anyone can decrypt it if they have a password. but NOT encrypt.
Can you use certificate based encryption? This is exactly the way HTTPS/SSL encryption works.
The server has a certificate with public and private keys. The private key is used to encrypt data. The certificate with just the public key is distributed to the clients and the public key is used to decrypt data.
I want to allow users on my site to create an asymmetric private & public key so they can:
upload the public key to the site so my web application can encrypt
files they upload
download files and decrypt them using a local
application and the private key
My issue is that if the users machine dies then they will most likely loose their private key.
My concept is that the application the users download to pull the files from the site will also allow them to generate the keys for use on the site.
So my question is, Is it possible to create an asymmetric private/public key pair based on a passphrase? This would allow the user to regenerate the private key if required.
One way to do exactly what you are asking is:
Define a security level N, the larger the more secure but slower this process will be.
Generate a "salt" and associate it with the user's id.
Since RSA key generation requires a secure random number generator, use the user's password and salt with PBKDF2, starting at iteration N, to generate secure random data.
This process should deterministically generate a public/private RSA key pair. However, the reasons not to do this are:
It was cooked up by me and, AFAIK, this post is the first time this process will be publicly vetted.
It is not known to me if PBKDF2 actually works as a secure random number generator for use with RSA.
It may or may not be true the PBKDF2 is guaranteed to generate a data from which a public/private RSA key pair will originate.
In practice, while this does work, it takes a very long time and the time it takes is based on the user's password, which is a user experience and security exposure point that needs considered.
A better way to accomplish what you are trying to do is:
Define a security level N, the larger the more secure but slower this process will be.
Generate a "salt" and associate it with the user's id.
Generate an RSA public / private key pair.
Iterate PBKDF2 N times to create a symmetric key based on the user's password and salt.
Use a symmetric encryption algorithm to encrypt the private key.
Upload the unencrypted public key and encrypted private key to the server.
This is better because:
All processes listed above are, AFAIK, standard and vetted.
Generation of public / private keys (time consuming) only occurs once while setting up the user's account.
Accessing the keys always occurs in a fixed amount of time.
This solves your problem:
Since the server only has the encrypted private key it cannot decrypt the user's data.
If the client machine dies the server can re-issue the private key.
Of course there are the obvious warnings like if the user forgets their password all their data is locked until public knowledge on how to crack RSA is available or a billion years of current computing power is put to the task of breaking their password ;-) (depending on N and key size of course). Also, the salt is important to prevent dictionary attacks.
What the fields are for your algorithm (e.g. RSA http://msdn.microsoft.com/en-us/library/system.security.cryptography.rsaparameters.aspx) are more or less up to you. You could easily create an algorithm to generate D and P from some string (calculating the other RSA fields from those). I'd really recommend you wouldn't pick one from an answer on StackOverflow though (that would simply give someone who wanted to get at your data something easy to try).
The reason algorithms like RSA are hard to break is the amount of effort that goes into reverse engineering the private/public key based on the encrypted data. If you introduce a "shortcut" like a reproducible generation algorithm based on a passphrase, the security of your system now depends on how well the passphrase is kept secret, not the encryption algorithm itself.
The same is true for the private key; which is why many organizations don't store the private key on hard drives and store them on secure external devices protected by a password or some biometric information.
I'd recommend you do not generate keys from a passphrase and simply use recommended practices for storing and protecting your private key.
If RSACryptoServiceProvider cannot Encrypt data larger than it's KeySize, how RsaProtectedConfigurationProvider is implemented in the .Net framework?
I am working on a utility that is going to be used to encrypt/decrypt some sensitive information. My two encryption provider options are DPAPI and RSA, while DPAPI not suited for web farm kind of environment, RSA is fits because of the Export/Import options with a KeyContainer. This is a stand alone application running on a workstation.
As I am aware that Asymmetric algorithms are not designed for large data, I just tried encrypting a string of length over 400K using the code below and it works well.
if (!section.SectionInformation.IsProtected)
{
section.SectionInformation.ProtectSection("RSAProtectedConfigurationProvider");
section.SectionInformation.ForceSave = true;
config.Save(ConfigurationSaveMode.Full);
}
Definitely this implies that more things are happening behind the scenes apart from the export import key options in aspnet_regiis.exe.
My understanding:
we encrypt myapp.exe.config with RsaProtectedConfigurationProvider, provide a key container name myrsakeycontainer, and export the public and private keys to an xml file myrsakeyfile.xml.
If we want myapp.exe.config to be decrypted in another computer, we import they keypair from myrsakeyfile.xml with a container named myrsakeycontainer.
this works well. I can achieve the same thing in my project via RSACryptoServiceProvider. But I can't handle data that larger than the key size that
new RSACryptoServiceProvider(cspParameters)
generated for me.
I want to be able to decrypt huge data (just in case) just the way
RsaProtectedConfigurationProvider does.
Yes I could use a RijndaelManaged (my favorite) for actual
encryption and for the symmetric key transport (export/import) I
could use the RSACryptoServiceProvider. This leaves me in a
situation that If I want to export/import the symmetric key, I should
first encrypt it with the public key or RSA, import it to another
machine, decrypt with the private key of RSA. Which is export the RSA
key pair along with the encrypted symmetric key.
But, when I export RSA key pair used by
RsaProtectedConfigurationProvider via aspnet_regiis.exe, I
believe that it exports only the public/private key pair in an xml
file and no other information (like the symmetric key information).
So, with just the RSA key pair, how does
RsaProtectedConfigurationProvider manage to derypt (huge - over
400K chars in my case) information that was encrypted on another
computer? In cases it uses a symmetric algorithm (perhaps?!) to
encrypt information, how is that symmetric key exported/imported to another
computer for decryption? Is that symmetric key part of the RSA key container exported via aspnet_regiis.exe or is the symmetric key is contrived dynamic based on an algorithm?
I could get away with a Rijndael, whose key is encrypeted with an RSA
key pair and I can export/import both the RSA key pair and the
Rijndael symmetric key to another computer. (which I have done in the past)
I am interested to know what is used inside
RsaProtectedConfigurationProvider.
Any theories? concepts? links? recommendations? please..
Similar Question - What algorithms are used by RSAProtectedConfigurationProvider in web.config encyrption?
The encrypted symmetric key is stored in the XML alongside the encrypted configuration information that the symmetric key has encrypted.
If you use Reflector to look at the code, what it does is load the XML node and use the asymmetric RSA private key to decrypt a symmetric key stored within the XML node itself.
The function that actually does this magic is here:
public virtual SymmetricAlgorithm GetDecryptionKey(EncryptedData encryptedData, string symmetricAlgorithmUri);
Declaring Type: System.Security.Cryptography.Xml.EncryptedXml
Assembly: System.Security, Version=2.0.0.0
See the code around
this.m_document.SelectNodes("//enc:EncryptedKey", nsmgr);
This blog post has a nice writeup about how you pair Asymmetric and Symmetric algorithms in real-world practice: http://pages.infinit.net/ctech/20031101-0151.html
Is there a way that my server can provide an encrypted string that can be decrypted on the client, but NOT re-encrypted on the client? I know this seems kind of backwards... here's what my need is.
I have a software key that needs to be activated against our remote server. The server needs to provide something back to the client that says "You are active" and contain info such as a date that it's valid until, how many licenses, etc. However, I need to prevent it from being easily tampered with to increase license count or the dates (i.e, re-encrypt the value with a new date using a key found in the de-compiled binary or w/e).
Is such a thing possible using public/private keys? Or perhaps hashes?
EDIT
Alternatively, can the server provide a hash that the client can validate is really from the server without giving the client the ability to spoof or generate a hash on it's own?
Thanks in advance.
Public/private key encryption should do what you need. Hashes are one way functions; a good hash function will make it impossible to retrieve the original value.
In this case, the server has a public/private key pair and the client has a public/private key pair. The server's public key is embedded into the client, and the server has the client's public key as well. The server can now encrypt your payload using it's private key and the client's public key. When the client wants to decrypt the payload, it uses it's private key and the server's public key. The client cannot re-encrypt the data without access to the server's private key.
http://en.wikipedia.org/wiki/Public-key_cryptography - for an explanation of how it all works
http://msdn.microsoft.com/en-us/library/e970bs09.aspx - as a starting point for .Net classes to make it easier
Sure. Use an asymmetric-key algorithm like RSA. Both keys are required to go from cleartext to cleartext; one will encrypt, the other will decrypt. You cannot use the same key you encrypted with to decrypt, and vice-versa. So, the client could not get ciphertext, decrypt it, then use any of the information it has to come up with the same ciphertext.
HOWEVER, asymmetric-key algorithms do not differentiate between the encryption and decryption keys until one is used to encrypt. They only require that the other key is used to decrypt a message encrypted by the first. So, theoretically, your client could "re-encrypt" a message using its "decryption" key that would be decrypt-able by the server using its "encryption key". I don't know of an algorithm that would disallow this; you'd simply have to build it into your communication library by omitting any way to use the decryption key for anything but decrypting.