예제 #1
0
        public void TestEncryptionDecryption()
        {
            var input = new byte[] { 0, 1, 5, 30, 244, 255, 193 };

            var rsa = new RSA();
            rsa.Init(new PrivateKeyParameter(Key));

            var encrypted = rsa.Encrypt(input);
            var decrypted = rsa.Decrypt(encrypted, 0, encrypted.Length);

            CollectionAssert.AreEqual(input, decrypted);
        }
예제 #2
0
        public void TestSignVerifyInvalid()
        {
            var input = Encoding.UTF8.GetBytes("Hello World");

            var rsa = new RSA();
            rsa.Init(new PrivateKeyParameter(Key));

            var signature = rsa.Sign(input, new SHA1Digest());

            signature[signature.Length - 1] ^= 1;

            Assert.IsFalse(rsa.Verify(input, signature, new SHA1Digest()));
        }
예제 #3
0
        public void TestSignVerify()
        {
            var input = Encoding.UTF8.GetBytes("Hello World");

            var rsa = new RSA();
            rsa.Init(new PrivateKeyParameter(Key));

            var signature = rsa.Sign(input, new SHA1Digest());

            Assert.IsTrue(rsa.Verify(input, signature, new SHA1Digest()));
            Assert.AreEqual("54eab8c1837f4ded1122e1fbf47d0225188148a092e180e83b489aba1f1dc7b5" +
                            "241103ba8f136b393cf8c054a6a69e0c372453aa098e091a2dbe0310f0b653cb", HexConverter.ToHex(signature));
        }
예제 #4
0
        public HandshakeMessage ReadClientKeyExchange(byte[] body)
        {
            var length = EndianBitConverter.Big.ToUInt16(body, 0);
            SecurityAssert.SAssert(body.Length == length + 2);

            var key = state.Certificates.GetPrivateKey(state.Certificate.SubjectPublicKey);
            var rsa = new RSA();
            rsa.Init(new PrivateKeyParameter(key));

            var preMasterSecret = rsa.Decrypt(body, 2, length);
            SecurityAssert.SAssert(preMasterSecret.Length == 48);
            SecurityAssert.SAssert(preMasterSecret[0] == state.Version.Major && preMasterSecret[1] == state.Version.Minor);

            return new ClientKeyExchangeMessage.RSA(preMasterSecret);
        }