/// <summary> /// Decrypts and returns the byte that this <see cref="SafeByte" /> instance represents. /// </summary> /// <returns></returns> /// <exception cref="System.InvalidOperationException">Thrown when byte is not set</exception> public byte Get() { EnsureByteIsSet(); byte[] byteBuffer = null; try { _memoryProtector.Unprotect(_encryptionKey); _memoryProtector.Unprotect(_encryptedByte); var encryptedBuffer = new byte[_encryptedByteLength]; try { Buffer.BlockCopy(_encryptedByte, 0, encryptedBuffer, 0, _encryptedByteLength); byteBuffer = _encryptor.Decrypt(encryptedBuffer, _encryptionKey); //Extract the byte from arbitrary bytes return(byteBuffer[_realBytePosition]); } finally { Array.Clear(encryptedBuffer, 0, _encryptedByteLength); } } finally { if (byteBuffer != null) { Array.Clear(byteBuffer, 0, byteBuffer.Length); } _memoryProtector.Protect(_encryptionKey); _memoryProtector.Protect(_encryptedByte); } }
public void Unprotect(byte[] encryptedData) { if (encryptedData == null) { throw new ArgumentNullException(nameof(encryptedData)); } var decryptedBytes = _encryptor.Decrypt(encryptedData, _key); SetBytesToByteArray( source: decryptedBytes, target: ref encryptedData); }
private ICollection <int> DecryptAndDeserialize(byte[] encryptedCollection, byte[] encryptionKey) { if (encryptedCollection == null) { return(new List <int>()); } var decryptedBytes = _encryptor.Decrypt(encryptedCollection, encryptionKey); try { var deserializedBytes = Deserialize(decryptedBytes); return(deserializedBytes.ToList()); } finally { Array.Clear(decryptedBytes, 0, decryptedBytes.Length); } }