Skip to content

Commit

Permalink
feat: Added ScrambledPayload to TLLVCrypto
Browse files Browse the repository at this point in the history
  • Loading branch information
diegojfer committed Jul 30, 2019
1 parent 56bdb3b commit dd475d4
Showing 1 changed file with 24 additions and 0 deletions.
24 changes: 24 additions & 0 deletions src/Crypto/TLLVCrypto.cs
Original file line number Diff line number Diff line change
Expand Up @@ -58,5 +58,29 @@ public static byte[] UnscrambledPayload(this CKCMessage message, byte[] key)
}
}
}

public static byte[] ScrambledPayload(byte[] payload, byte[] iv, byte[] key)
{
ArgumentThrow.IfLengthNotMultiple(payload, 16, "Invalid payload to scramble. The buffer length should be multiple of 16.", nameof(payload));
ArgumentThrow.IfLengthNot(iv, 16, "Invalid key to scramble. Key buffer should be 16 bytes.", nameof(iv));
ArgumentThrow.IfLengthNot(key, 16, "Invalid key to scramble. Key buffer should be 16 bytes.", nameof(key));

using (var aes = Aes.Create())
{
aes.IV = iv;
aes.Key = key;
aes.Mode = CipherMode.CBC;
aes.Padding = PaddingMode.None;

using (var encryptor = aes.CreateEncryptor())
using (var memoryStream = new MemoryStream())
using (var cryptoStream = new CryptoStream(memoryStream, encryptor, CryptoStreamMode.Write))
{
cryptoStream.Write(payload);

return memoryStream.ToArray();
}
}
}
}
}

0 comments on commit dd475d4

Please sign in to comment.