private void Encrypt(byte b) { //Mix the with arbitrary bytes _realBytePosition = _fastRandom.GetInt(0, SaltSize); var arbitraryBytes = _fastRandom.GetBytes(SaltSize); RuntimeHelper.ExecuteCodeWithGuaranteedCleanup( //Action () => { arbitraryBytes[_realBytePosition] = b; //Get key _encryptionKey = _fastRandom.GetBytes(KeySize); //Encrypt var encryptedBuffer = _encryptor.Encrypt(arbitraryBytes, _encryptionKey); RuntimeHelper.ExecuteCodeWithGuaranteedCleanup( //Action () => { //Add arbitrary bytes _encryptedByteLength = encryptedBuffer.Length; _encryptedByte = GetMemoryProtectableSizedBytes(encryptedBuffer); }, //Cleanup () => { if (encryptedBuffer != null) { Array.Clear(encryptedBuffer, 0, encryptedBuffer.Length); } }); }, //Cleanup () => { Array.Clear(arbitraryBytes, 0, arbitraryBytes.Length); }); }
internal EncryptedSafeByteCollection(IFastEncryptor encryptor, IByteArrayProtector memoryProtector, IFastRandom fastRandom, ISafeByteFactory safeByteFactory) { if (encryptor == null) { throw new ArgumentNullException(nameof(encryptor)); } if (memoryProtector == null) { throw new ArgumentNullException(nameof(memoryProtector)); } if (safeByteFactory == null) { throw new ArgumentNullException(nameof(safeByteFactory)); } _encryptor = encryptor; _memoryProtector = memoryProtector; _safeByteFactory = safeByteFactory; _encryptionKey = fastRandom.GetBytes(_memoryProtector.BlockSizeInBytes); _memoryProtector.Protect(_encryptionKey); }
/// <summary> /// Creates and sets a random initialization vector. /// </summary> /// <returns>The random IV</returns> public byte[] SetRandomIV() { InitVector = _random.GetBytes(8); IVSet = true; return(InitVector); }