Take the 2-minute tour ×
Information Security Stack Exchange is a question and answer site for Information security professionals. It's 100% free, no registration required.

I am in a situation where I would like to encrypt audio and text files for an application I am working on. I did some looking around and from what I have seen the Rijndal encryption algorithm would work well for this. I found an implementation that uses .NET's RijndaelManaged class here.

In my site, I will be accepting audio files from customers and generating text files from them. I would like to encrypt both of these files while they are on my servers, this way if a malicious user is able to access the file system they will not be able to access the contents of the files.

From what I understand, it is very easy to screw up encryption, so I was hoping to get some opinions on whether the encryption example that I linked to is an effective way of encrypting files? Are there any potential weaknesses in this example? If so, suggestions on how to make this example more secure?

share|improve this question

2 Answers 2

The encryption isn't your problem as most C# encryption examples will do the right thing with the CryptoStream class. But after quick look at the CodeProject article that you linked, I can say with certainty that you had better not use the code as presented. I will list the problems in the order they appear in the code.

  1. private void EncryptFile(string inputFile, string outputFile)
    The encryption method takes a path name. That means that the files will be on your file system in an unencrypted vulnerable form until it is encrypted by this method and deleted. It should be encrypted in server memory as it is being pulled through the connection before touching any storage device!
  2. string password = @"myKey123"; // Your Key Here
    UnicodeEncoding UE = new UnicodeEncoding();
    byte[] key = UE.GetBytes(password);
    What the hell? The password should never be used directly from the raw string! It should be hashed with something secure!
  3. CryptoStream cs = new CryptoStream(fsCrypt, RMCrypto.CreateEncryptor(key, key), CryptoStreamMode.Write);
    Why is the initialization vector the same as the key? There is a method for that: RijndaelManaged.GenerateIV();
  4. catch { MessageBox.Show("Encryption failed!", "Error"); }
    If you are running that on a server without a user interface, that's going to cause problems. But the bigger problem is that this catch block isn't specific enough to determine how to handle the problem! See Best practices for exception management in Java or C#.

That aside, the other potential weakness is how you generate your keys, where you keep them, and how access to the keys are controlled.

In your case, who keeps the keys to the ciphertext: you or the customer or both? If you have the keys too, you will need to figure out how an attacker that has broken into your server and gotten your ciphertext will not also have your keys.

Your best bet is to store the encrypted data on the server with the encrypted key. The key itself should be encrypted using the user's credentials. That way, the server can only decrypt the data and send it to the user when the user logs in and decrypts the key with the password and the server can encrypt and decrypt the data with the decrypted key until the user logs off, at which point the key should be deleted from memory. If you implemented this system and your data was stolen, the attacker would have a set of encrypted keys (which are useless until decrypted with the customers' password) and blobs of encrypted data (which are also useless without the decryption keys).

share|improve this answer
    
Great answer, thank you so much. I had a few questions regarding how to store the encrypted key on the server. When you say store the encrypted key on the server, would I be doing that in, say, the user table in my database? Would the key simply be randomly generated for each user? –  Abe Miessler May 8 at 15:54

Personally, I have better trust in MSDN articles over anything else. That being said, I would start with Cryptographic Services, then Walkthrough: Creating a Cryptographic Application, which explains the process pretty well.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.