Cryptography

Cryptography

Cryptography vs encryption: Cryptography is the science of concealing messages with a secret code. Encryption is the way to encrypt and decrypt data.

.Net provides classes in the namespace System.Security.Cryptography

Rfc2898DeriveBytes

Suggested use case would be for a one way hash although it CAN BE de-hashed with a key and salt.

HMACSHA512

C# code examples here.

Suggested use case would be for signing a payload so it can be de-hashed with a key, the salt is optional.

Ciphertext is encrypted text transformed from plaintext using an encryption algorithm. Ciphertext can’t be read until it has been converted into plaintext (decrypted) with a key. The decryption cipher is an algorithm that transforms the ciphertext back into plaintext.

SHA2

The most commonly used is SHA-256 as there are many SHA2 variants. produces a 256-bit has (64 hexadecimal digits).

SHA1

Has known to be insecure with a bunch of practical collision attacks already publicly disclosed.

SHA1, Secure Hash Algorithm v1 produces a 160-bit hash (20 bytes). In hexadecimal format it is an integer 40 digits long.

Example from geekytidbits.com

1
2
3
4
5
6
7
8
9
var password = "qwerty";
var salt = "MySecretSalt";
var hasher = new SHA1CryptoServiceProvider();

var textWithSaltBytes = Encoding.UTF8.GetBytes(string.Concat(password, salt));
var hashedBytes = hasher.ComputeHash(textWithSaltBytes);
hasher.Clear();

var hashedPassword = Convert.ToBase64String(hashedBytes);

MD5

Has known to be insecure with a bunch of practical collision attacks already publicly disclosed.

MD5 produces a 128-bit hash value, there is an example here to hash a password using MD5 with salt.

1
2
3
4
5
carl                             // string
a0df931e7a7f9b608c165504bde9b620 // md5 hash of this string

cArl // string
aeab68e8acc704f7d63e677a32bd8d4f // md5 hash of this string

References