using System; using System.Collections.Generic; using System.Globalization; using System.Linq; using System.Numerics; using System.Text; namespace Chop.Game.Encryption { class Crypto { public RC4 RC4Decode; private BigInteger _prime; private BigInteger _publicKey; private BigInteger _privateKey; public void InitRc4(byte[] tKey) { RC4Decode = new RC4(); RC4Decode.Init(tKey); } public void InitDh(BigInteger p, BigInteger generator, string bigrand) { _prime = p; _privateKey = new BigInteger(bigrand, 16); _publicKey = generator.modPow(_privateKey, _prime); } public String GenerateSharedKey(string cKey) { return (new BigInteger(cKey, 10).modPow(_privateKey, _prime).ToString(16)).ToUpper(); } public String GetPublicKey() { return _publicKey.ToString(10); } public String GenerateRandomHexString(int len) { var result = ""; var rnd = new Random(); for (var i = 0; i < len; i++) { var rand = 1 + (int)(rnd.NextDouble() * 254); result += int.Parse(rand.ToString(), (NumberStyles)16); } return result; } public byte[] HextoBytes(String hexString) { if ((hexString.Length % 2) != 0) { hexString = ("0" + hexString); } var bytes = new byte[hexString.Length / 2]; var j = 0; for (var i = 0; i < bytes.Length; i++) { bytes[i] = (byte)int.Parse(hexString.Substring(j, j + 2), (NumberStyles)16); j += 2; } return bytes; } } }