1
1
#if ! DISABLE_CRYPTOGRAPHY
2
- using System ;
3
2
using System . Security . Cryptography ;
4
3
using System . IO ;
4
+ using MLAPI . Serialization ;
5
5
6
6
namespace MLAPI . Cryptography
7
7
{
@@ -10,55 +10,53 @@ namespace MLAPI.Cryptography
10
10
/// </summary>
11
11
public static class CryptographyHelper
12
12
{
13
- internal static byte [ ] EncryptionBuffer ;
14
13
private static readonly byte [ ] IVBuffer = new byte [ 16 ] ;
15
14
/// <summary>
16
15
/// Decrypts a message with AES with a given key and a salt that is encoded as the first 16 bytes of the buffer
17
16
/// </summary>
18
- /// <param name="encryptedBuffer ">The buffer with the salt </param>
17
+ /// <param name="encryptedStream ">The encrypted stream </param>
19
18
/// <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 )
22
21
{
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 ( ) )
26
25
{
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 ) )
28
29
{
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 ( ) )
32
31
{
33
- cs . Write ( encryptedBuffer , 16 , encryptedBuffer . Length - 16 ) ;
32
+ outStream . CopyFrom ( cs ) ;
33
+ return outStream ;
34
34
}
35
-
36
- return stream ;
37
35
}
38
36
}
39
37
}
40
38
41
39
/// <summary>
42
40
/// 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
43
41
/// </summary>
44
- /// <param name="clearBuffer ">The buffer to be encrypted</param>
42
+ /// <param name="clearStream ">The stream to be encrypted</param>
45
43
/// <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 )
48
46
{
49
- using ( MemoryStream stream = new MemoryStream ( ) )
47
+ using ( RijndaelManaged aes = new RijndaelManaged ( ) )
50
48
{
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 ) )
52
53
{
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 ( ) )
57
55
{
58
- cs . Write ( clearBuffer , 0 , clearBuffer . Length ) ;
56
+ outStream . Write ( aes . IV , 0 , 16 ) ;
57
+ outStream . CopyFrom ( cs ) ;
58
+ return outStream ;
59
59
}
60
-
61
- return stream ;
62
60
}
63
61
}
64
62
}
0 commit comments