Tag Archives: encryption

Get the MD5 hash of a file in .Net

MSDN has a page on how to get the MD5 hash of a string.

Easy enough, but if you follow their example exactly for a file and use File.ReadAllText() to get the string, you will get the wrong MD5 string for binary files. Instead, use File.ReadAllBytes() to bypass encoding issues. (This also applies to SHA1 hashing.)

private static string GetMD5Hash(string filePath)
        {
            byte[] computedHash = new MD5CryptoServiceProvider().ComputeHash(File.ReadAllBytes(filePath));
            var sBuilder = new StringBuilder();
            foreach (byte b in computedHash)
            {
                sBuilder.Append(b.ToString("x2").ToLower());
            }
            return sBuilder.ToString();
        }

AES encryption strategies with .Net

I did a presentation last week on AES encryption techniques in .Net.

I’ll post some details here later, but for now, I’ve uploaded a zip file with the project code.

Here’s the key bit:

 
            string key = "1234567891123456";
            string secret = @"This is a secret.";
 
            Console.WriteLine("basic:");
            EncryptString(key, secret);
            Console.ReadKey();
 
            Console.WriteLine("salt the secret:");
            // good when there are multiple machines but a dynamic global shared secret (for example, Profile Create Date or User ID)
            string secret2 = secret + " ###" + DateTime.Now.Millisecond;
            EncryptString(key, secret2);
 
            secret2 = secret + " ###" + DateTime.Now.Millisecond;
            EncryptString(key, secret2);
 
            Console.ReadKey();
 
            Console.WriteLine("salt the key:"); 
            // good when the same machine encrypts/decrepts
            string uniqueMachineIdentifier = MachineId.GetProcessorID();
            Console.WriteLine("MachineId: " + uniqueMachineIdentifier);
            EncryptString(key + uniqueMachineIdentifier, secret);
            Console.ReadKey();
 
            Console.WriteLine("SHA1 hash the passphrase with a salt:"); 
            // note: talk about why hashing is good
            SHA1 sha = new SHA1CryptoServiceProvider();
            // This is one implementation of the abstract class SHA1.
            string password = "this is my user password and/or userid";
            byte[] saltedKey = Encoding.Default.GetBytes(key + password);
 
            byte[] result = sha.ComputeHash(saltedKey);
            EncryptString(Convert.ToBase64String(result), secret);
            Console.ReadKey();

AES interoperability between .Net and iPhone

One of my projects requires encrypting data on the iPhone and decrypting it using .Net. This is easy to do with the Common Crypto library in the iPhone SDK and the AesCryptoServiceProvider class in .Net, but the encryption parameters have to be the same for it to work.

I couldn’t figure it out, but the geniuses at StackOverflow did, so I am posting my results here. The zip file includes a basic iPhone app and a .Net console project with helpful classes to do the encryption/decryption and base64 conversion. I didn’t write most of the code – thanks to Blue Beetle for the .Net code and Greg Haygood for the Objective C.

Download zip.