Пример #1
0
        static void Test1()
        {
            var rsa = new RSACryptoServiceProvider(2048);
            var pubKey = rsa.ExportCspBlob(false);
            var privateKey = rsa.ExportCspBlob(true);

            var b = new RSACrypto().Encrypt(pubKey, System.Text.Encoding.UTF8.GetBytes("hello."));
            var c = new RSACrypto().Decrypt(privateKey, b);
            string s = string.Empty;


            

            //var rsa1 = new RSACryptoServiceProvider(1024);
            //rsa1.ImportCspBlob(pubKey);
            //var secret = rsa1.Encrypt(System.Text.Encoding.UTF8.GetBytes("hello."), false);

            //var rsa2 = new RSACryptoServiceProvider(1024);
            //rsa2.ImportCspBlob(privateKey);
            //var plain = rsa2.Decrypt(secret, false);

        }
Пример #2
0
        public void EnableAllMessageFilterTest()
        {
            var client = new SimpleTcpClient();
            var identifier = Guid.NewGuid().ToByteArray();
            var bootstrap = new ServerBootstrap(EndPointDispatcher.GetRandomPort());
            Assert.IsTrue(bootstrap.StartUp(null, new TestModule()));
            var handler = client.GetFutureHandler(null, (a, b) =>
            {
                var message = b as DuplexMessage;
                Assert.AreEqual(MessageVersion.V1, message.Header.Version);
                Assert.AreEqual(MessageFilterType.Checksum, message.Header.FilterType & MessageFilterType.Checksum);
                Assert.AreEqual(MessageFilterType.Compression, message.Header.FilterType & MessageFilterType.Compression);
                Assert.AreEqual(SerializeMode.Json, message.Header.SerializeMode);
                Assert.AreEqual(MessageState.Success, message.Header.State);
                Assert.AreEqual(ErrorCode.NoError, message.Header.ErrorCode);
                Assert.AreEqual(identifier.ToHex(), message.Header.Identifier);
                var obj = message.GetContent<Entity>();
                Assert.AreEqual("a1", obj.Name);
                Assert.AreEqual(2, obj.Nest.Age);
                Assert.AreEqual(Desc, obj.Task);
            });
            bootstrap.Server.Handler = handler;
            Assert.IsTrue(client.Connect(bootstrap.EndPoint));
            var buffer = ByteBufferAllocator.Instance.Allocate(48);
            buffer.AutoExpand = true;
            var pubKey = ObjectHost.Host.Resolve<ICryptoKeyProvider>().GetPublicKey(null);
            var body = new RSACrypto().Encrypt(pubKey, new Json().Serialize(new Entity
            {
                Name = "a1",
                Nest = new Nest { Age = 2 },
                Task = Desc
            }).Zip());

            buffer.PutInt32(49 + body.Length);
            buffer.Put(1);
            buffer.PutInt16(1);
            buffer.Put(ErrorCode.NoError.ToByte());
            buffer.Put(MessageType.Command.ToByte());
            buffer.Put(identifier);
            buffer.Put(Guid.NewGuid().ToByteArray());
            buffer.Put((MessageFilterType.Checksum | MessageFilterType.Compression | MessageFilterType.Crypto).ToByte());
            buffer.Put(new byte[] { 0, 0 });
            buffer.Put(SerializeMode.Json.ToByte());
            buffer.Position = 0;
            var header = buffer.GetArray(45);
            buffer.Put(BitConverter.GetBytes(Crc32.CalculateDigest(header.Concat(body), (uint)0, (uint)(header.Length + body.Length))));
            buffer.Put(body);
            Assert.IsTrue(client.Send(buffer));
            Assert.AreEqual(1, handler.RcvCount);
            //send again
            Assert.IsTrue(client.Send(buffer));
            Assert.AreEqual(2, handler.RcvCount);
            client.Close();
        }