Hello everyone, I am making a program for encrypting / decrypting text When I tested, I tried small text in a couple of lines and it worked as it should, but then I tried the bulk text and after pressing the encryption button I threw out the "Crypto error" error through catch) and an unhandled exception ArgumentNullException
ArgumentNullException not processed
Unhandled exception of type "System.ArgumentNullException" in mscorlib.dll
Additional Information: Value cannot be null.
At the same time highlights the line in the code:textBox2.Text = Convert.ToBase64String(EncryptedData);
Here I am posting the full code of my Encryption button
private void button4_Click(object sender, EventArgs e) { byte[] data = new byte[1024]; RSACryptoServiceProvider rsa = new RSACryptoServiceProvider(); try { if (publickey.Length == 0) { _ispub_xml = false; MessageBox.Show("Неверный открытый ключ"); return; } else { rsa.FromXmlString(publickey); } } catch (Exception ex) { MessageBox.Show("Проблема с RSA \n" + ex.Message.ToString()); } try { data = Encoding.UTF8.GetBytes(textBox1.Text); } catch (Exception ss) { MessageBox.Show(ss.ToString()); return; } try { EncryptedData = rsa.Encrypt(data, false); } catch (CryptographicException ex) { MessageBox.Show("Crypto error... \n" + ex.Message.ToString()); } textBox2.Text = Convert.ToBase64String(EncryptedData); for (int i = 0; i < data.Length - 1; i++) { data.SetValue((byte)0, i); } for (int i = 0; i < EncryptedData.Length - 1; i++) { EncryptedData.SetValue((byte)0, i); } } I’ll say right away that I didn’t write everything from scratch, so apparently there are pitfalls, but I just want to make it work :)
"Алгоритмы шифрования с открытым ключом используют буфер фиксированного размера"found this solution:"Стоит заметить, что длина текстового сообщения не может превышать длину ключа, поэтому в данном проекте была реализована разбивка текста на блоки, каждый из которых шифруется, а затем они, обратно склеиваются, что позволяет набирать текст неограниченного размера."How to do this? - Pavel Fedorov