Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Read all available data in CryptoStream.Read() #1006

Merged
merged 5 commits into from
Jul 28, 2021
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 12 additions & 2 deletions SteamKit2/SteamKit2/Util/CryptoHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ public static byte[] AESDecrypt( byte[] input, byte[] key, byte[] iv )
using ( var ms = new MemoryStream( input ) )
using ( var cs = new CryptoStream( ms, aesTransform, CryptoStreamMode.Read ) )
{
outLen = cs.Read( plainText, 0, plainText.Length );
outLen = ReadAll( cs, plainText );
}

byte[] output = new byte[ outLen ];
Expand Down Expand Up @@ -406,7 +406,7 @@ static byte[] SymmetricDecrypt( byte[] input, byte[] key, out byte[] iv )
// plaintext is never longer than ciphertext
byte[] plaintext = new byte[ cipherText.Length ];

int len = cs.Read( plaintext, 0, plaintext.Length );
int len = ReadAll( cs, plaintext );

byte[] output = new byte[ len ];
Array.Copy( plaintext, 0, output, 0, len );
Expand Down Expand Up @@ -538,5 +538,15 @@ public static byte[] GenerateRandomBlock( int size )
}
}

private static int ReadAll( Stream stream, byte[] buffer )
xPaw marked this conversation as resolved.
Show resolved Hide resolved
{
int bytesRead;
int totalRead = 0;
while ( ( bytesRead = stream.Read( buffer, totalRead, buffer.Length - totalRead ) ) != 0 )
{
totalRead += bytesRead;
}
return totalRead;
}
}
}
8 changes: 7 additions & 1 deletion SteamKit2/SteamKit2/Util/ZipUtil.cs
Original file line number Diff line number Diff line change
Expand Up @@ -265,7 +265,13 @@ private static byte[] InflateBuffer( byte[] compressedBuffer, UInt32 decompresse
using ( DeflateStream deflateStream = new DeflateStream( ms, CompressionMode.Decompress ) )
{
byte[] inflated = new byte[ decompressedSize ];
deflateStream.Read( inflated, 0, inflated.Length );

int bytesRead;
int totalRead = 0;
while ( ( bytesRead = deflateStream.Read( inflated, totalRead, inflated.Length - totalRead ) ) != 0 )
{
totalRead += bytesRead;
}

return inflated;
}
Expand Down
32 changes: 32 additions & 0 deletions SteamKit2/Tests/CryptoHelperFacts.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
using System;
using System.Net;
using System.Security.Cryptography;
using System.Text;
using SteamKit2;
using Xunit;

namespace Tests
{
public class CryptoHelperFacts
{
[Fact]
public void TestSymmetricEncryption()
{
const string decryptedExpected = "this is a 24 byte string";
const string encryptionKey = "encryption key";

using var sha256 = SHA256.Create();
var key = sha256.ComputeHash( Encoding.UTF8.GetBytes( encryptionKey ) );

var encryptedData = Encoding.UTF8.GetBytes( decryptedExpected );
encryptedData = CryptoHelper.SymmetricEncrypt( encryptedData, key );
var encryptedString = Convert.ToBase64String( encryptedData );

var decryptedData = Convert.FromBase64String( encryptedString );
decryptedData = CryptoHelper.SymmetricDecrypt( decryptedData, key );
var decryptedString = Encoding.UTF8.GetString( decryptedData );

Assert.Equal( decryptedExpected, decryptedString );
}
}
}