mirror of
https://github.com/MCCTeam/Minecraft-Console-Client
synced 2025-10-14 21:22:49 +00:00
Remove Java dependency for Encryption Key Request
Encryption Key Request is now fully processed using .NET RSA capabilities -> Good news, that was the last step for removing IKVM dependencies! -> So now MCC does not need to use any external library. Yay! :) -> MCC should now be compatible with Mono (Mac, Linux). + Removed no longed needed DLL files (9 MBytes!) + State MC 1.7.5 as a supported version. + Update version number to MCC 1.7.2
This commit is contained in:
parent
01ba55527d
commit
58d7b0734f
8 changed files with 140 additions and 94 deletions
|
|
@ -3,93 +3,149 @@ using System.Collections.Generic;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
using System.Text;
|
using System.Text;
|
||||||
using System.Security.Cryptography;
|
using System.Security.Cryptography;
|
||||||
using java.security;
|
|
||||||
using java.security.spec;
|
|
||||||
using javax.crypto;
|
|
||||||
using javax.crypto.spec;
|
|
||||||
|
|
||||||
namespace MinecraftClient
|
namespace MinecraftClient
|
||||||
{
|
{
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Cryptographic functions ported from Minecraft Source Code (Java). Decompiled with MCP. Copy, paste, little adjustements.
|
/// Methods for handling all the crypto stuff: RSA (Encryption Key Request), AES (Encrypted Stream), SHA-1 (Server Hash).
|
||||||
/// </summary>
|
/// </summary>
|
||||||
|
|
||||||
public class Crypto
|
public class Crypto
|
||||||
{
|
{
|
||||||
public static PublicKey GenerateRSAPublicKey(byte[] key)
|
/// <summary>
|
||||||
{
|
/// Get a cryptographic service for encrypting data using the server's RSA public key
|
||||||
X509EncodedKeySpec localX509EncodedKeySpec = new X509EncodedKeySpec(key);
|
/// </summary>
|
||||||
KeyFactory localKeyFactory = KeyFactory.getInstance("RSA");
|
/// <param name="key">Byte array containing the encoded key</param>
|
||||||
return localKeyFactory.generatePublic(localX509EncodedKeySpec);
|
/// <returns>Returns the corresponding RSA Crypto Service</returns>
|
||||||
}
|
|
||||||
|
|
||||||
public static SecretKey GenerateAESPrivateKey()
|
public static RSACryptoServiceProvider DecodeRSAPublicKey(byte[] x509key)
|
||||||
{
|
{
|
||||||
AesManaged aes = new AesManaged();
|
/* Code from StackOverflow no. 18091460 */
|
||||||
aes.KeySize = 128; aes.GenerateKey();
|
|
||||||
return new SecretKeySpec(aes.Key, "AES");
|
|
||||||
}
|
|
||||||
|
|
||||||
public static byte[] Encrypt(Key par0Key, byte[] par1ArrayOfByte)
|
byte[] SeqOID = { 0x2A, 0x86, 0x48, 0x86, 0xF7, 0x0D, 0x01, 0x01, 0x01 };
|
||||||
{
|
|
||||||
return func_75885_a(1, par0Key, par1ArrayOfByte);
|
|
||||||
}
|
|
||||||
|
|
||||||
private static byte[] func_75885_a(int par0, Key par1Key, byte[] par2ArrayOfByte)
|
System.IO.MemoryStream ms = new System.IO.MemoryStream(x509key);
|
||||||
{
|
System.IO.BinaryReader reader = new System.IO.BinaryReader(ms);
|
||||||
try
|
|
||||||
{
|
|
||||||
return cypherencrypt(par0, par1Key.getAlgorithm(), par1Key).doFinal(par2ArrayOfByte);
|
|
||||||
}
|
|
||||||
catch (IllegalBlockSizeException var4)
|
|
||||||
{
|
|
||||||
var4.printStackTrace();
|
|
||||||
}
|
|
||||||
catch (BadPaddingException var5)
|
|
||||||
{
|
|
||||||
var5.printStackTrace();
|
|
||||||
}
|
|
||||||
|
|
||||||
Console.Error.WriteLine("Cipher data failed!");
|
if (reader.ReadByte() == 0x30)
|
||||||
|
ReadASNLength(reader); //skip the size
|
||||||
|
else
|
||||||
return null;
|
return null;
|
||||||
}
|
|
||||||
private static Cipher cypherencrypt(int par0, String par1Str, Key par2Key)
|
int identifierSize = 0; //total length of Object Identifier section
|
||||||
|
if (reader.ReadByte() == 0x30)
|
||||||
|
identifierSize = ReadASNLength(reader);
|
||||||
|
else
|
||||||
|
return null;
|
||||||
|
|
||||||
|
if (reader.ReadByte() == 0x06) //is the next element an object identifier?
|
||||||
{
|
{
|
||||||
try
|
int oidLength = ReadASNLength(reader);
|
||||||
{
|
byte[] oidBytes = new byte[oidLength];
|
||||||
Cipher var3 = Cipher.getInstance(par1Str);
|
reader.Read(oidBytes, 0, oidBytes.Length);
|
||||||
var3.init(par0, par2Key);
|
if (oidBytes.SequenceEqual(SeqOID) == false) //is the object identifier rsaEncryption PKCS#1?
|
||||||
return var3;
|
return null;
|
||||||
}
|
|
||||||
catch (InvalidKeyException var4)
|
int remainingBytes = identifierSize - 2 - oidBytes.Length;
|
||||||
{
|
reader.ReadBytes(remainingBytes);
|
||||||
var4.printStackTrace();
|
|
||||||
}
|
|
||||||
catch (NoSuchAlgorithmException var5)
|
|
||||||
{
|
|
||||||
var5.printStackTrace();
|
|
||||||
}
|
|
||||||
catch (NoSuchPaddingException var6)
|
|
||||||
{
|
|
||||||
var6.printStackTrace();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
Console.Error.WriteLine("Cipher creation failed!");
|
if (reader.ReadByte() == 0x03) //is the next element a bit string?
|
||||||
|
{
|
||||||
|
ReadASNLength(reader); //skip the size
|
||||||
|
reader.ReadByte(); //skip unused bits indicator
|
||||||
|
if (reader.ReadByte() == 0x30)
|
||||||
|
{
|
||||||
|
ReadASNLength(reader); //skip the size
|
||||||
|
if (reader.ReadByte() == 0x02) //is it an integer?
|
||||||
|
{
|
||||||
|
int modulusSize = ReadASNLength(reader);
|
||||||
|
byte[] modulus = new byte[modulusSize];
|
||||||
|
reader.Read(modulus, 0, modulus.Length);
|
||||||
|
if (modulus[0] == 0x00) //strip off the first byte if it's 0
|
||||||
|
{
|
||||||
|
byte[] tempModulus = new byte[modulus.Length - 1];
|
||||||
|
Array.Copy(modulus, 1, tempModulus, 0, modulus.Length - 1);
|
||||||
|
modulus = tempModulus;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (reader.ReadByte() == 0x02) //is it an integer?
|
||||||
|
{
|
||||||
|
int exponentSize = ReadASNLength(reader);
|
||||||
|
byte[] exponent = new byte[exponentSize];
|
||||||
|
reader.Read(exponent, 0, exponent.Length);
|
||||||
|
|
||||||
|
RSACryptoServiceProvider RSA = new RSACryptoServiceProvider();
|
||||||
|
RSAParameters RSAKeyInfo = new RSAParameters();
|
||||||
|
RSAKeyInfo.Modulus = modulus;
|
||||||
|
RSAKeyInfo.Exponent = exponent;
|
||||||
|
RSA.ImportParameters(RSAKeyInfo);
|
||||||
|
return RSA;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Server Hash Computation */
|
/// <summary>
|
||||||
|
/// Subfunction for decrypting ASN.1 (x509) RSA certificate data fields lengths
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="reader">StreamReader containing the stream to decode</param>
|
||||||
|
/// <returns>Return the read length</returns>
|
||||||
|
|
||||||
public static string getServerHash(String toencode, PublicKey par1PublicKey, SecretKey par2SecretKey)
|
private static int ReadASNLength(System.IO.BinaryReader reader)
|
||||||
{
|
{
|
||||||
byte[] hash = digest(new byte[][] { Encoding.GetEncoding("iso-8859-1").GetBytes(toencode), par2SecretKey.getEncoded(), par1PublicKey.getEncoded() });
|
//Note: this method only reads lengths up to 4 bytes long as
|
||||||
|
//this is satisfactory for the majority of situations.
|
||||||
|
int length = reader.ReadByte();
|
||||||
|
if ((length & 0x00000080) == 0x00000080) //is the length greater than 1 byte
|
||||||
|
{
|
||||||
|
int count = length & 0x0000000f;
|
||||||
|
byte[] lengthBytes = new byte[4];
|
||||||
|
reader.Read(lengthBytes, 4 - count, count);
|
||||||
|
Array.Reverse(lengthBytes); //
|
||||||
|
length = BitConverter.ToInt32(lengthBytes, 0);
|
||||||
|
}
|
||||||
|
return length;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Generate a new random AES key for symmetric encryption
|
||||||
|
/// </summary>
|
||||||
|
/// <returns>Returns a byte array containing the key</returns>
|
||||||
|
|
||||||
|
public static byte[] GenerateAESPrivateKey()
|
||||||
|
{
|
||||||
|
AesManaged AES = new AesManaged();
|
||||||
|
AES.KeySize = 128; AES.GenerateKey();
|
||||||
|
return AES.Key;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Get a SHA-1 hash for online-mode session checking
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="serverID">Server ID hash</param>
|
||||||
|
/// <param name="PublicKey">Server's RSA key</param>
|
||||||
|
/// <param name="SecretKey">Secret key chosen by the client</param>
|
||||||
|
/// <returns>Returns the corresponding SHA-1 hex hash</returns>
|
||||||
|
|
||||||
|
public static string getServerHash(string serverID, byte[] PublicKey, byte[] SecretKey)
|
||||||
|
{
|
||||||
|
byte[] hash = digest(new byte[][] { Encoding.GetEncoding("iso-8859-1").GetBytes(serverID), SecretKey, PublicKey });
|
||||||
bool negative = (hash[0] & 0x80) == 0x80;
|
bool negative = (hash[0] & 0x80) == 0x80;
|
||||||
if (negative) { hash = TwosComplimentLittleEndian(hash); }
|
if (negative) { hash = TwosComplementLittleEndian(hash); }
|
||||||
string result = GetHexString(hash).TrimStart('0');
|
string result = GetHexString(hash).TrimStart('0');
|
||||||
if (negative) { result = "-" + result; }
|
if (negative) { result = "-" + result; }
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Generate a SHA-1 hash using several byte arrays
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="tohash">array of byte arrays to hash</param>
|
||||||
|
/// <returns>Returns the hashed data</returns>
|
||||||
|
|
||||||
private static byte[] digest(byte[][] tohash)
|
private static byte[] digest(byte[][] tohash)
|
||||||
{
|
{
|
||||||
SHA1CryptoServiceProvider sha1 = new SHA1CryptoServiceProvider();
|
SHA1CryptoServiceProvider sha1 = new SHA1CryptoServiceProvider();
|
||||||
|
|
@ -99,6 +155,12 @@ namespace MinecraftClient
|
||||||
return sha1.Hash;
|
return sha1.Hash;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Converts a byte array to its hex string representation
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="p">Byte array to convert</param>
|
||||||
|
/// <returns>Returns the string representation</returns>
|
||||||
|
|
||||||
private static string GetHexString(byte[] p)
|
private static string GetHexString(byte[] p)
|
||||||
{
|
{
|
||||||
string result = string.Empty;
|
string result = string.Empty;
|
||||||
|
|
@ -107,7 +169,13 @@ namespace MinecraftClient
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
private static byte[] TwosComplimentLittleEndian(byte[] p)
|
/// <summary>
|
||||||
|
/// Compute the two's complement of a little endian byte array
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="p">Byte array to compute</param>
|
||||||
|
/// <returns>Returns the corresponding two's complement</returns>
|
||||||
|
|
||||||
|
private static byte[] TwosComplementLittleEndian(byte[] p)
|
||||||
{
|
{
|
||||||
int i;
|
int i;
|
||||||
bool carry = true;
|
bool carry = true;
|
||||||
|
|
@ -123,15 +191,8 @@ namespace MinecraftClient
|
||||||
return p;
|
return p;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Encrypted Stream between client and server */
|
|
||||||
|
|
||||||
public static AesStream SwitchToAesMode(System.IO.Stream stream, Key key)
|
|
||||||
{
|
|
||||||
return new AesStream(stream, key.getEncoded());
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// An encrypted stream using AES
|
/// An encrypted stream using AES, used for encrypting network data on the fly using AES.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
|
|
||||||
public class AesStream : System.IO.Stream
|
public class AesStream : System.IO.Stream
|
||||||
|
|
|
||||||
|
|
@ -60,18 +60,6 @@
|
||||||
</PropertyGroup>
|
</PropertyGroup>
|
||||||
<PropertyGroup />
|
<PropertyGroup />
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<Reference Include="IKVM.OpenJDK.Core, Version=7.0.4335.0, Culture=neutral, PublicKeyToken=13235d27fcbfff58, processorArchitecture=MSIL">
|
|
||||||
<SpecificVersion>False</SpecificVersion>
|
|
||||||
<HintPath>.\IKVM.OpenJDK.Core.dll</HintPath>
|
|
||||||
</Reference>
|
|
||||||
<Reference Include="IKVM.OpenJDK.Security, Version=7.0.4335.0, Culture=neutral, PublicKeyToken=13235d27fcbfff58, processorArchitecture=MSIL">
|
|
||||||
<SpecificVersion>False</SpecificVersion>
|
|
||||||
<HintPath>.\IKVM.OpenJDK.Security.dll</HintPath>
|
|
||||||
</Reference>
|
|
||||||
<Reference Include="IKVM.OpenJDK.Util, Version=7.0.4335.0, Culture=neutral, PublicKeyToken=13235d27fcbfff58, processorArchitecture=MSIL">
|
|
||||||
<SpecificVersion>False</SpecificVersion>
|
|
||||||
<HintPath>.\IKVM.OpenJDK.Util.dll</HintPath>
|
|
||||||
</Reference>
|
|
||||||
<Reference Include="System" />
|
<Reference Include="System" />
|
||||||
<Reference Include="System.Core" />
|
<Reference Include="System.Core" />
|
||||||
<Reference Include="System.Windows.Forms" />
|
<Reference Include="System.Windows.Forms" />
|
||||||
|
|
@ -116,10 +104,6 @@
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<Content Include="resources\appicon.ico" />
|
<Content Include="resources\appicon.ico" />
|
||||||
<Content Include="lib\IKVM.OpenJDK.Core.dll" />
|
|
||||||
<Content Include="lib\IKVM.OpenJDK.Security.dll" />
|
|
||||||
<Content Include="lib\IKVM.OpenJDK.Util.dll" />
|
|
||||||
<Content Include="lib\IKVM.Runtime.dll" />
|
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
|
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
|
||||||
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
|
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
|
||||||
|
|
|
||||||
|
|
@ -462,11 +462,9 @@ namespace MinecraftClient
|
||||||
else if (pid == 0x01) //Encryption request
|
else if (pid == 0x01) //Encryption request
|
||||||
{
|
{
|
||||||
string serverID = readNextString();
|
string serverID = readNextString();
|
||||||
byte[] Serverkey_RAW = readNextByteArray();
|
byte[] Serverkey = readNextByteArray();
|
||||||
byte[] token = readNextByteArray();
|
byte[] token = readNextByteArray();
|
||||||
var PublicServerkey = Crypto.GenerateRSAPublicKey(Serverkey_RAW);
|
return StartEncryption(uuid, sessionID, token, serverID, Serverkey);
|
||||||
var SecretKey = Crypto.GenerateAESPrivateKey();
|
|
||||||
return StartEncryption(uuid, sessionID, token, serverID, PublicServerkey, SecretKey);
|
|
||||||
}
|
}
|
||||||
else if (pid == 0x02) //Login successfull
|
else if (pid == 0x02) //Login successfull
|
||||||
{
|
{
|
||||||
|
|
@ -477,8 +475,11 @@ namespace MinecraftClient
|
||||||
}
|
}
|
||||||
else return false;
|
else return false;
|
||||||
}
|
}
|
||||||
public bool StartEncryption(string uuid, string sessionID, byte[] token, string serverIDhash, java.security.PublicKey serverKey, javax.crypto.SecretKey secretKey)
|
public bool StartEncryption(string uuid, string sessionID, byte[] token, string serverIDhash, byte[] serverKey)
|
||||||
{
|
{
|
||||||
|
System.Security.Cryptography.RSACryptoServiceProvider RSAService = Crypto.DecodeRSAPublicKey(serverKey);
|
||||||
|
byte[] secretKey = Crypto.GenerateAESPrivateKey();
|
||||||
|
|
||||||
Console.ForegroundColor = ConsoleColor.DarkGray;
|
Console.ForegroundColor = ConsoleColor.DarkGray;
|
||||||
ConsoleIO.WriteLine("Crypto keys & hash generated.");
|
ConsoleIO.WriteLine("Crypto keys & hash generated.");
|
||||||
Console.ForegroundColor = ConsoleColor.Gray;
|
Console.ForegroundColor = ConsoleColor.Gray;
|
||||||
|
|
@ -493,8 +494,8 @@ namespace MinecraftClient
|
||||||
}
|
}
|
||||||
|
|
||||||
//Encrypt the data
|
//Encrypt the data
|
||||||
byte[] key_enc = Crypto.Encrypt(serverKey, secretKey.getEncoded());
|
byte[] key_enc = RSAService.Encrypt(secretKey, false);
|
||||||
byte[] token_enc = Crypto.Encrypt(serverKey, token);
|
byte[] token_enc = RSAService.Encrypt(token, false);
|
||||||
byte[] key_len = BitConverter.GetBytes((short)key_enc.Length); Array.Reverse(key_len);
|
byte[] key_len = BitConverter.GetBytes((short)key_enc.Length); Array.Reverse(key_len);
|
||||||
byte[] token_len = BitConverter.GetBytes((short)token_enc.Length); Array.Reverse(token_len);
|
byte[] token_len = BitConverter.GetBytes((short)token_enc.Length); Array.Reverse(token_len);
|
||||||
|
|
||||||
|
|
@ -505,7 +506,7 @@ namespace MinecraftClient
|
||||||
Send(encryption_response_tosend);
|
Send(encryption_response_tosend);
|
||||||
|
|
||||||
//Start client-side encryption
|
//Start client-side encryption
|
||||||
setEncryptedClient(Crypto.SwitchToAesMode(c.GetStream(), secretKey));
|
setEncryptedClient(new Crypto.AesStream(c.GetStream(), secretKey));
|
||||||
|
|
||||||
//Get the next packet
|
//Get the next packet
|
||||||
readNextVarInt(); //Skip Packet size (not needed)
|
readNextVarInt(); //Skip Packet size (not needed)
|
||||||
|
|
|
||||||
|
|
@ -15,7 +15,7 @@ namespace MinecraftClient
|
||||||
{
|
{
|
||||||
private static McTcpClient Client;
|
private static McTcpClient Client;
|
||||||
public static string[] startupargs;
|
public static string[] startupargs;
|
||||||
public const string Version = "1.7.1";
|
public const string Version = "1.7.2";
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// The main entry point of Minecraft Console Client
|
/// The main entry point of Minecraft Console Client
|
||||||
|
|
@ -23,7 +23,7 @@ namespace MinecraftClient
|
||||||
|
|
||||||
static void Main(string[] args)
|
static void Main(string[] args)
|
||||||
{
|
{
|
||||||
Console.WriteLine("Console Client for MC 1.7.2 to 1.7.4 - v" + Version + " - By ORelio & Contributors");
|
Console.WriteLine("Console Client for MC 1.7.2 to 1.7.5 - v" + Version + " - By ORelio & Contributors");
|
||||||
|
|
||||||
//Basic Input/Output ?
|
//Basic Input/Output ?
|
||||||
if (args.Length >= 1 && args[args.Length - 1] == "BasicIO")
|
if (args.Length >= 1 && args[args.Length - 1] == "BasicIO")
|
||||||
|
|
|
||||||
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Loading…
Add table
Add a link
Reference in a new issue