11#if ! DISABLE_CRYPTOGRAPHY
2- using System ;
32using System . Security . Cryptography ;
43using System . IO ;
4+ using MLAPI . Serialization ;
55
66namespace MLAPI . Cryptography
77{
@@ -10,55 +10,53 @@ namespace MLAPI.Cryptography
1010 /// </summary>
1111 public static class CryptographyHelper
1212 {
13- internal static byte [ ] EncryptionBuffer ;
1413 private static readonly byte [ ] IVBuffer = new byte [ 16 ] ;
1514 /// <summary>
1615 /// Decrypts a message with AES with a given key and a salt that is encoded as the first 16 bytes of the buffer
1716 /// </summary>
18- /// <param name="encryptedBuffer ">The buffer with the salt </param>
17+ /// <param name="encryptedStream ">The encrypted stream </param>
1918 /// <param name="clientId">The clientId whose AES key to use</param>
20- /// <returns>The decrypted byte array </returns>
21- public static Stream Decrypt ( byte [ ] encryptedBuffer , uint clientId )
19+ /// <returns>The decrypted stream </returns>
20+ public static Stream DecryptStream ( Stream encryptedStream , uint clientId )
2221 {
23- Array . Copy ( IVBuffer , 0 , IVBuffer , 0 , 16 ) ;
24-
25- using ( MemoryStream stream = new MemoryStream ( EncryptionBuffer ) )
22+ encryptedStream . Read ( IVBuffer , 0 , 16 ) ;
23+
24+ using ( RijndaelManaged aes = new RijndaelManaged ( ) )
2625 {
27- using ( RijndaelManaged aes = new RijndaelManaged ( ) )
26+ aes . IV = IVBuffer ;
27+ aes . Key = NetworkingManager . singleton . ConnectedClients [ clientId ] . AesKey ;
28+ using ( CryptoStream cs = new CryptoStream ( encryptedStream , aes . CreateDecryptor ( ) , CryptoStreamMode . Read ) )
2829 {
29- aes . IV = IVBuffer ;
30- aes . Key = NetworkingManager . singleton . ConnectedClients [ clientId ] . AesKey ;
31- using ( CryptoStream cs = new CryptoStream ( stream , aes . CreateDecryptor ( ) , CryptoStreamMode . Write ) )
30+ using ( PooledBitStream outStream = PooledBitStream . Get ( ) )
3231 {
33- cs . Write ( encryptedBuffer , 16 , encryptedBuffer . Length - 16 ) ;
32+ outStream . CopyFrom ( cs ) ;
33+ return outStream ;
3434 }
35-
36- return stream ;
3735 }
3836 }
3937 }
4038
4139 /// <summary>
4240 /// Encrypts a message with AES with a given key and a random salt that gets encoded as the first 16 bytes of the encrypted buffer
4341 /// </summary>
44- /// <param name="clearBuffer ">The buffer to be encrypted</param>
42+ /// <param name="clearStream ">The stream to be encrypted</param>
4543 /// <param name="clientId">The clientId whose AES key to use</param>
46- /// <returns>The encrypted byte array with encoded salt</returns>
47- public static Stream Encrypt ( byte [ ] clearBuffer , uint clientId )
44+ /// <returns>The encrypted stream with encoded salt</returns>
45+ public static Stream EncryptStream ( Stream clearStream , uint clientId )
4846 {
49- using ( MemoryStream stream = new MemoryStream ( ) )
47+ using ( RijndaelManaged aes = new RijndaelManaged ( ) )
5048 {
51- using ( RijndaelManaged aes = new RijndaelManaged ( ) )
49+ aes . Key = NetworkingManager . singleton . ConnectedClients [ clientId ] . AesKey ; ;
50+ aes . GenerateIV ( ) ;
51+
52+ using ( CryptoStream cs = new CryptoStream ( clearStream , aes . CreateEncryptor ( ) , CryptoStreamMode . Read ) )
5253 {
53- aes . Key = NetworkingManager . singleton . ConnectedClients [ clientId ] . AesKey ; ;
54- aes . GenerateIV ( ) ;
55- stream . Write ( aes . IV , 0 , 16 ) ;
56- using ( CryptoStream cs = new CryptoStream ( stream , aes . CreateEncryptor ( ) , CryptoStreamMode . Write ) )
54+ using ( PooledBitStream outStream = PooledBitStream . Get ( ) )
5755 {
58- cs . Write ( clearBuffer , 0 , clearBuffer . Length ) ;
56+ outStream . Write ( aes . IV , 0 , 16 ) ;
57+ outStream . CopyFrom ( cs ) ;
58+ return outStream ;
5959 }
60-
61- return stream ;
6260 }
6361 }
6462 }
0 commit comments