2014-05-31 01:59:03 +02:00
|
|
|
|
using System;
|
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
|
using System.Linq;
|
|
|
|
|
|
using System.Text;
|
|
|
|
|
|
using System.Security.Cryptography;
|
|
|
|
|
|
using System.IO;
|
|
|
|
|
|
|
|
|
|
|
|
namespace MinecraftClient.Crypto.Streams
|
|
|
|
|
|
{
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// An encrypted stream using AES, used for encrypting network data on the fly using AES.
|
|
|
|
|
|
/// This is the regular AesStream class used with the regular .NET framework from Microsoft.
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
|
|
|
|
|
|
public class RegularAesStream : Stream, IAesStream
|
|
|
|
|
|
{
|
|
|
|
|
|
CryptoStream enc;
|
|
|
|
|
|
CryptoStream dec;
|
|
|
|
|
|
public RegularAesStream(Stream stream, byte[] key)
|
|
|
|
|
|
{
|
|
|
|
|
|
BaseStream = stream;
|
|
|
|
|
|
enc = new CryptoStream(stream, GenerateAES(key).CreateEncryptor(), CryptoStreamMode.Write);
|
|
|
|
|
|
dec = new CryptoStream(stream, GenerateAES(key).CreateDecryptor(), CryptoStreamMode.Read);
|
|
|
|
|
|
}
|
|
|
|
|
|
public System.IO.Stream BaseStream { get; set; }
|
|
|
|
|
|
|
|
|
|
|
|
public override bool CanRead
|
|
|
|
|
|
{
|
|
|
|
|
|
get { return true; }
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public override bool CanSeek
|
|
|
|
|
|
{
|
|
|
|
|
|
get { return false; }
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public override bool CanWrite
|
|
|
|
|
|
{
|
|
|
|
|
|
get { return true; }
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public override void Flush()
|
|
|
|
|
|
{
|
|
|
|
|
|
BaseStream.Flush();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public override long Length
|
|
|
|
|
|
{
|
|
|
|
|
|
get { throw new NotSupportedException(); }
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public override long Position
|
|
|
|
|
|
{
|
|
|
|
|
|
get
|
|
|
|
|
|
{
|
|
|
|
|
|
throw new NotSupportedException();
|
|
|
|
|
|
}
|
|
|
|
|
|
set
|
|
|
|
|
|
{
|
|
|
|
|
|
throw new NotSupportedException();
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public override int ReadByte()
|
|
|
|
|
|
{
|
|
|
|
|
|
return dec.ReadByte();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public override int Read(byte[] buffer, int offset, int count)
|
|
|
|
|
|
{
|
|
|
|
|
|
return dec.Read(buffer, offset, count);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public override long Seek(long offset, System.IO.SeekOrigin origin)
|
|
|
|
|
|
{
|
|
|
|
|
|
throw new NotSupportedException();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public override void SetLength(long value)
|
|
|
|
|
|
{
|
|
|
|
|
|
throw new NotSupportedException();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public override void WriteByte(byte b)
|
|
|
|
|
|
{
|
|
|
|
|
|
enc.WriteByte(b);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public override void Write(byte[] buffer, int offset, int count)
|
|
|
|
|
|
{
|
|
|
|
|
|
enc.Write(buffer, offset, count);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2022-08-15 23:55:44 +08:00
|
|
|
|
private static Aes GenerateAES(byte[] key)
|
2014-05-31 01:59:03 +02:00
|
|
|
|
{
|
2022-08-15 23:55:44 +08:00
|
|
|
|
Aes aes = Aes.Create();
|
|
|
|
|
|
aes.Mode = CipherMode.CFB;
|
|
|
|
|
|
aes.Padding = PaddingMode.None;
|
|
|
|
|
|
aes.KeySize = 128;
|
|
|
|
|
|
aes.FeedbackSize = 8;
|
|
|
|
|
|
aes.Key = key;
|
|
|
|
|
|
aes.IV = key;
|
|
|
|
|
|
return aes;
|
2014-05-31 01:59:03 +02:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|