Rijndael – Algoritm de criptare a datelor

Dupa parerea expertilor in domeniu AES:Rijndael este cel mai bun algoritm de criptare. Algoritmul dezvoltat de catre Daemen si Vincent Rijmen a castigat concursul “Advanced Encryption Standard”. Este un “block cipher” pe 128 de biti cu o cheie de maxim 256 de biti. (sursa: http://copiatac.3x.ro/Proiecte_AC/CipariuDanciu.htm)

Cod C# pentru criptarea unui string

private static RijndaelManaged rijndaelCipher = new RijndaelManaged();
private static string secretSalt = “string care contine litere, cifre si caractere speciale“;

private static string EncryptString(string toEncrypt) {
try {
byte[] plainText = Encoding.Unicode.GetBytes(toEncrypt);
// Using salt to make it harder to guess the key using a dictionary attack.
byte[] salt = Encoding.ASCII.GetBytes(secretSalt.Length.ToString());
// The (secret Key) will be generated from the specified password and salt.
PasswordDeriveBytes secretKey = new PasswordDeriveBytes(secretSalt, salt);
// Create a encryptor from the existing SecretKey bytes. Is used 32 bytes
//for the secret key, (the default rijndael key length is 256 bit = 32 bytes)
//and then 16 bytes for the IV (initialization vector),the default Rijndael IV length is 128 bit = 16 bytes

ICryptoTransform encryptor = rijndaelCipher.CreateEncryptor(secretKey.GetBytes(32), secretKey.GetBytes(16));
// Create a MemoryStream that is going to hold the encrypted bytes
MemoryStream memoryStream = new MemoryStream();
// Create a CryptoStream through which will be processed the data. CryptoStreamMode.Write means
//that is going to be writing data to the stream and
//the output will be written in the MemoryStream provided. (always use write mode for encryption)
CryptoStream cryptoStream = new CryptoStream(memoryStream, encryptor, CryptoStreamMode.Write);
// Start the encryption process.
cryptoStream.Write(plainText, 0, plainText.Length);
// Finish encrypting.
// Convert encrypted data from a memoryStream into a byte array.
byte[] cipherBytes = memoryStream.ToArray();
// Close both streams.
// Convert encrypted data into a base64-encoded string. A common mistake would be to use
//an Encoding class for that. It does not work, because not all byte values can be represented
//by characters. Using Base64 encoding, that is designed exactly for what we are trying to do.

string encryptedData = Convert.ToBase64String(cipherBytes);
// Return encrypted string.
return encryptedData;
} catch (Exception ex) {
return null;

private static string DecryptString(string toDecrypt) {
try {
byte[] encryptedData = Convert.FromBase64String(toDecrypt);
byte[] salt = Encoding.ASCII.GetBytes(secretSalt.Length.ToString());
PasswordDeriveBytes secretKey = new PasswordDeriveBytes(secretSalt, salt);
// Create a decryptor from the existing secretKey bytes.
ICryptoTransform decryptor = rijndaelCipher.CreateDecryptor(secretKey.GetBytes(32), secretKey.GetBytes(16));
MemoryStream memoryStream = new MemoryStream(encryptedData);
// Create a CryptoStream. (always use Read mode for decryption).
CryptoStream cryptoStream = new CryptoStream(memoryStream, decryptor, CryptoStreamMode.Read);
// Since at this point we don’t know what the size of decrypted data will be, allocate the
//buffer long enough to hold encryptedData, decryptedData is never longer than encryptedData.

byte[] plainText = new byte[encryptedData.Length];
// Start decrypting.
int decryptedCount = cryptoStream.Read(plainText, 0, plainText.Length);
// Convert decrypted data into a string.
string decryptedData = Encoding.Unicode.GetString(plainText, 0, decryptedCount);
// Return decrypted string.
return decryptedData;
} catch (Exception ex) {
return null;

Pentru criptarea unui fisier singura diferenta este ca in loc de folosirea unui memory stream se va folosi file stream


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s