示例#1
0
        public void TestXmlImportExport()
        {
            RSAParameters @private = RsaStatic.GenerateKeyPairParams();
            RSAParameters @public  = RsaStatic.ExtractPublicKey(@private);

            string privateXml = @private.ExportXmlKey();
            string publicXml  = @public.ExportXmlKey();

            RSAParameters privateImport = new RSAParameters().ImportXmlKey(privateXml);
            RSAParameters publicImport  = new RSAParameters().ImportXmlKey(publicXml);

            CollectionAssert.AreEqual(@private.Modulus, privateImport.Modulus);
            CollectionAssert.AreEqual(@private.Exponent, privateImport.Exponent);
            CollectionAssert.AreEqual(@private.P, privateImport.P);
            CollectionAssert.AreEqual(@private.Q, privateImport.Q);
            CollectionAssert.AreEqual(@private.DP, privateImport.DP);
            CollectionAssert.AreEqual(@private.DQ, privateImport.DQ);
            CollectionAssert.AreEqual(@private.InverseQ, privateImport.InverseQ);
            CollectionAssert.AreEqual(@private.D, privateImport.D);

            CollectionAssert.AreEqual(@public.Modulus, publicImport.Modulus);
            CollectionAssert.AreEqual(@public.Exponent, publicImport.Exponent);
            Assert.IsNull(publicImport.P);
            Assert.IsNull(publicImport.Q);
            Assert.IsNull(publicImport.DP);
            Assert.IsNull(publicImport.DQ);
            Assert.IsNull(publicImport.InverseQ);
            Assert.IsNull(publicImport.D);
        }
示例#2
0
        private async void btnRsaDecrypt_Click(object sender, EventArgs e)
        {
            byte[] ct = Util.GetBytes(tbRsaCiphertext.Text);
            byte[] pt = await Task.Run(() => RsaStatic.Decrypt(ct, tbRsaPrivateKey.Text));

            tbRsaPlaintext.Text  = Encoding.UTF8.GetString(pt);
            tbRsaCiphertext.Text = "";
        }
示例#3
0
 private async void btnRsaExtract_Click(object sender, EventArgs e)
 {
     if (string.IsNullOrWhiteSpace(tbRsaPrivateKey.Text))
     {
         tbRsaPrivateKey.Text = await Task.Run(() => RsaStatic.GenerateKeyPairXml());
     }
     tbRsaPublicKey.Text = RsaStatic.ExtractPublicKey(tbRsaPrivateKey.Text);
 }
示例#4
0
        public void TestXmlKeyGeneration()
        {
            string @private = RsaStatic.GenerateKeyPairXml();
            string @public  = RsaStatic.ExtractPublicKey(@private);

            Assert.IsNotNull(@private);
            Assert.IsNotNull(@public);
            Assert.AreNotEqual(@private, @public, true);
            Assert.AreNotEqual(@private.Length, @public.Length);
        }
示例#5
0
 public async Task TestThrowing()
 {
     SocketSettings settings = new SocketSettings()
     {
         CatchApplicationExceptions = false, RsaKey = RsaStatic.GenerateKeyPairParams()
     };
     IVSLCallback callback = new FakeCallback();
     FakeSocket   socket   = new FakeSocket(settings, callback);
     await Assert.ThrowsExceptionAsync <InvalidOperationException>(() => socket.OnPacketReceived(0, null));
 }
示例#6
0
        public void TestCryptBlock()
        {
            RSAParameters @private = RsaStatic.GenerateKeyPairParams();
            RSAParameters @public  = RsaStatic.ExtractPublicKey(@private);

            byte[] plaintext  = Encoding.UTF8.GetBytes("Some test for cryptographic operations");
            byte[] ciphertext = RsaStatic.EncryptBlock(plaintext, @public);
            byte[] decrypted  = RsaStatic.DecryptBlock(ciphertext, @private);

            CollectionAssert.AreEqual(plaintext, decrypted);
        }
示例#7
0
        public async Task TestCatching()
        {
            SocketSettings settings = new SocketSettings()
            {
                CatchApplicationExceptions = true, RsaKey = RsaStatic.GenerateKeyPairParams()
            };
            IVSLCallback callback = new FakeCallback();
            FakeSocket   socket   = new FakeSocket(settings, callback);

            Assert.IsFalse(await socket.OnPacketReceived(0, null));
        }
示例#8
0
        public void TestCryptArbitraryLength()
        {
            RSAParameters @private = RsaStatic.GenerateKeyPairParams();
            RSAParameters @public  = RsaStatic.ExtractPublicKey(@private);

            byte[] plaintext = new byte[734]; // exceed the maximum of 214 bytes
            new Random().NextBytes(plaintext);
            byte[] ciphertext = RsaStatic.Encrypt(plaintext, @public);
            byte[] decrypted  = RsaStatic.Decrypt(ciphertext, @private);

            CollectionAssert.AreEqual(plaintext, decrypted);
        }
示例#9
0
        private async Task <bool> ReceivePacketAsync_RSA_2048_OAEP()
        {
            CryptoAlgorithm alg   = CryptoAlgorithm.RSA_2048_OAEP;
            int             index = 1;

            byte[] ciphertext = new byte[256];
            if (!await channel.ReceiveAsync(ciphertext, 0, 256))
            {
                return(false);
            }
            byte[] plaintext;
            try
            {
                plaintext = RsaStatic.DecryptBlock(ciphertext, rsaKey);
            }
            catch (CryptographicException ex)
            {
                exceptionHandler.CloseConnection(ex);
                return(false);
            }
            byte id = plaintext[0]; // index = 1

            if (!AssertInternal(id, alg) || !handler.ValidatePacket(id, alg, out PacketRule rule))
            {
                return(false);
            }

            uint length;

            if (rule.Packet.ConstantLength.HasValue)
            {
                length = rule.Packet.ConstantLength.Value;
            }
            else
            {
                length = BitConverter.ToUInt32(plaintext, index);
                index += 4;
                if (!AssertSize(209, (int)length))
                {
                    return(false); // 214 - 1 (id) - 4 (uint) => 209
                }
            }
            if (!AssertAvailable(plaintext.Length - index, (int)length))
            {
                return(false);
            }
            parent.Log($"Received internal RSA packet: ID={id} Length={length}");
            return(await handler.HandleInternalPacketAsync(rule, plaintext.TakeAt(index, (int)length)));
        }
示例#10
0
        public void TestRsaParamsGeneration()
        {
            RSAParameters @private = RsaStatic.GenerateKeyPairParams();
            RSAParameters @public  = RsaStatic.ExtractPublicKey(@private);

            Assert.IsNotNull(@private.Modulus);
            Assert.IsNotNull(@private.Exponent);
            Assert.IsNotNull(@private.P);
            Assert.IsNotNull(@private.Q);
            Assert.IsNotNull(@private.DP);
            Assert.IsNotNull(@private.DQ);
            Assert.IsNotNull(@private.InverseQ);
            Assert.IsNotNull(@private.D);

            Assert.IsNotNull(@public.Modulus);
            Assert.IsNotNull(@public.Exponent);
            Assert.IsNull(@public.P);
            Assert.IsNull(@public.Q);
            Assert.IsNull(@public.DP);
            Assert.IsNull(@public.DQ);
            Assert.IsNull(@public.InverseQ);
            Assert.IsNull(@public.D);
        }
示例#11
0
 private async void btnRsaGenerate_Click(object sender, EventArgs e)
 {
     tbRsaPrivateKey.Text = await Task.Run(() => RsaStatic.GenerateKeyPairXml());
 }