Skip to content

MirzaCryptoHelpers.SymmetricCryptos

Mirza Ghulam Rasyid edited this page Oct 6, 2017 · 3 revisions

MirzaCryptoHelpers.SymmetricCryptos Namespace

This namespace contains a few wrappers for symmetric cryptography operations. All core classes here implement ISymmetricCrypto interface that only provides two basic methods:

byte[] Encrypt(byte[] data, string password);
byte[] Decrypt(byte[] data, string password);

You can provide your own implementation for encryption/decryption process from this interface and there is one class for dependency injection named SymmetricCrypto class that accepts ISymmetricCrypto interface for one of its constructors. If no parameter used, an instance of AESCrypto will be used. This namespace provides streamlined version of all .NET Cryptography operations with simplicity as the main goal of this helper library.

Samples in this wiki will use AESCrypto class as all core classes have same signatures.

Add namespaces first

using MirzaCryptoHelpers.SymmetricCryptos;
using MirzaCryptoHelpers.Common; //for BitHelpers class

Encrypt & Decrypt Data With Default IV

string input = "I need to be encrypted!!!";
byte[] inputBytes = BitHelpers.ConvertStringToBytes(input);
string password = "PASSWORD12345";
byte[] cipherBytes = new AESCrypto().Encrypt(inputBytes, password); //encrypted data in bytes
byte[] plainBytes = new AESCrypto().Decrypt(cipherBytes, password); //decrypted data in bytes

Encrypt & Decrypt Data With Self-Generated IV

Basically, this IV will be generated by Encryption method as out parameter. You must save this IV in order to decrypt data with your password.

string input = "I need to be encrypted!!!";
byte[] inputBytes = BitHelpers.ConvertStringToBytes(input);
string password = "PASSWORD12345";
bytes[] myIV;
byte[] cipherBytes = new AESCrypto().Encrypt(inputBytes, password, out myIV); //encrypted data in bytes
byte[] plainBytes = new AESCrypto().Decrypt(cipherBytes, password, myIV); //decrypted data in bytes

Encrypt & Decrypt Data With Your Own IV

You can create your own IV using BitHelpers.GenerateRandomNumbers(16) where 16 is the valid IV size for AES encryption in this class. You can get default IV size from ValidIVSize property in any class that Implements ISymmetricCrypto namespace. Ex: new AESCrypto().ValidIVSize

string input = "I need to be encrypted!!!";
byte[] inputBytes = BitHelpers.ConvertStringToBytes(input);
string password = "PASSWORD12345";
bytes[] myIV = BitHelpers.GenerateRandomNumbers(16); //creating IV, you must save it to decrypt data along with password
byte[] cipherBytes = new AESCrypto().Encrypt(inputBytes, password, myIV); //encrypted data in bytes
byte[] plainBytes = new AESCrypto().Decrypt(cipherBytes, password, myIV); //decrypted data in bytes

Clone this wiki locally