public void GenerateRandom()
        {
            IsaacRandom random = new IsaacRandom(Guid.NewGuid().ToString());

            for (int i = 0; i < Program.RNG_COUNT; i++)
            {
                random.NextInt();
            }
        }
Beispiel #2
0
        public static void WriteInputFilesToStub(CryptCommand command)
        {
            var seed            = Guid.NewGuid().ToString();
            var projectFilePath = Path.Combine(command.SrcStubOutput, ProjectFileName);
            var stubSrcFilePath = Path.Combine(command.SrcStubOutput, StubFileName);
            var csProjLines     = File.ReadAllLines(projectFilePath).ToList();

            csProjLines[csProjLines.Count - 1] = "";
            string stubSourceText    = File.ReadAllText(stubSrcFilePath);
            string inFilesStubString = "";

            stubSourceText = stubSourceText.Replace(DecryptionGuidVar, "\"" + seed + "\"");

            foreach (var inFile in command.InputFiles)
            {
                var cipher           = new IsaacRandom(seed);
                var extension        = Path.GetExtension(inFile);
                var outputFileName   = command.RandomiseOutputFileNames ? Guid.NewGuid().ToString() + extension : Path.GetFileName(inFile);
                var encryptionOutput = Path.Combine(command.SrcStubOutput, outputFileName);

                byte[] inputFile = File.ReadAllBytes(inFile);

                for (int i = 0; i < inputFile.Length; i++)
                {
                    inputFile[i] += cipher.NextByte();
                }
                File.WriteAllBytes(encryptionOutput, inputFile);
                csProjLines.Add(string.Format(ResourceText, outputFileName));

                inFilesStubString += "\"" + outputFileName + "\"";
                if (!command.InputFiles.Last().Equals(inFile))
                {
                    inFilesStubString += ", ";
                }
            }

            stubSourceText = stubSourceText.Replace(ResourceNameVar, inFilesStubString);
            stubSourceText = stubSourceText.Replace(SecondsDelayVar, command.DelayExecution.ToString());
            csProjLines.Add(ProjectEndText);

            File.WriteAllText(stubSrcFilePath, stubSourceText);
            File.WriteAllText(projectFilePath, string.Join('\n', csProjLines));
        }
        public void Test_Deterministic_Algorithm(uint sequenceSize)
        {
            var seed = CorrelationId.GenerateCorrelationId().ToString();

            uint[] sequence          = new uint[sequenceSize];
            uint[] duplicateSequence = new uint[sequenceSize];

            IsaacRandom cipher = new IsaacRandom(seed);

            for (int i = 0; i < sequenceSize; i++)
            {
                sequence[i] = cipher.NextInt();
            }

            IDeterministicRandom cipherClone = new IsaacRandom(seed);

            for (int i = 0; i < sequenceSize; i++)
            {
                duplicateSequence[i] = cipherClone.NextInt();
            }

            Assert.True(sequence.SequenceEqual(duplicateSequence));
        }
Beispiel #4
0
 public IsaacRandomPair(IsaacRandom encodingRandom, IsaacRandom decodingRandom)
 {
     this.EncodingRandom = encodingRandom;
     this.DecodingRandom = decodingRandom;
 }
Beispiel #5
0
        /// <summary>
        /// Decodes the payload state.
        /// </summary>
        /// <param name="ctx">The ctx.</param>
        /// <param name="buffer">The buffer.</param>
        /// <param name="output">The output.</param>
        private void DecodePayload(IChannelHandlerContext ctx, IByteBuffer buffer, List <object> output)
        {
            if (buffer.ReadableBytes >= _loginLength)
            {
                IByteBuffer payload = buffer.ReadBytes(_loginLength);
                var         version = 255 - payload.ReadByte();

                var release = payload.ReadShort();

                var memoryStatus = payload.ReadByte();
                if (memoryStatus != 0 && memoryStatus != 1)
                {
                    _logger.Information("Login memoryStatus ({0}) not in expected range of [0, 1].", memoryStatus);
                    WriteResponseCode(ctx, LoginStatus.StatusLoginServerRejectedSession);
                    return;
                }

                var lowMemory = memoryStatus == 1;

                var crcs = new int[Constants.ArchiveCount];
                for (var index = 0; index < Constants.ArchiveCount; index++)
                {
                    crcs[index] = payload.ReadInt();
                }

                var length = payload.ReadByte();
                if (length != _loginLength - 41)
                {
                    _logger.Information("Login packet unexpected length ({0})", length);
                    WriteResponseCode(ctx, LoginStatus.StatusLoginServerRejectedSession);
                    return;
                }

                /*
                 * var secureBytes = payload.ReadBytes(length);
                 * var value = new BigInteger(secureBytes.Array.Take(length).ToArray());
                 *
                 * RSA?
                 * value = BigInteger.ModPow(value, Constants.RSA_MODULUS, Constants.RSA_EXPONENT);
                 * var secureBuffer = Unpooled.WrappedBuffer(value.ToByteArray());
                 */


                var id = payload.ReadByte();
                if (id != 10)
                {
                    _logger.Information("Unable to read id from secure payload.");
                    WriteResponseCode(ctx, LoginStatus.StatusLoginServerRejectedSession);
                    return;
                }


                var clientSeed   = payload.ReadLong();
                var reportedSeed = payload.ReadLong();
                if (reportedSeed != _serverSeed)
                {
                    _logger.Information("Reported seed differed from server seed.");
                    WriteResponseCode(ctx, LoginStatus.StatusLoginServerRejectedSession);
                    return;
                }

                var uid           = payload.ReadInt();
                var username      = payload.ReadString();
                var password      = payload.ReadString();
                var socketAddress = (IPEndPoint)ctx.Channel.RemoteAddress;
                var hostAddress   = socketAddress.Address.ToString();

                var seed = new int[4];
                seed[0] = (int)(clientSeed >> 32);
                seed[1] = (int)clientSeed;
                seed[2] = (int)(_serverSeed >> 32);
                seed[3] = (int)_serverSeed;

                var decodingRandom = new IsaacRandom(seed);
                for (int index = 0; index < seed.Length; index++)
                {
                    seed[index] += 50;
                }

                var encodingRandom = new IsaacRandom(seed);

                var credentials = new PlayerCredentials
                {
                    Username        = username,
                    Password        = password,
                    EncodedUsername = _usernameHash,
                    Uid             = uid,
                    HostAddress     = hostAddress,
                };

                var randomPair = new IsaacRandomPair(encodingRandom, decodingRandom);
                var request    = new Rs2LoginRequest
                {
                    Credentials   = credentials,
                    RandomPair    = randomPair,
                    Reconnecting  = _reconnecting,
                    LowMemory     = lowMemory,
                    ReleaseNumber = release,
                    ArchiveCrcs   = crcs,
                    ClientVersion = version,
                    OnResult      = (loginTask) => WriteProcessorResponseAsync(loginTask, ctx, randomPair)
                };

                _loginProcessor.Enqueue(request);
            }
        }