Minecraft-Console-Client/MinecraftClient/Crypto/Streams/MonoAesStream.cs
ORelio c30d3025f7 Use BouncyCastle for handling AES on Mono Framework
Mono Framework does not handle CFB-8 AES encryption mode. So now MCC
will now use borrowed code from the BouncyCastle project for handling
AES when running on Mono framework, instead of using a dirty workaround
to try getting Mono encryption working. Regular .NET framework
encryption module will still be used when not running under Mono (eg on
Windows or using Wine)

Should hopefully fix all the issues encountered on Mono including #41
and finally achieve full compatibility of MCC with Mac and Linux.
2015-03-19 22:16:42 +01:00

110 lines
2.9 KiB
C#

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Security.Cryptography;
using System.IO;
using Org.BouncyCastle.Crypto;
using Org.BouncyCastle.Crypto.Parameters;
using Org.BouncyCastle.Crypto.Engines;
using Org.BouncyCastle.Crypto.Modes;
using Org.BouncyCastle.Crypto.IO;
namespace MinecraftClient.Crypto.Streams
{
/// <summary>
/// An encrypted stream using AES, used for encrypting network data on the fly using AES.
/// This is a mono-compatible adaptation which uses AES engine from the BouncyCastle project.
/// </summary>
public class MonoAesStream : Stream, IAesStream
{
IPaddingProvider pad;
CipherStream cstream;
public MonoAesStream(System.IO.Stream stream, byte[] key, IPaddingProvider provider)
{
BaseStream = stream;
BufferedBlockCipher enc = GenerateAES(key, true);
BufferedBlockCipher dec = GenerateAES(key, false);
cstream = new CipherStream(stream, dec, enc);
pad = provider;
}
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()
{
byte[] temp = new byte[1];
Read(temp, 0, 1);
return temp[0];
}
public override int Read(byte[] buffer, int offset, int count)
{
return cstream.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)
{
Write(new byte[] { b }, 0, 1);
}
public override void Write(byte[] buffer, int offset, int count)
{
cstream.Write(buffer, offset, count);
}
private BufferedBlockCipher GenerateAES(byte[] key, bool forEncryption)
{
BufferedBlockCipher cipher = new BufferedBlockCipher(new CfbBlockCipher(new AesFastEngine(), 8));
cipher.Init(forEncryption, new ParametersWithIV(new KeyParameter(key), key));
return cipher;
}
}
}