Пример #1
0
        public void EncryptDecrypt_ShouldRoundTrip()
        {
            var input = new byte[] { 0, 1, 5, 30, 244, 255, 193 };

            var rsa = new RSACipher(new DefaultRandomGenerator());

            rsa.Init(new RSAPrivateKeyParameter(Key));

            var encryptOutput = new byte[rsa.KeySize];
            var decryptOutput = new byte[input.Length];

            rsa.Encrypt(input, 0, encryptOutput, 0, input.Length);
            rsa.Decrypt(encryptOutput, 0, decryptOutput, 0, encryptOutput.Length);

            Assert.Equal(input, decryptOutput);
        }
Пример #2
0
        private byte[] ReadMessage(byte[] body)
        {
            if (_certificateConfig.Certificate is null)
            {
                throw new InvalidOperationException("Certificate is not initialized");
            }

            var length = EndianBitConverter.Big.ToUInt16(body, 0);

            SecurityAssert.Assert(body.Length == length + 2);

            var key = (RSAPrivateKey)_certificateManager.GetPrivateKey(_certificateConfig.Certificate.SubjectPublicKey);
            var rsa = new RSACipher(_random);

            rsa.Init(new RSAPrivateKeyParameter(key));

            var preMasterSecret = new byte[48];

            rsa.Decrypt(body, 2, preMasterSecret, 0, length);
            SecurityAssert.Assert(preMasterSecret[0] == _versionConfig.Version.Major);
            SecurityAssert.Assert(preMasterSecret[1] == _versionConfig.Version.Minor);

            return(preMasterSecret);
        }