public static long Long(long min, long max) { ulong range = (ulong)(max - min); ulong randomUlong; do { random.NextBytes(bufferLong); randomUlong = (ulong)BitConverter.ToInt64(bufferLong, 0); }while (randomUlong > ulong.MaxValue - ((ulong.MaxValue % range) + 1) % range); return((long)(randomUlong % range + (ulong)min)); }
public MsgKeepAlive() : base() { FastRandom fastRand = new FastRandom(); this.Payload = new byte[fastRand.Next(32, 256)]; fastRand.NextBytes(this.Payload); }
/// <summary> /// Agressively scan the algorithm for weakness /// </summary> /// <param name="EncryptCode"></param> /// <param name="DecryptCode"></param> /// <returns></returns> private static bool IsAlgorithmWeak(byte[] EncryptCode, byte[] DecryptCode, int Seed) { FastRandom rnd = new FastRandom(Seed); byte[] RandData = new byte[513]; rnd.NextBytes(RandData); byte[] Key = new byte[] { 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3, 4, 4, 4, 4, 4, 4, 4, 5, 5, 5, 5, 5 }; byte[] Salt = new byte[] { 5, 5, 5, 5, 5, 4, 4, 4, 4, 4, 4, 4, 3, 3, 3, 3, 3, 2, 2, 2, 2, 2, 2, 1, 1, 1, 1, 1, 1, 1 }; byte[] IV = new byte[] { 100, 132, 194, 103, 165, 222, 64, 110, 144, 217, 202, 129, 54, 97, 230, 25, 34, 58, 100, 79, 80, 124, 14, 61, 191, 5, 174, 94, 194, 10, 222, 215 }; WopEx wop = new WopEx(Key, Salt, IV, EncryptCode, DecryptCode, WopEncMode.Simple, 1, true); //test it 50 times if it's safe to use for (int x = 0; x < 50; x++) { byte[] crypted = new byte[RandData.Length]; Array.Copy(RandData, crypted, RandData.Length); wop.Encrypt(crypted, 0, crypted.Length); double Equals = 0; for (int i = 0; i < crypted.Length; i++) { if (RandData[i] == crypted[i]) { Equals++; } } wop.Decrypt(crypted, 0, crypted.Length); //check if decryption went successful if (RandData.Length != crypted.Length) { return(true); } for (int i = 0; i < RandData.Length; i++) { if (RandData[i] != crypted[i]) { //the decryption-routine failed return(true); } } double Pertentage = (Equals / (double)RandData.Length) * 100D; bool isWeak = Pertentage > 5; //if >5 % is the same as original it's a weak algorithm if (isWeak) { return(true); } } return(false); }
private byte[] KeyExtender(byte[] Input, int TargetLen) { int temp = 0xFF28423; for (int i = 0; i < Input.Length; i++) { temp += Input[i]; } int oldLen = Input.Length; FastRandom rnd = new FastRandom(temp); Array.Resize(ref Input, TargetLen); rnd.NextBytes(Input, oldLen, TargetLen); return(Input); }
public byte[] Encrypt(byte[] Data, int Offset, int Length) { lock (AES) { byte[] NewIV = new byte[16]; Frandom.NextBytes(NewIV); this.IV = NewIV; //mask the IV to make it harder to grab the IV while packet sniffing / MITM IvConfuser.Obfuscate(ref NewIV, 0); using (ICryptoTransform Encryptor = AES.CreateEncryptor()) { using (PayloadWriter pw = new PayloadWriter(new System.IO.MemoryStream())) { pw.WriteBytes(NewIV); pw.WriteBytes(Encryptor.TransformFinalBlock(Data, Offset, Length)); return(pw.ToByteArray()); } } } }
/// <summary> /// A dirty way to expand a key, need to find a more clean solution /// </summary> private byte[] ExpandKey(byte[] input) { if (input.Length > KEY_SIZE) { return(input); } int OrgLen = input.Length; Array.Resize(ref input, KEY_SIZE); FastRandom rnd = new FastRandom(BitConverter.ToInt32(input, 0)); const int BlockSize = 124; for (int i = OrgLen, j = 5; i < KEY_SIZE; i += BlockSize, j += 32) { int len = i + BlockSize < KEY_SIZE ? BlockSize : KEY_SIZE - i; rnd.NextBytes(input, i, input.Length); rnd = new FastRandom(BitConverter.ToInt32(input, j)); } return(input); }
private ClientConnection(Socket socket) : base(socket) { _rc4 = new Rc4(FastRandom.NextBytes(40)); ObjectHelper.Swap(ref inIncrement, ref outIncrement); ObjectHelper.Swap(ref inDecodingByte, ref outEncodingByte); }
private byte[] KeyExtender(byte[] Input, int TargetLen) { int temp = 0xFF28423; for (int i = 0; i < Input.Length; i++) temp += Input[i]; int oldLen = Input.Length; FastRandom rnd = new FastRandom(temp); Array.Resize(ref Input, TargetLen); rnd.NextBytes(Input, oldLen, TargetLen); return Input; }
private static void GetNextRandomInstruction(FastRandom rnd, ref InstructionInfo EncInstruction, ref InstructionInfo DecInstruction) { lock (RndInstLock) { Instruction[] InstructionList = new Instruction[] { //Instruction.BitLeft, //unstable do not use Instruction.Minus, Instruction.Plus, //Instruction.ForLoop_PlusMinus, //Instruction.RotateLeft_Big, //Instruction.RotateLeft_Small, Instruction.SwapBits, Instruction.XOR }; Instruction inst = InstructionList[rnd.Next(0, InstructionList.Length)]; switch (inst) { case Instruction.BitLeft: { int bitSize = rnd.Next(1, 3); //maybe needs to be higher ? EncInstruction = new InstructionInfo(inst, bitSize); DecInstruction = new InstructionInfo(Instruction.BitRight, bitSize); break; } case Instruction.Minus: { byte[] TempDate = new byte[32]; rnd.NextBytes(TempDate); EncInstruction = new InstructionInfo(inst, new BigInteger(TempDate)); DecInstruction = new InstructionInfo(Instruction.Plus, new BigInteger(TempDate)); break; } case Instruction.Plus: { byte[] TempDate = new byte[32]; rnd.NextBytes(TempDate); EncInstruction = new InstructionInfo(inst, new BigInteger(TempDate)); DecInstruction = new InstructionInfo(Instruction.Minus, new BigInteger(TempDate)); break; } case Instruction.ForLoop_PlusMinus: { int size = rnd.Next(); int size2 = rnd.Next(); int loops = rnd.Next(2, 255); EncInstruction = new InstructionInfo(inst, (uint)size, (uint)size2, loops); DecInstruction = new InstructionInfo(inst, (uint)size, (uint)size2, loops); break; } case Instruction.RotateLeft_Big: { byte bitSize = (byte)rnd.Next(1, 60); EncInstruction = new InstructionInfo(inst, (uint)bitSize); DecInstruction = new InstructionInfo(Instruction.RotateRight_Big, (uint)bitSize); break; } case Instruction.RotateLeft_Small: { byte bitSize = (byte)rnd.Next(1, 30); EncInstruction = new InstructionInfo(inst, (uint)bitSize); DecInstruction = new InstructionInfo(Instruction.RotateRight_Small, (uint)bitSize); break; } case Instruction.SwapBits: { EncInstruction = new InstructionInfo(inst, 0); DecInstruction = new InstructionInfo(inst, 0); break; } case Instruction.XOR: { byte[] TempDate = new byte[32]; rnd.NextBytes(TempDate); EncInstruction = new InstructionInfo(inst, new BigInteger(TempDate)); DecInstruction = new InstructionInfo(inst, new BigInteger(TempDate)); break; } default: { break; } } } }
public static void TestRandomGeneratorRndXorshiftByte() { int randomValue = 0; Console.WriteLine("Test RndXorshift generator bytes: "); dh.StartWatch(); XorshiftUnrolled64 rndXorshift = new XorshiftUnrolled64(); byte[] buffer = new byte[1024 * 1024]; //GCHandle gchBuffer = GCHandle.Alloc(buffer, GCHandleType.Pinned); for (int i = 0; i < 20000; i++) { rndXorshift.NextBytes(buffer); /* * foreach (var value in buffer) * { * randomValue = value; * } */ } dh.StoptWatch(); Console.WriteLine(dh.GetMessage()); Console.WriteLine("Test Fast Random generator bytes: "); dh.StartWatch(); FastRandom fastRandom = new FastRandom(); byte[] buffer1 = new byte[1024 * 1024]; //GCHandle gchBuffer = GCHandle.Alloc(buffer, GCHandleType.Pinned); for (int i = 0; i < 20000; i++) { fastRandom.NextBytes(buffer1); /* * foreach (var value in buffer) * { * randomValue = value; * } */ } dh.StoptWatch(); Console.WriteLine(dh.GetMessage()); Console.WriteLine("Test Random generator bytes: "); Random random = new Random(); byte[] buffer2 = new byte[1024 * 1024]; //GCHandle gchBuffer = GCHandle.Alloc(buffer, GCHandleType.Pinned); for (int i = 0; i < 20000; i++) { random.NextBytes(buffer2); /* * foreach (var value in buffer) * { * randomValue = value; * } */ } dh.StoptWatch(); Console.WriteLine(dh.GetMessage()); }
/// <summary> /// A dirty way to expand a key, need to find a more clean solution /// </summary> private byte[] ExpandKey(byte[] input) { if (input.Length > KEY_SIZE) return input; int OrgLen = input.Length; Array.Resize(ref input, KEY_SIZE); FastRandom rnd = new FastRandom(BitConverter.ToInt32(input, 0)); const int BlockSize = 124; for (int i = OrgLen, j = 5; i < KEY_SIZE; i += BlockSize, j += 32) { int len = i + BlockSize < KEY_SIZE ? BlockSize : KEY_SIZE - i; rnd.NextBytes(input, i, input.Length); rnd = new FastRandom(BitConverter.ToInt32(input, j)); } return input; }
/// <summary> /// Agressively scan the algorithm for weakness /// </summary> /// <param name="EncryptCode"></param> /// <param name="DecryptCode"></param> /// <returns></returns> private static bool IsAlgorithmWeak(byte[] EncryptCode, byte[] DecryptCode, int Seed) { FastRandom rnd = new FastRandom(Seed); byte[] RandData = new byte[513]; rnd.NextBytes(RandData); byte[] Key = new byte[] { 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3, 4, 4, 4, 4, 4, 4, 4, 5, 5, 5, 5, 5 }; byte[] Salt = new byte[] { 5, 5, 5, 5, 5, 4, 4, 4, 4, 4, 4, 4, 3, 3, 3, 3, 3, 2, 2, 2, 2, 2, 2, 1, 1, 1, 1, 1, 1, 1 }; byte[] IV = new byte[] { 100, 132, 194, 103, 165, 222, 64, 110, 144, 217, 202, 129, 54, 97, 230, 25, 34, 58, 100, 79, 80, 124, 14, 61, 191, 5, 174, 94, 194, 10, 222, 215 }; WopEx wop = new WopEx(Key, Salt, IV, EncryptCode, DecryptCode, WopEncMode.Simple, 1, true); //test it 50 times if it's safe to use for (int x = 0; x < 50; x++) { byte[] crypted = new byte[RandData.Length]; Array.Copy(RandData, crypted, RandData.Length); wop.Encrypt(crypted, 0, crypted.Length); double Equals = 0; for (int i = 0; i < crypted.Length; i++) { if (RandData[i] == crypted[i]) { Equals++; } } wop.Decrypt(crypted, 0, crypted.Length); //check if decryption went successful if (RandData.Length != crypted.Length) return true; for (int i = 0; i < RandData.Length; i++) { if (RandData[i] != crypted[i]) { //the decryption-routine failed return true; } } double Pertentage = (Equals / (double)RandData.Length) * 100D; bool isWeak = Pertentage > 5; //if >5 % is the same as original it's a weak algorithm if (isWeak) return true; } return false; }