N
N
Nikita Kochetkov2016-01-14 03:44:48
C++ / C#
Nikita Kochetkov, 2016-01-14 03:44:48

Why doesn't the RSA C# private key change?

Regardless of which certificate with keys I import to it, the field containing the received key (richBox3) does not change.
the code:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Xml;
using System.Security.Cryptography;
using System.Windows.Forms;
using System.Security.Cryptography.X509Certificates;

namespace лаб4_вар_9
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {

            string OpenCertificate = @textBox3.Text;
            X509Certificate pubcert = new X509Certificate();
            pubcert.Import(OpenCertificate);
            string resultsTrue = pubcert.ToString(true);
            string resultsFalse = pubcert.ToString(false);

            string PrivatCertificate = @textBox2.Text;
            X509Certificate privcert = new X509Certificate();
            privcert.Import(PrivatCertificate);
            string resultsTrue2 = privcert.ToString(true);
            string resultsFalse2 = privcert.ToString(false);

            RSACryptoServiceProvider RsaKey;       
            CspParameters cp = new CspParameters();
            cp.KeyContainerName = "MyKeyContainerName";

            RsaKey = new RSACryptoServiceProvider(cp);// начинаем рaботу с систем провайдер


            label4.Text = label4.Text + RsaKey.KeySize.ToString();// показываем количество свободных байтов

            string publickey = resultsTrue;// показываем ключ 1
            string privatekey = RsaKey.ToXmlString(true);// показываем ключ 1

            richTextBox2.Text = publickey;  // выводим на экран 
            richTextBox1.Text = privatekey;

            UnicodeEncoding ByteConverter = new UnicodeEncoding();
            byte[] dataToEncrypt = ByteConverter.GetBytes(textBox1.Text);// символы переводим в байты 
            try
            {
                byte[] txtEnCrypt = RsaKey.Encrypt(dataToEncrypt, false);

                richTextBox4.Text = Convert.ToBase64String(txtEnCrypt);
            }
            catch (CryptographicException ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            CspParameters cp = new CspParameters();
            cp.KeyContainerName = "MyKeyContainerName";
            RSACryptoServiceProvider RsaKey = new RSACryptoServiceProvider(cp);
            string publickey = RsaKey.ToXmlString(false);
            richTextBox3.Text = publickey;  // выводим на экран 
            byte[] dataToDecrypt = Convert.FromBase64String(richTextBox4.Text);// преобразуеи из байтов в нормальный текст 
            
            try
            {
                byte[] TxtToDecrypt = RsaKey.Decrypt(dataToDecrypt, false);// разшифрование
                int len = TxtToDecrypt.Length;
                char[] val = new char[len];
                val = Encoding.Unicode.GetChars(TxtToDecrypt);
                richTextBox5.Text = string.Join("", val);
            }
            catch (CryptographicException ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void button3_Click(object sender, EventArgs e)
        {
            if (openFileDialog1.ShowDialog() == DialogResult.OK)
            {
               textBox2.Text = openFileDialog1.FileName;
                
            }
        }

        private void button4_Click(object sender, EventArgs e)
        {
            if (openFileDialog2.ShowDialog() == DialogResult.OK)
            {
                textBox3.Text = openFileDialog2.FileName;
            }
        }

Screen form
85529d1e51f54712a39d11d65e9bc131.png

Answer the question

In order to leave comments, you need to log in

1 answer(s)
G
glenean, 2016-01-14
@glenean

Look in the layout, in your code richTextBox3 is called richTextBox3, and in the picture you write richBox3,
you refer to the element which is either not in the layout, or you have not initialized it.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question