public string Decrypt(ProtectedMemory key, string message) { Blake2bProtectedCryptoProvider blake2b = new Blake2bProtectedCryptoProvider(); using ProtectedMemory paddedKey = blake2b.ComputeHashProtected(key, 64); ExtractHeader(message, paddedKey, out byte[] iv, out byte[] buffer); using (ProtectedMemory aesKey = DeriveAesKey(paddedKey)) { AesState aesState = new AesState(aesKey, iv); aesState.AesCbcDecryptBuffer(ref buffer); } int contentLength = Pkcs7Padding.GetContentLength(buffer, 16); return(Encoding.UTF8.GetString(buffer, 0, contentLength)); }
public ProtectedMemory DecryptProtected(ProtectedMemory key, string message) { Blake2bProtectedCryptoProvider blake2b = new Blake2bProtectedCryptoProvider(); using ProtectedMemory paddedKey = blake2b.ComputeHashProtected(key, 64); ExtractHeader(message, paddedKey, out byte[] iv, out byte[] messageBytes); using ProtectedMemory protectedBuffer = ProtectedMemory.Allocate(messageBytes.Length); protectedBuffer.Write(messageBytes, 0, messageBytes.Length); using (ProtectedMemory aesKey = DeriveAesKey(paddedKey)) { AesState aesState = new AesState(aesKey, iv); aesState.AesCbcDecryptBuffer(protectedBuffer); } int contentLength = Pkcs7Padding.GetContentLength(protectedBuffer, 16); ProtectedMemory result = ProtectedMemory.Allocate(contentLength); protectedBuffer.CopyTo(0, result, 0, contentLength); return(result); }