private void DecryptFile()
        {
            byte[]     message = File.ReadAllBytes(file_textBox.Text);
            BigInteger key     = BigInteger.Parse(closedKey_textBox.Text);
            BigInteger r       = BigInteger.Parse(r_textBox.Text);

            byte[] result = new byte[message.Length / 2];
            try
            {
                result = RSAEncryptor.Decrypt(message, key, r);
            }
            catch (ArithmeticException ex)
            {
                MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }

            string resultText = "";

            foreach (var num in result)
            {
                resultText += num.ToString() + " ";
            }

            result_textBox.Text = resultText;
            if (saveFileDialog.ShowDialog() == DialogResult.OK)
            {
                File.WriteAllBytes(saveFileDialog.FileName, result);
            }
        }
Beispiel #2
0
        public void EncryptsAndDecryptsMessages(byte[] message, string pubKey, string privKey)
        {
            // Arrange
            var keyParser = new PemRSAKeyParser();
            var target    = new RSAEncryptor();

            keyParser.TryParsePublic(pubKey, out RSAParameters publicKey);
            keyParser.TryParsePrivate(privKey, out RSAParameters privateKey);

            // Act
            var encryptedMessage = target.Encrypt(message, publicKey);
            var decryptedMessage = target.Decrypt(encryptedMessage, privateKey);

            // Assert
            Assert.Equal(message, decryptedMessage);
        }