Best way to AES Encrypt large files (Approx. 5GB) - c#

My Requirement:
I have a Azure Storage Account with 2 containers named Normal and Encrypted.
Now a zip file will be uploaded into "Normal" container which I need to encrypt and place it into "Encrypted" Container.
File can be anywhere between 3GB to 5GB.
Currently, I have used AES CBC with padding PKCS7 (I have not used HMAC). and this works fine. But based on some security concerns, we have found that AES CBC does not provide integrity.
Challenges:
As AES GCM is more secure, I am exploring on how can be the bigger files can be encrypted using AES GCM. If we encrypt in chunks, then for each Chunk different Auth Tag is generating. How can this be addressed? as I need to give Key, IV and Auth Tag for decryption team to decrypt it.
Is it good and is it possible to encrypted 5GB files using AES GCM? If so, can anyone help me out with some example code in C# or link to refer?
If AES GCM is not good for this, then how can I implement HMAC to my AES CBC code. As I am working on Azure Blobs, I am using CryptoStream to encrypt and write the content into "Encrypted" container.

AES-GCM will provide the inbuild authentication (integrity) mechanism. But through this mode, we can only encrypted limited content (say up to 1GB).
AES-CBC will support streaming through CryptoStream class and can encrypt the larger files in chunks without any issue. Only Issue with AES-CBC is it doesn't provide integrity. For this we can add some extra logic to implement HMAC though which we can achieve the integrity.
HMAC - Create SHA256 hash of your encrypted content and pass it to Decryption team. Decryption team needs to validate this hash. Only if Hash is matched, decryption team can proceed for decrypting the encrypted content. Through this, decryption team can ensure that encrypted content is not tampered.
One of the highlight point mentioned here is:
The authentication part of GCM (GHASH) is weaker than HMAC, GHASH provides a maximum 128-bit authentication tag, whereas HMAC allows lot longer tags (HMAC-SHA-256 would allow 256-bit authentication tag). In addition, forgery of GHASH tags in some cases is easier than HMAC
Hence, I went with option of AES-CBC with HMAC encryption.

Related

encryption and decryption using certificates in C#

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.

Properly generating RSA+AES keys

For learning purposes, I'm creating a chat application where the connections are done via SSL/TLS, and the messages are encrypted using AES-CBC-256 and the AES keys are encrypted with RSA-2048.
The AES key is randomly generated (AesProvider.GenerateKey()) per user per session (which means one key for every person an user is chatting with) and the IV is randomly generated (AesProvider.GenerateIV()) by passing in the key generated, each time a message is created (before being sent).
On the RSA side, I'm generating a secure random session name to store the private keys generated in containers, and sending out the public key. I'm also using the same model (one key pair per user per session) as in AES.
I should also state that I'm using HMAC-SHA512 to hash the messages and sending the HMAC key encrypted using the same public key that the AES key/Iv gets encrypted with. Since I've read that it doesn't need to be regenerated often, I'm planning on regenerate the HMAC key every 5000 or 10000 calls.
Questions:
1) Should I be creating only one RSA key pair per user and use it for all sessions, or is it good how it is right now?
2) Is using the same AES key and only changing the IV like explained above considered secure?
Not much to answer because what you are doing is the best practice aready.
Some notes though;
RSA key pairs per session is not required (and generating the key is expensve).
You can have only one 2048 bit strong RSA key throughout the lifecycle of your application or for years, since this is what even the most security demanding web applications like e-commerce sites or financial applications do.
You should have a random AES key/IV pair for each session, that is fine.
It is better to have one HMAC key per session (not process wide) since you are sending the key securely (RSA encrypted) and you are also sending the HMAC value securely (AES encrypted) on the wire.
Changing only the IV is almost equal to changing the key and IV (in a sense) because the encrypted output will be different for the same content if you change the IV.
One note however. To prevent a man-in-the-middle attack mimicking your server certificate, is your client code validating the certificate through means of signature checking, or is it just the public key that you are sending without any validation on the client side?
You should have either a self-signed persistent certificate or generate the random certificate (RSA Key Pair) as is issued by the persistent certificate (eg, CN=FrozenDeathChatServer) where the clients during installation of your client software install under the trusted root certificate authorities.

Encrypting XML and providing readable password to app users

I'm very new to the topic of encryption.
I'm building a WPF application (to manage database permissions) with a config file that contains one connection string that needs to be encrypted. I'm using 256 Rijndael encryption and generate a symmetric Key and IV.
Now that I have the Key and IV I can generate the proper Rijndael key to decrypt the relevant XML node. What I need now is a readable password that each authorized user receives from me in order for the app to successfully talk to the db. It's ok if the user has to type the password in every time the app is launched.
Am I approaching this correctly? Is there an easier way to do what I'm trying to do?
Would really appreciate the help.
thanks!

RsaProtectedConfigurationProvider implementation vs RSACryptoServiceProvider c#

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

What type of encryption should i use

I need to find out what type of encryption is suitable for me.
For my case i usually need to pass information from my mobile webapp to a .NET webservice via ajax.
To summarize:
(Encrypt using javascript)Mobile ---Encrypted Data--> WebService(decrypt in C#)
Just use SSL - than all your transmission will be secure and the changes in your code will be minimal (just putting https in front of your urls should be enough).
Using https is not that enough because it uses mostly RSA encryption, which is crackable using prime number generator algorithm.
I suggest to use concatenated encryption process, use openssl RSA 4096 to encrypt aes key and use aes to encrypt your message. Beaware this is crackable also by eavedropping your communication and extracting your aes key.
I would suggest the following:
encrypt your aes key with any private algorithm you can write .. then encrypt the output with RSA .. on the receive end, decrypt RSA to get the encrypted aes key, use your algorithm to decrypt it, then use aes key to decrypt the data.
i suggest to keep on changing the aes key based on a some random value ..

Categories

Resources