Exemplo n.º 1
0
        public async Task <byte[]> ValidateAndDecrypt(EncryptThenMacData encryptThenMacData)
        {
            byte[] data = Convert.FromBase64String(encryptThenMacData.Data);
            byte[] hmac = Convert.FromBase64String(encryptThenMacData.Hmac);
            if (!await _hmacService.IsValid(hmac, data))
            {
                throw new Exception("HMAC is not valid.");
            }
            AesEncryptedData aesEncryptedData = _serializationService.DeserializeFromBytes <AesEncryptedData>(data);

            return(await _aesEncryptionService.Decrypt(aesEncryptedData));
        }
Exemplo n.º 2
0
        public async Task <EncryptThenMacData> EncryptThenMac(byte[] data)
        {
            AesEncryptedData aesEncryptedData = await _aesEncryptionService.Encrypt(data);

            byte[] serializedAesEncryptedData = _serializationService.SerializeToBytes(aesEncryptedData);
            byte[] hmac = await _hmacService.Generate(serializedAesEncryptedData);

            return(new EncryptThenMacData()
            {
                Data = Convert.ToBase64String(serializedAesEncryptedData),
                Hmac = Convert.ToBase64String(hmac)
            });
        }
Exemplo n.º 3
0
 public Task <byte[]> Decrypt(AesEncryptedData aesEncryptedData)
 {
     return(Task.FromResult(
                XorCipher(aesEncryptedData.EncryptedData, aesEncryptedData.Iv)
                ));
 }