public IContentResponse HandlePasswordSet(NameValueCollection headers, Stream inputStream)
        {
            INameValueStore store = new RegistryStorage(Settings.RegistryPath);

            int contentLen = int.Parse(headers["Content-Length"]);

            if (contentLen == 0)
            {
                using (RSAPrivateKey _temporaryKey = new RSAPrivateKey(2048))
                {
                    store.Write("Transfers", "temp-key", Convert.ToBase64String(_temporaryKey.ToArray()));
                    return(new DynamicResponse("application/public-key", _temporaryKey.PublicKey.ToArray()));
                }
            }

            string tempkey;

            if (contentLen <= 2048 && store.Read("Transfers", "temp-key", out tempkey))
            {
                byte[] bytes = IOStream.Read(inputStream, contentLen);
                using (RSAPrivateKey _temporaryKey = RSAPrivateKey.FromBytes(Convert.FromBase64String(tempkey)))
                    bytes = _temporaryKey.Decrypt(bytes);

                _content.KeyPair.SetServerPassword(bytes);
            }
            return(DynamicResponse.Empty);
        }
예제 #2
0
        public void TestPKICertificate()
        {
            byte[] rawdata = new byte[8001];
            new Random().NextBytes(rawdata);

            byte[] cypher;
            using (RSAPublicKey publicKey = new RSAPublicKey(TestCertPublicKey()))
                cypher = publicKey.Encrypt(rawdata);

            using (RSAPrivateKey privateKey = new RSAPrivateKey(TestCertPrivateKey()))
                Assert.AreEqual(rawdata, privateKey.Decrypt(cypher));
        }
예제 #3
0
        private void buttonDecrypt_Click(object sender, EventArgs e)
        {
            if (textBoxDecryptText.Text.Length == 0)
            {
                MessageBox.Show("Введите текст зашифрованного сообщения!", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }

            if (privateKey == null)
            {
                MessageBox.Show("Выберете закрытый ключ!", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }

            if (textBoxEDecryptPassphrase.Text.Length == 0)
            {
                MessageBox.Show("Введите парольную фразу!", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }

            try
            {
                privateKey.DecryptByPassphrase(textBoxEDecryptPassphrase.Text);
            }
            catch (Exception)
            {
                MessageBox.Show("Неверная парольная фраза!", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }

            string decryption = privateKey.Decrypt(textBoxDecryptText.Text);

            textBoxDecryptMessage.Text = decryption;

            textBoxDecryptText.Text        = "";
            textBoxEDecryptPassphrase.Text = "";
            labelDecryptPrivateKey.Text    = "";
            privateKey = null;
        }