Releasing: Comatose WordPress Theme

This has been in the pipe a long time, it started simply as a set of experiments playing with HTML 5, layouts, responsive design, page structure, typeography, displaying images on mobile screens, the mobile web, minimalism, and my own dislike with the previous design(s) of my site. Finally, I decided to turn it into a full fledged WordPress theme. This is the result. It powers my site, and the InterKnowlogy development blogs where I work.

So here is the low down:

  • HTML 5 layout with CSS 3 styling
  • Responsive Layout
  • Clean, minimalist design
  • Print style
  • Clean, readable, and consistent text styling.
  • Clean and readable typography.
  • Clean display of code.
  • Beautiful images
  • Images display at native resolution on Regular and High Density screens
  • Widget enabled sidebar support
  • Nested comment support
  • Gravatar image integration for comments (High resolution images for High Density screens)
  • Custom menu support
  • Nested sub-sub-sub menu support
  • Graceful degradation in older browsers

But don’t take my word for it. Download links, versions and release notes are all on the project page:

And all of the source code is hosted on GitHub.

Desktop Screenshots

Comatose Windows with Chrome showing Sidebar

iPad Retina Screenshots


Comatose iPad Retina Menu

See more screenshots

Providing integrity for Encrypted data with HMACs in .NET

Note: This post is a continuation of my previous article on AES Crypto in .NET

Once again I’m about to dip into and scratch the surface of Cryptography. So here’s the disclamer: This is not my job. I don’t do this for a living. Don’t ever make up your own encryption algorithms. Try not to write your own cryptographic code. Don’t take anything that I say as the advice of a security expert or legal advice or assume that any code in this post or any linked posts are correct in any way shape or form. If you have a project that requires cryptographic security, I suggest you find someone who has been doing this far longer than I have to write the code for you. Then have several 3rd party security firms review or write your security code. In short, I’m not responsible for your mistakes, or the correctness of the code or writing presented here.

As even more of a taste of just how hard it is, I admit that I thought the last article I wrote covered most of the basics of modern encryption in .NET pretty well. The result? Nope. Missed something big. See, cryptography is hard not because it’s hard to say “take a password plus this data and make it so someone can’t see the data without the password”, but because of all the ways someone could get access to your password, or access your data when its not encrypted, or access your computer when it’s unlocked, or crack your password if you pick a bad password to begin with. Take a look at this reddit thread article that nicely rips apart my previous article. In all seriousness, I really appreciate the feedback, especially when it comes to security because it’s hard, and the devil is in the details.

I forgot to cover data integrity in my last article. Encryption is not enough. Redditors referred to it as authentication, which is one of the uses of HMAC’s, but authenticity is not whats important in the example case I presented for my original article. We are interested in “How to detect that someone or something accidentally or maliciously changed the encrypted data”, essentially, we’re interested in cryptographic data integrity. In addition to that there were a few other things wrong with the article that the redditors pointed out, such as my salt values being overly large, just picking CBC without covering other chaining methods, not discussing how to remove keys from memory, and referencing Jeff Atwood. The long and the short of it, they’re completely right. Getting crypto correct is hard, and good crypto systems are worth millions of dollars. You’re probably not getting paid that much to write a little encryption. So use a library thats already been written and provides high level abstractions and don’t write crypto code yourself.

Alright. HMAC, what is it? The MAC stands for a Message Authentication Code, the H stands for Hash. Put it all together and you have a Hash-based Message Authentication Code. It’s a hashing function thats deliberately designed to resist malicious tampering. The key is preventing malicious tampering. A normal hash function (e.g. MD5 or SHA1) would detect accidental byte tweaks, but somebody maliciously tampering with your data could tinker with it, create a new hash code for the data, replace the old hash code, and you would never be the wiser. For this reason, Message Authentication Codes generally fall into the category of keyed hash algorithms since they use a key or derived key and mix it with a hashing or encryption function to produce a value that an attacker can’t reproduce, providing both data integrity, and (if you happen to be in a sender and receiver role where both parties share a key) authentication. There are however, a couple of things to take into account. First, we shouldn’t use the same key twice to encrypt and hash the data. The more times the same key is used, especially against a known piece of data, the more likely an attack can be developed and used to figure out our key. Arguably, the final key used for encryption and the final key used for the message authentication should be different, as different as possible. The best way to do this would be to append or change the key such that both the encryption key and authentication key run through the KDF (Key Derivation Function) from different starting points. As an example, consider the following:

  1. // Derive the passkey from a hash of the passwordBytes plus salt with the number of hashing rounds.
  2. var deriveKey = new Rfc2898DeriveBytes(password, passwordSalt, 10000);
  3. var deriveHMAC = new Rfc2898DeriveBytes(password, hmacSalt, 10000);
  1. // This gives us a derived byte key from our passwordBytes.
  2. var aes256Key = deriveKey.GetBytes(32);
  3. var hmacKey = deriveHMAC.GetBytes(32);

Because the hash function mixes the password with the salt, and because we have different salts, after only one round the derived keys will already be different on account of the salt value. So we have a derived key. One for the actual encryption, one to prevent tampering and provide data integrity.

On a side note, it’s arguably better to authenticate the encrypted output (encrypt then authenticate) rather than authenticate the plaintext, then encrypt, or authenticate and encrypt. Again, I know it’s beating a dead horse, but cryptography is hard, and ultimately, the security of a system is going to depend on the security of the entire system, not just the individual parts. So. We have an HMAC key, we have an encryption key, and we know that we want to encrypt, then authenticate the encrypted output. In addition, we want to make sure anything else that could easily be tampered with is also authenticated, such as our Initialization Vector, since any change to it can easily affect our decrypted output. Another small advantage thats almost not worth mentioning is that by authenticating the encrypted output instead of the plain text is that we can detect if anything has changed even before we start decrypting the text. So here’s an example. In this case, I chose to use HMACSHA1, there’s others on the MSDN but I chose this particular one since it uses the same hash algorithm used internally by the KDF I used in my previous post, Rfc2898DeriveBytes, aka (PKDF2).

  1. var hmac = new HMACSHA1(hmacKey);
  2. var ivPlusEncryptedText = iv.Concat(cipherTextBytes).ToArray();
  3. var hmacHash = hmac.ComputeHash(ivPlusEncryptedText);

In this case, we’re using our derived hmacKey, and we’re computing the hash of both the initialization vector concated with our encrypted ciphertext. That gives us everything we need to have a self validating “package” of data that is secured and can’t be tampered without us knowing unless the attacker knows our key or can break AES256 encryption, but at that point this whole discussion is pointless.

With decryption, remember how I said we compute the Encryption and HMAC key separately? If we did that, and if we computed the hmac over the encrypted data, we can perform the validation step on the data before we compute our decryption key. The only reason we would do this is so that if the data is invalid or has been tampered with we don’t take the time to also compute the decryption key. Small things, but I wanted to explain why the key computation for the encryption and hmac is kept separate:

  1. var deriveHmac = new Rfc2898DeriveBytes(password, hmacSalt, 10000);
  2. var hmacKey= deriveHmac.GetBytes(32);
  3. var hmacsha1 = new HMACSHA1(hmacKey);
  4. var ivPlusEncryptedText = ivBytes.Concat(encryptedBytes).ToArray();
  5. var hash = hmacsha1.ComputeHash(ivPlusEncryptedText);
  7. if (!BytesAreEqual(hash, hmac))
  8. throw new CryptographicException( "Your encrypted data was tampered with!" );
  10. var deriveKey = new Rfc2898DeriveBytes(password, passwordSalt, 10000);
  11. var aes256Key = deriveKey.GetBytes(32);
  13. using (var transform = new AesManaged())
  14. {
  15. using (var ms = new MemoryStream(encryptedBytes))
  16. {
  17. using (var cryptoStream = new CryptoStream(ms, transform.CreateDecryptor(aes256Key, ivBytes), CryptoStreamMode.Read))
  18. {
  19. var decryptedBytes = new byte[encryptedBytes.Length];
  20. var length = cryptoStream.Read(decryptedBytes, 0, decryptedBytes.Length);
  22. var decryptedData = decryptedBytes.Take(length).ToArray();
  23. }
  24. }
  25. }

So, there you go. Basic explanation about why HMAC’s are important, what I missed, some code, and the disclaimer to write security code at your own risk. Full demo code, demo code output, and a bunch of random links after the break.

See the code, output, and related links

CSS Sanity Tip for Programmers

I’m a programmer. I’ll admit it. I’m a professional nerd who, for some reason, thinks the web is awesome. I’ll be honest. I hate how difficult layout is in HTML sometimes. Seriously. How many times have YOU tried to get your margins to play nice with floated, bordered elements, and had to change the border width by 3 pixels in a floated column layout? You probably just about shot yourself. Why? Because the default box model when laying things out since forever and a half ago (e.g. 3 internet years) is box-sizing: content-box;.

What does it mean? content-box means that the box layout is measured from the outer edge of the content. So if I set width: 50px the box that represents that html element is going to be 50px wide. What sucks, and is really weird about this at times, is when we add a margin to our box like this margin: 5px. All of a sudden the ‘actual size’ of the box now increased by 10px! (5px on each side). In addition, margins aren’t “distance away from the edge of another box” like you’d expect, it’s “distance away from the edge of another content box”. That’s why when you have paragraphs next to each other and one has 5px margin and the next one has 10px margin, the paragraphs are 10px apart, not 15px like you might expect.

How do I fix it?

  1. * {
  2. -moz-box-sizing: border-box;
  3. -webkit-box-sizing: border-box;
  4. box-sizing: border-box;
  5. }

Uh. Ok. What does that do? THAT will change every element to measure everything from the outside of your box. So now, when you have two paragraphs, and you give one 5px margin and the other 10px margin, they are going to be 15px apart. If your a developer / programmer who works with programmatic layouts on a day to day basis and gets frustrated by HTML and CSS, try this out. It’ll probably save you LOADS of headaches.

While your at it, read read this post by Paul Irish, because Paul Irish is awesome, and because it covers this in more depth.

Folding The Web

I’ve got a bone to pick, and maybe it’s not even worth picking but here it is: Most of the responsive grids on the web today suck. Responsive grids are hard to do right. A responsive site has to be well planned out, is difficult to implement, and are usually prone to an overabundance of excess styling to get it to work right and a lack of re-usability. That’s where a framework comes in, and although most of them help a little in some form or another, they don’t meet the overly high expectations I have for them. Of the better ones I’ve seen over the last several months, the best one I’ve looked at so far is the one in the twitter bootstrap framework: – (See the scaffolding section for more about the Grid)

As much as these frameworks are getting closer to what I want, most of them still suffer from one big problem: They only have two modes. If you look closely as you size down your browser on the bootstrap site, the sites snaps, then snaps again, and finally the content re-arranges down to a single column. What I really dislike about this, is that there are no intermediate steps between desktop and mobile. Yes, the size is different, and it helps a LOT, but the layout doesn’t change.

One of the more obvious, and disturbingly annoying, examples of this two mode thinking is Zurb Foundation, because not only do they lack the intermediary step sizing, but the way it’s done causes some pieces of content to MASSIVLY increase in size in mobile layout. You’ll probably notice it most when it causes images to get resized into pixelated, oversampled, Web 1.0 stock image nastiness that makes graphic designers cry. Seriously. In mobile layout, your content should never be larger than it is in a wide, full width desktop environment:


So. Do I have a solution? Not completely. But I do have a demo of something cool I’ve been working on as I’ve been refining my ideas on how a responsive grid I would want to work with would behave. Take a look at it here:

It includes nesting grids and has a fully fluid layout. I’m still working on how I can make the grid more robust and get it to work in situations where the container itself is centered or fixed. I also want to include and make more complicated layout re-arrangement behaviors (like inlining a sidebar below your main content, or expanding a menu, and so on like you might have seen with the theme on my site) easier to do.

(Obligatory Screenshot)

Finally, I can’t rightly write a post like this without pointing out some great sources of inspiration: First, the Golden Grid System by Joni Korpi, which is a great example of a responsive site, even though I always feel like it’s overdone on a big monitor. Another really good article that I feel really marked the start of people thinking about a responsive web is an article on A List Apart on responsive web design by Ethan Marcotte. Finally, this post by Paul Irish on why you should only build and serve one layout of your site, and why you should be designing towards the modern web.

Happy Folding. Look for more posts on this in the nearish future.

More than you ever wanted to know about AES Crypto in .NET

First, before I begin anything, I want to point out that cryptography is hard. REALLY hard. There are so many possible points of failure, and those points of failure and methods of attack can change depending on what the purpose of encrypting the data is. If you do it wrong, you get lawsuits, and/or you end up on the front pages of major news organizations for data security breaches. Or a rival nation gets your top-secret plans to rule the world. You get the idea. I’ll touch on some of the technical points of failure briefly; I’m mainly going to be exploring this topic through a fairly simple scenario: A password vault file, encrypted with a password, and stored locally on my computer (potentially synced to other computers through some syncing service).

As with any good crypto, the first line of defense is always preventing an attacker from gaining access to the encrypted data in the first place. If data is going over a network, encryption should be part of that as well, and that brings in a whole new host of issues that are outside of the scope of this article. Since I’m planning on storing the data locally on my hard drive, the first line of defense is me and my computer: Lock my computer when I walk away. Make sure there’s no spyware or malware on my computer, no keyloggers or other devices that could steal my password, etc… Going back to my original point about crypto being hard, it’s hard not only because writing the code is hard, it’s difficult because of all the ancillary ways someone could potentially get access to my data that I have to account for.

If I leave my computer unlocked, someone could steal data off of it. Install a keylogger. If I leave this password app open someone could just go in and look at my passwords after I’ve decrypted them. If they have physical access someone could install a HARDWARE keylogger between my keyboard and computer. Pull out my hard drive and clone it. Hack into the company Wi-Fi network and remote access my computer. Or even STEAL my computer. Or TSA could confiscate it. And/or force / legally compel me to reveal my password. You get the idea. There is no such thing as absolute security or secrecy.

However, let’s assume for a moment that an attacker DOES somehow manage to get access to my vault file. (Maybe by getting access to my computer when the vault program is closed and the data encrypted, or a folder syncing program has a hiccup and spams a public link to it on the Internet) My password file is in the hands of someone actively trying to get at my data, and the only thing protecting the data is my password and good cryptography. So. How do I use good cryptographic practices to secure my data?

Since we want to use the latest and greatest, we pick AES (Advanced Encryption Standard, successor to Date Encryption Standard and Triple Data Encryption Standard or Triple DES), Rijndael 256 to be specific (It’s good enough for government work…). In the System.Security.Cryptography namespace, Microsoft has kindly supplied us with the RijndaelManaged class and a pre-provided implementation of the AES standard. I ALWAYS recommend using a pre provided class instead of attempting to roll your own. The ones from Microsoft have gone through EXTENSIVE testing, code, and security review to verify the correctness of the algorithms and code. Frankly, if you write your own, yours will probably have bugs. Bad ones. Bugs that might get you put on the front page of major news organizations for data security breaches if you do it wrong. Warning given, point made, moving on.

Now, there are a couple of things you’ll need in order to do symmetric encryption when we crack open the RijndaelManaged class and try to create an encryption transform:

  • A Key
  • An Initialization Vector or IV
  • Your Data

The Key

First, the key, which is represented as a byte array of some fixed size. The size of your key is important, the number of bits has to match what the algorithm supports, in the case of Rijndael, it supports key sizes of 128, 192, and 256. In our case, we’ll be using a key size of 256. The number of bytes we need is 256 divided by 8 (remember, the key size for the algorithm is specified in bits, but our code mainly works with bytes). So if I’m declaring a byte array for the key for AES 256, it would be new byte[32].

Derive your Key

Now wait, how the heck to I remember a 32 number combination? I can’t. You probably can’t, and unless you’re a brainiac with an eidetic memory I wouldn’t suggest trying with any reasonable hope of success. So, since I have now come to this sad conclusion about my memory, I need some way to take a password or key that I can memorize and turn it into a 32 byte key. Fortunately, there’s a way to do just that by way of a password based key derivation function called PBKDF2. It’s also a standard (RFC 2898, which is only important because the code uses the name of the spec for the class name and not PBKDF2 like I would expect… go figure). Essentially, PBKDF2 is a piece of code that hashes an arbitrary length of text into a pseudo random key. Now, we could use something like MD5 to do something similar, but PBKDF2is designed with cryptography in mind, and to be slow on purpose. Why does it need to be slow? Simply this, cryptography is a big huge lever that makes it easy to go one way, and hard to go another. MD5 is FAST to compute, and someone trying to break into your encrypted data wants to be able to test as many passwords as possible as fast as possible. PBKDF2is slow and more difficult to compute, and the harder it is, the longer it takes to generate a hash, which dramatically reduces the number of passwords a hacker trying to attack your vault can try per second. (There’s a really good article by Jeff Atwood here: on hashing algorithms in security, rainbow tables, speed hashing, why it’s important to have a slow hash for security, and so on. If you’re interested on reading more about it)

How do we create this hashed key with PBKDF2? The constructor takes 3 things, a string password, an iteration count, and a byte array for something called a salt (Not in that order). The Password is easy, it’s a string or byte array and it’s whatever you choose to use as your “uB3rAw3$omeP@assw0rd!“. iteration count is pretty self-explanatory too, it defaults to 1000, and the bigger the number, the more calculation rounds it does and the longer it takes to calculate the hash. But what about this salt thing? What is it, and why is it important? Do I even care? So here we go.

Salt your Vault

If you’ve heard about salt or salting passwords before, it was probably in reference to a website or service, usually some piece of code that you controlled that sat between your users (or a supposed attacker) and the hashed passwords in your database so that an attacker couldn’t just *make up* a password like zn3lzl that would hash to the same value as the users password like mycoolpassword2 and allow them to log in. If we assume that the attacker has access to your password vault, we can probably assume he has access to your program as well, and it won’t be very hard to figure out some static salt value you’ve stored in your code. So it’s useless right? In some ways yes, in some ways no. IF it’s JUST you, or the attacker is targeting JUST YOUR vault, then yes, it doesn’t matter. However, if for some reason your password vault app becomes popular, and an attacker gains access to, lets say 5000 different peoples vault files, it’s much much easier to check the same passwords across ALL the vaults at once if the salt values are the same. That is again assuming he is not targeting just one particular vault. If they’re all different, the time required to check a password across all the vaults goes up by a factor of 5000, making it less viable to quickly crack multiple vaults. My thought is to simply store the salt along with the vault, since if an attacker has access to your vault, he probably has access to wherever I’d store the salt anyways, and it at least requires him to compute a separate hash for each vault. So, I’ll generate and store a separate, cryptographically random generated salt with my vault file.

Something like this:

  1. var salt = new byte[256];
  3. using (var rnd = RandomNumberGenerator.Create())
  4. {
  5. rnd.GetBytes(salt);
  6. }

As an aside, realize that no amount of encryption can save your data from a bad password. Good hackers are GOING to use huge word lists and fantastic substitution / transposition / combination rules before they even attempt to use a brute force attack. ‘MyL1ttl3P0ny‘ is not a good password. ‘abc123‘ is arguably much worse; but then you’re probably not reading this if abc123 is something you’d use for a password.

Now that we have our password, the salt (loaded from our vault file) and the number of iterations, we can derive a key with the correct size:

  1. var deriveBytes = new Rfc2898DeriveBytes(myPassword, mySalt, 10000);
  2. var aes256Key = deriveBytes.GetBytes(32);

Tada! magic. We now have a byte key from the password that we supplied earlier. Ok, now that we have our key, we can see that when you try to create our encryption or decryption transform that it is requiring something called an initialization vector (IV). I know when I was going through all this that I was thinking “what the heck is an initialization vector?” Since I had to teach myself what it was and why it was important, I’m going to assume somebody also doesn’t know and brain dump what I’ve learned on you all as well.

Block Ciphers, Chaining, and Initialization Vectors

First, we have to understand a few things, how a block ciphers works, how cipher block chaining works, how the IV plays in, and why it’s all important. To begin with, block ciphers. Most computer ciphers these days are done using cipher blocks of a given bit size, usually in round multiples of 2, for example 128 bit or 16 bytes chunks (this is actually the size AES uses). Our clear text gets chunked up into these small, manageable blocks of data. Then encryption transform is then run multiple times on each block, and the resulting blocks are all concatenated together to form the final encrypted output.

Because encrypted blocks are computed independently, changes somewhere in the original unencrypted data might only change the encrypted data for a few blocks of the resulting encrypted output. Furthermore, if you happened to have the same 16 byte chunk repeated again such that it aligned with a different blocks, you will get the exact same block of encrypted output. On one hand, this could be useful if you want to send delta updates to an encrypted file, it also means that attacks against individual blocks are feasibly be easier or useful information could be obtained by comparing one version of the vault to another or even patterns might still exist even within the encrypted date. A really awesome example of why it’s important to apply some additional steps during the encryption process with block ciphers are these three images below: (Courtesy of Wikipedia):

Original | No Block Chaining | Proper Cipher Block Chaining

This is where CBC or cipher block chaining comes in. CBC fixes this problem by taking the previously encrypted block and performing a bitwise XOR between it and the raw bytes of the plain text of the next block to be encrypted. Effectively, this means that a one byte change in the first block will change EVERY OTHER block in the resulting ciphertext. However, there’s one last piece. If the first couple of blocks are NOT changed in any way, you can still leek some information about changes that were made by way of the first changed block. So if I have blocks A B C D E and block C gets changed, CBC will cause the resulting ciphertext for blocks D and E to change. It will NOT affect blocks A and B. So, at last, we finally get to the purpose of the initialization vector. The IV is essentially a completely random block to start off the cipher chain. Makes sense now doesn’t it? If I have a completely random block that I create and pass along, it guarantees that even if the plain text doesn’t change at all, the ciphertext will be completely different each time, assuming I generate a new IV each time I change the cipher text. In practice, encrypted data should be statistically indistinguishable from random noise.

Illustration of the process of a block cipher with CBC for Encryption:

Illustration of the process of a block cipher with CBC for Decryption:

Now that we know why we need an initialization vector, we also know, or can guess, what size it should be without having to look at the documentation (16 bytes, because that’s the block size for AES, and our IV is essentially a known random starting block). Since we should generate a new block and also store it with our vault, I’m going to declare our IV and initialize it the same way we initialized our salt:

  1. var iv = new byte[16];
  3. using (var rnd = RandomNumberGenerator.Create())
  4. {
  5. rnd.GetBytes(mySalt);
  6. rnd.GetBytes(iv);
  7. }

The CryptoStream

Now that we have all the pieces we can put it all together and encrypt our data to a MemoryStream (the memory stream could be anything, including a FileStream, but this is easier for demo and console output):

  1. using(var ms = new MemoryStream())
  2. {
  3. using (var cryptoStream = new CryptoStream(ms, transform.CreateEncryptor(aes256Key, iv), CryptoStreamMode.Write))
  4. {
  5. cryptoStream.Write(plainTextBytes, 0, plainTextBytes.Count());
  6. // cryptoStream.FlushFinalBlock();
  7. }
  9. cipherTextBytes = ms.ToArray();
  10. }

EDIT: Thanks to Nick (see comments), he pointed out that cryptoStream.FlushFinalBlock() is unnecessary if you dispose the cryptoStream before you read the data out from the underlying stream. (This happens automatically with a using statement)

And now the code snippet for decrypting the cypherTextBytes:

  1. using (var ms = new MemoryStream(cipherTextBytes))
  2. {
  3. using (var cryptoStream = new CryptoStream(ms, transform.CreateDecryptor(aes256Key, iv), CryptoStreamMode.Read))
  4. {
  5. var decryptedBytes = new byte[cipherTextBytes.Length];
  6. var length = cryptoStream.Read(decryptedBytes, 0, decryptedBytes.Length);
  8. var decryptedText = Encoding.UTF8.GetString(decryptedBytes.Take(length).ToArray());
  9. }
  10. }

Padding under the covers

Some final notes block cipher padding: You’ll notice that when I’m reading the stream I have the following line of code: decryptedBytes.Take(length).ToArray() When you’re using a block cipher like AES, just like the key has to be exactly a certain size, each block of initial data ALSO has to be a certain size. This means you ARE going to get some extra data tacked on the end of your stream that you weren’t anticipating. There’s a couple of ways this can be handled, RijndaelManaged has a couple of padding modes that it can use ranging from filling all the additional slots of data with zeros, or filling them with completely random data, but by default it uses PaddingMode.PKCS7 which fills each extra bytes needed to make the length of the data an even multiple of 16 bytes with the number that represents the number of padded bytes added to fill the empty space. If you have the exact amount of data to exactly to fill the number of blocks, the padding algorithm will add an extra block to ensure that the last byte in the last block that is read represents the amount of padding. Otherwise, depending on whatever data you’re storing, you could accidentally lose some of your data if it was misinterpreted as padding. The crypto stream is aware of the padding and will return the correct length of the original data on the last read call. I simplified the stream reading process for simplicity of demonstrating the use of the crypto stream and how it handles padding, it just reads everything and trims the result with decryptedBytes.Take(length).ToArray(). In real life, you should use or make a ‘real’ stream reader that reads data out of the stream in chunks and aggregates them together, or just serialize / deserialize your objects directly to and from the crypto stream.


The Full Demo

  1. using System;
  2. using System.IO;
  3. using System.Linq;
  4. using System.Security.Cryptography;
  5. using System.Text;
  7. public class CryptoDemo
  8. {
  9. public static void Main(string[] args)
  10. {
  11. TestEncryptionAndDecryption();
  13. Console.ReadLine();
  14. }
  16. public static void TestEncryptionAndDecryption()
  17. {
  18. const string myPassword = "uB3rAw3$omeP@assw0rd!";
  19. const string myData = "Lorem ipsum dolor sit amet, consectetur adipiscing elit. " +
  20. "Morbi rutrum pulvinar purus, nec ornare neque cursus id. " +
  21. "Nunc non tortor est. Morbi laoreet commodo tellus, et suscipit neque elementum eu. " +
  22. "Sed velit lorem, ultricies id varius vitae, eleifend eget massa. " +
  23. "Curabitur dignissim eleifend quam, sit amet interdum velit rutrum vel. " +
  24. "Nulla nec enim tortor.";
  25. var plainTextBytes = Encoding.UTF8.GetBytes(myData);
  27. Console.WriteLine("Password ({0} bytes): ", Encoding.UTF8.GetBytes(myPassword).Length);
  28. Console.WriteLine(myPassword);
  29. Console.WriteLine();
  31. Console.WriteLine("Plain Text ({0} bytes): ", plainTextBytes.Length);
  32. Console.WriteLine(myData);
  33. Console.WriteLine();
  35. var mySalt = new byte[256];
  36. var iv = new byte[16];
  38. using (var rnd = RandomNumberGenerator.Create())
  39. {
  40. rnd.GetBytes(mySalt);
  41. rnd.GetBytes(iv);
  42. }
  44. var transform = new RijndaelManaged();
  46. Console.WriteLine("Salt ({0} bytes): ", mySalt.Length);
  47. Console.WriteLine(Convert.ToBase64String(mySalt));
  48. Console.WriteLine();
  49. Console.WriteLine("Initilization Vector ({0} bytes): ", iv.Length);
  50. Console.WriteLine(Convert.ToBase64String(iv));
  51. Console.WriteLine();
  53. // Derive the passkey from a hash of the password plus salt with the number of hashing rounds.
  54. var deriveBytes = new Rfc2898DeriveBytes(myPassword, mySalt, 10000);
  56. // This gives us a derived byte key from our password.
  57. var aes256Key = deriveBytes.GetBytes(32);
  59. Console.WriteLine("Derived Key ({0} bytes): ", aes256Key.Length);
  60. Console.WriteLine(Convert.ToBase64String(aes256Key));
  61. Console.WriteLine();
  63. byte[] cipherTextBytes;
  65. using (var ms = new MemoryStream())
  66. {
  67. using (var cryptoStream = new CryptoStream(ms, transform.CreateEncryptor(aes256Key, iv), CryptoStreamMode.Write))
  68. {
  69. cryptoStream.Write(plainTextBytes, 0, plainTextBytes.Count());
  70. // cryptoStream.FlushFinalBlock();
  71. }
  73. cipherTextBytes = ms.ToArray();
  75. Console.WriteLine("Encrypted Text ({0} bytes): ", cipherTextBytes.Length);
  76. Console.WriteLine(Convert.ToBase64String(cipherTextBytes));
  77. Console.WriteLine();
  78. }
  80. // this resets the algorithm. Normally, if we have a seperate encrypt / decrypt method
  81. // we would create a new instance of RijndaelManaged
  82. transform.Clear();
  84. using (var ms = new MemoryStream(cipherTextBytes))
  85. {
  86. using (var cryptoStream = new CryptoStream(ms, transform.CreateDecryptor(aes256Key, iv), CryptoStreamMode.Read))
  87. {
  88. var decryptedBytes = new byte[cipherTextBytes.Length];
  89. var length = cryptoStream.Read(decryptedBytes, 0, decryptedBytes.Length);
  91. var decryptedText = Encoding.UTF8.GetString(decryptedBytes.Take(length).ToArray());
  93. Console.WriteLine("Decrypted Text ({0} bytes): ", decryptedText.Length);
  94. Console.Write(decryptedText);
  95. Console.WriteLine();
  96. }
  97. }
  98. }
  99. }

Sample Output

Password (21 bytes):

Plain Text (355 bytes):
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Morbi rutrum pulvinar purus, nec ornare neque cursus id. Nunc non tortor est. Morbi laoreet commodo tellus, et suscipit neque elementum eu. Sed velit lorem, ultricies id varius vitae, eleifend eget massa. Curabitur dignissim eleifend quam, sit amet interdum velit rutrum vel. Nulla nec enim tortor.

Salt (256 bytes):

Initilization Vector (16 bytes):

Derived Key (32 bytes):

Encrypted Text (368 bytes):

Decrypted Text (355 bytes):
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Morbi rutrum pulvinar purus, nec ornare neque cursus id. Nunc non tortor est. Morbi laoreet commodo tellus, et suscipit neque elementum eu. Sed velit lorem, ultricies id varius vitae, eleifend eget massa. Curabitur dignissim eleifend quam, sit amet interdum velit rutrum vel. Nulla nec enim tortor.

That concludes my epic tour de AES in .NET. It’s like writing papers in college all over again. Hope somebody finds it useful.