public void SymmetricEncryptor_HeavyUsage() { CryptoRandomizer random = new CryptoRandomizer(); const int Iterations = 100; const int MaxMemoryBlock = 100000; for (int i = 0; i < Iterations; i++) { int blockSize = random.Next(MaxMemoryBlock); byte[] buffer = new ByteGenerator().GenerateBytes(blockSize); string key = CryptoString.GenerateRandomText(1000); byte[] decryptedBuffer; byte[] encryptedBuffer; using (SymmetricEncryptor encryptor = new SymmetricEncryptor()) { //Encrypt encryptedBuffer = encryptor.EncryptBytes(buffer, key); // Decrypt decryptedBuffer = encryptor.DecryptBytes(encryptedBuffer, key); } // IDispose - Closes and clears the keys in memory // Assert - Check to make sure the bytes are all the same Assert.IsTrue(buffer.SequenceEqual(decryptedBuffer)); } }
/// <summary> /// Decrypts the bytes of the Blob and validates the checksum hash to ensure data integrity /// </summary> /// <returns></returns> public byte[] Decrypt(bool validateChecksum = true) { // Validate Encrypted (if flag is set) if (validateChecksum) { ValidateChecksum(_encryptedBytes); } // Encrypted Block byte[] checksumEncrypted = _encryptedBytes.Skip(_encryptedBytes.Length - HashSize).ToArray(); byte[] encryptedBytes = _encryptedBytes.Take(_encryptedBytes.Length - HashSize).ToArray(); // Decrypted Block byte[] fullDecryptedData = _encryptor.DecryptBytes(encryptedBytes); byte[] checksumDecrypted = fullDecryptedData.Skip(fullDecryptedData.Length - HashSize).ToArray(); // Validate Decrypted (if flag is set) if (validateChecksum) { ValidateChecksum(fullDecryptedData); } // Strip the Checksum and you have your original data byte[] decryptedBytes = fullDecryptedData.Take(fullDecryptedData.Length - HashSize).ToArray(); return(decryptedBytes); }
public void SymmetricEncryptor_BasicUsage() { // Arrange int blockSize = 1000; byte[] buffer = new ByteGenerator().GenerateBytes(blockSize); string key = "test key"; byte[] decryptedBuffer; byte[] encryptedBuffer; using (SymmetricEncryptor encryptor = new SymmetricEncryptor()) { //Encrypt encryptedBuffer = encryptor.EncryptBytes(buffer, key); // Decrypt decryptedBuffer = encryptor.DecryptBytes(encryptedBuffer, key); } // IDispose - Closes and clears the keys in memory // Assert - Check to make sure the bytes are all the same Assert.IsTrue(buffer.SequenceEqual(decryptedBuffer)); }