@fiercekilla
Старательно изучаю всё подряд

Ссылка на объект не указывает на экземпляр объекта.Что делать?

Ошибка вылезает в строке
byte[] bytesDecrypted = csp.Decrypt(encData, false);


Код:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Security.Cryptography;
using System.Security.Cryptography.X509Certificates;

namespace WindowsFormsApplication7
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

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

        private void button1_Click(object sender, EventArgs e)
        {
            X509Certificate2 x509 = new X509Certificate2(X509Certificate2.CreateFromCertFile(@textBox1.Text));
            using (RSACryptoServiceProvider csp = (RSACryptoServiceProvider)x509.PublicKey.Key)
            {
                byte[] data = Encoding.UTF8.GetBytes(richTextBox1.Text);
                byte[] encData = csp.Encrypt(data, false);
                richTextBox2.Text = Convert.ToBase64String(encData);
               
            }

        }

        private void button3_Click(object sender, EventArgs e)
        {
             X509Certificate2 x509 = new X509Certificate2(X509Certificate2.CreateFromCertFile(@textBox2.Text));
             byte[] encData = Convert.FromBase64String(richTextBox2.Text);
             
             using (RSACryptoServiceProvider csp = (RSACryptoServiceProvider)x509.PrivateKey)
             {
                 byte[] bytesDecrypted = csp.Decrypt(encData, false);
                 int len = bytesDecrypted.Length;
                 char[] val = new char[len];
                 val = Encoding.Unicode.GetChars(bytesDecrypted);
                 richTextBox3.Text = string.Join("", val);
                 //richTextBox3.Text = Encoding.UTF8.GetString(bytesDecrypted);
             }
        }

        private void button4_Click(object sender, EventArgs e)
        {
            if (openFileDialog2.ShowDialog() == DialogResult.OK)
            {
                textBox2.Text = openFileDialog2.FileName;
            }
        }
    }
}
  • Вопрос задан
  • 1076 просмотров
Пригласить эксперта
Ответы на вопрос 1
@fiercekilla Автор вопроса
Старательно изучаю всё подряд
Алексей Немиро:
Нашел null тут:
using (RSACryptoServiceProvider csp = (RSACryptoServiceProvider)x509.PrivateKey)

У св-ва PrivateKey, но почему там null не пойму,
Для шифровки использую pfx сертификат, для расшифровки cer
Ответ написан
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Войти через центр авторизации
Похожие вопросы