private async Task <BinaryData> ClientSideDecryptInternal(BinaryData downloadedMessage, bool async, CancellationToken cancellationToken) { if (!EncryptedMessageSerializer.TryDeserialize(downloadedMessage, out var encryptedMessage)) { return(downloadedMessage); // not recognized as client-side encrypted message } var encryptedMessageStream = new MemoryStream(Convert.FromBase64String(encryptedMessage.EncryptedMessageText)); var decryptedMessageStream = await _decryptor.DecryptReadInternal( encryptedMessageStream, encryptedMessage.EncryptionData, ivInStream : false, noPadding : false, async : async, cancellationToken).ConfigureAwait(false); // if we got back the stream we put in, then we couldn't decrypt and are supposed to return the original // message to the user if (encryptedMessageStream == decryptedMessageStream) { return(downloadedMessage); } return(async ? await BinaryData.FromStreamAsync(decryptedMessageStream, cancellationToken).ConfigureAwait(false) : BinaryData.FromStream(decryptedMessageStream)); }
public void TryDeserializeGracefulOnBadInput(string input) { bool tryResult = EncryptedMessageSerializer.TryDeserialize(new BinaryData(input), out var parsedEncryptedMessage); Assert.AreEqual(false, tryResult); Assert.IsNull(parsedEncryptedMessage?.EncryptedMessageText); Assert.IsNull(parsedEncryptedMessage?.EncryptionData); Assert.IsNull(parsedEncryptedMessage); }
public void TryDeserializeEncryptedMessage() { var result = new ClientSideEncryptor(new ClientSideEncryptionOptions(ClientSideEncryptionVersion.V1_0) { KeyEncryptionKey = GetIKeyEncryptionKey().Object, KeyWrapAlgorithm = KeyWrapAlgorithm }).BufferedEncryptInternal( new MemoryStream(Encoding.UTF8.GetBytes(TestMessage)), async: false, default).EnsureCompleted(); var encryptedMessage = new EncryptedMessage() { EncryptedMessageText = Convert.ToBase64String(result.Ciphertext), EncryptionData = result.EncryptionData }; var serializedMessage = EncryptedMessageSerializer.Serialize(encryptedMessage); bool tryResult = EncryptedMessageSerializer.TryDeserialize(serializedMessage, out var parsedEncryptedMessage); Assert.AreEqual(true, tryResult); Assert.IsTrue(AreEqual(encryptedMessage, parsedEncryptedMessage)); }
public void TryDeserializeGracefulOnBadInput(string input) { bool tryResult = EncryptedMessageSerializer.TryDeserialize(input, out var parsedEncryptedMessage); Assert.AreEqual(false, tryResult); Assert.AreEqual(default, parsedEncryptedMessage);