5. Adapdev.Cryptography

5.1. Encryption and Decryption

All encryption occurs using the Crypto class. The Crypto class supports the following algorithms:

  • DES

  • TripleDES

  • Rijndael

Below are examples for the various algorithms:

5.1.1. DES

Encrypting / Decrypting a string

// Get the encrypted value using the built in key and vector
byte[] cipherText = Crypto.Encrypt("some text to encrypt", 
                                    EncryptionAlgorithm.Des);

string plainText = Crypto.Decrypt(cipherText, 
                                  EncryptionAlgorithm.Des);

Encrypting / Decrypting to a file

// Get the encrypted value using the built in key and vector
byte[] cipherText = Crypto.EncryptToFile("encrypted.dat", "some text to encrypt", 
                                         EncryptionAlgorithm.Des);

string plainText = Crypto.DecryptFromFile("encrypted.dat", EncryptionAlgorithm.Des);

Encrypting / Decrypting a string using a custom key and vector

// DES requires an 8-character key and vector
string key = "password";
string vector = "init vec";

// Get the encrypted value using the built in key and vector
byte[] cipherText = Crypto.Encrypt("some text to encrypt", key, 
                                   vector, EncryptionAlgorithm.Des);

string plainText = Crypto.Decrypt(cipherText, key, 
                                  vector, EncryptionAlgorithm.Des);

Encrypting / Decrypting to a file using a custom key and vector

// DES requires an 8-character key and vector
string key = "password";
string vector = "init vec";

// Get the encrypted value using the built in key and vector
byte[] cipherText = Crypto.EncryptToFile("encrypted.dat", "some text to encrypt", 
                                         key, vector, EncryptionAlgorithm.Des);

string plainText = Crypto.DecryptFromFile("encrypted.dat", key, 
                                          vector, EncryptionAlgorithm.Des);

5.1.2. TripleDES

Encrypting / Decrypting a string

// Get the encrypted value using the built in key and vector
byte[] cipherText = Crypto.Encrypt("some text to encrypt", EncryptionAlgorithm.TripleDes);

string plainText = Crypto.Decrypt(cipherText, EncryptionAlgorithm.TripleDes);

Encrypting / Decrypting to a file

// Get the encrypted value using the built in key and vector
byte[] cipherText = Crypto.EncryptToFile("encrypted.dat", 
                                         "some text to encrypt", 
                                         EncryptionAlgorithm.TripleDes);

string plainText = Crypto.DecryptFromFile("encrypted.dat", EncryptionAlgorithm.TripleDes);

Encrypting / Decrypting a string using a custom key and vector

// TripleDES requires an 16-character key and 8-character vector
string key = "password12345678";
string vector = "init vec";

// Get the encrypted value using the built in key and vector
byte[] cipherText = Crypto.Encrypt("some text to encrypt", key, 
                                   vector, EncryptionAlgorithm.TripleDes);

string plainText = Crypto.Decrypt(cipherText, key, 
                                  vector, EncryptionAlgorithm.TripleDes);

Encrypting / Decrypting to a file using a custom key and vector

// TripleDES requires an 16-character key and 8-character vector
string key = "password12345678";
string vector = "init vec";

// Get the encrypted value using the built in key and vector
byte[] cipherText = Crypto.EncryptToFile("encrypted.dat", "some text to encrypt", key, 
                                          vector, EncryptionAlgorithm.TripleDes);

string plainText = Crypto.DecryptFromFile("encrypted.dat", key, 
                                          vector, EncryptionAlgorithm.TripleDes);

5.1.3. Rijndael

Encrypting / Decrypting a string

// Get the encrypted value using the built in key and vector
byte[] cipherText = Crypto.Encrypt("some text to encrypt", EncryptionAlgorithm.Rijndael);

string plainText = Crypto.Decrypt(cipherText, EncryptionAlgorithm.Rijndael);

Encrypting / Decrypting to a file

// Get the encrypted value using the built in key and vector
byte[] cipherText = Crypto.EncryptToFile("encrypted.dat", 
                                         "some text to encrypt", 
                                         EncryptionAlgorithm.Rijndael);

string plainText = Crypto.DecryptFromFile("encrypted.dat", EncryptionAlgorithm.Rijndael);

Encrypting / Decrypting a string using a custom key and vector

// Rijndael requires an 16-character key and vector
string key = "password12345678";
string vector = "init vec is big.";

// Get the encrypted value using the built in key and vector
byte[] cipherText = Crypto.Encrypt("some text to encrypt", key, 
                                   vector, EncryptionAlgorithm.Rijndael);

string plainText = Crypto.Decrypt(cipherText, key, 
                                  vector, EncryptionAlgorithm.Rijndael);

Encrypting / Decrypting to a file using a custom key and vector

// Rijndael requires an 16-character key and vector
string key = "password12345678";
string vector = "init vec is big.";

// Get the encrypted value using the built in key and vector
byte[] cipherText = Crypto.EncryptToFile("encrypted.dat", "some text to encrypt", key, 
                                          vector, EncryptionAlgorithm.Rijndael);

string plainText = Crypto.DecryptFromFile("encrypted.dat", key, 
                                          vector, EncryptionAlgorithm.Rijndael);

5.2. Hashing

Hashing is a way to provide a forward-only encryption of a string. This means that no key exists for decryption, so you simply rehash an item and compare the hash result to see if they're the same. This is an excellent technique for password storage, since a hacker can't decrypt the password - they can only hash passwords and try to find a match.

Here's an example of hashing:

string hash = Hasher.Hash("some text");
Console.WriteLine(hash);

// Output is: VS4hzUzZkYZ448Gg30kbww==

In order to compare has values, simply use the static IsHashEqual method:

string hash = Hasher.Hash("some text");

bool areEqual = Hasher.IsHashEqual("some text", hash); // returns true;
bool areEqual2 = Hasher.IsHashEqual("some other text", hash); // returns false;

The following hashing algorithms are supported:

  • MD5

  • SHA

  • SHA256

  • SHA384

  • SHA512

Below are examples using the various algorithms.

5.2.1. Default Hashing (MD5)

By default, Hasher uses the MD5 algorithm:

string hash = Hasher.Hash("some text"); // uses MD5
bool areEqual = Hasher.IsHashEqual("some text", hash); // compares using MD5

You can also explicitly specify MD5:

string hash = Hasher.Hash("some text", Hasher.HashType.MD5);
bool areEqual = Hasher.IsHashEqual("some text", hash, Hasher.HashType.MD5);

5.2.2. Hashing with SHA*

You can specify which SHA algorithm to use:

// Use basic SHA
string hash = Hasher.Hash("some text", Hasher.HashType.SHA);
bool areEqual = Hasher.IsHashEqual("some text", hash, Hasher.HashType.SHA);
// Use basic SHA256
string hash = Hasher.Hash("some text", Hasher.HashType.SHA256);
bool areEqual = Hasher.IsHashEqual("some text", hash, Hasher.HashType.SHA256);
// Use basic SHA384
string hash = Hasher.Hash("some text", Hasher.HashType.SHA384);
bool areEqual = Hasher.IsHashEqual("some text", hash, Hasher.HashType.SHA384);
// Use basic SHA512
string hash = Hasher.Hash("some text", Hasher.HashType.SHA512);
bool areEqual = Hasher.IsHashEqual("some text", hash, Hasher.HashType.SHA512);