2 Replies Latest reply: Feb 2, 2012 6:32 PM by EJP RSS

    Simple RSA application not decrypting in Form2.

    914964
      Hi all,

      Im new to cryptograpgy and also to this Oracle forum, so I hope Im OK asking questions about C# in her!?

      I have just started to learn about Cryptography as I will be implementing it into an asp.net payment system at some point for my dissertation. I have just put together a very simple WindowsFormApp to encrypt / decrypt a string message.

      When I enter > encrypt > decrypt > display the string message all in one form everything works fine... But when I send the encrypted string data to a second form and try to decrypt in there it doesn't work. I know that the encrypted data is received by the second form because I can see its value of (128) when Im stepping through the code.

      However, When I step out of the .Decrypt() method the value (128) becomes null...? I can not work out why / how it becomes null when the same code used in the first form (Form1) works fine...!

      Any help will be gratefully received. Thank you!

      Form1

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

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

      RSACryptoServiceProvider RSAalg = new RSACryptoServiceProvider();
      UnicodeEncoding ByteConverter = new UnicodeEncoding();
      byte[] encryptedData;

      private void button1_Click(object sender, EventArgs e) // button1 = Encrypt
      {
      try
      {
      byte[] dataToEncrypt = ByteConverter.GetBytes(textBox1.Text);

      //Display the origianl data to the console.
      //textBox1.Text = ("Original Data: " + dataString);

      encryptedData = RSAalg.Encrypt(dataToEncrypt, false);

      textBox1.Text = ("Encrypted Data: "+ ByteConverter.GetString(encryptedData));
      }
      catch (CryptographicException ex)
      {
      textBox1.Text = (ex.Message);
      }
      }

      private void button2_Click(object sender, EventArgs e) // button2 = 'Send' to Form2
      {
      try
      {
      //byte[] decryptedData;
      //decryptedData = RSAalg.Decrypt(encryptedData, false);

      Form2 frm2 = new Form2();
      frm2.Show();
      frm2.displayMsg(encryptedData);

      //textBox2.Text = ("Decrypted plaintext: " + ByteConverter.GetString(decryptedData));

      }
      catch (CryptographicException)
      {
      // ToDo:
      }
      }

      private void Form1_Load(object sender, EventArgs e)
      {

      }
      }

      Form2

      namespace encryptTut
      {
      public partial class Form2 : Form
      {
      public Form2()
      {
      InitializeComponent();
      }

      RSACryptoServiceProvider RSAalg = new RSACryptoServiceProvider();
      UnicodeEncoding ByteConverter = new UnicodeEncoding();
      byte[] decryptedData;
      byte[] encryptedData;

      private void Form2_Load(object sender, EventArgs e)
      {

      }

      public void displayMsg(byte[] encryptedData)
      {
      decryptedData = RSAalg.Decrypt(encryptedData, false);
      }

      private void button1_Click_1(object sender, EventArgs e) // buttoon1 = Show encrypted message
      {
      textBox1.Text = ("Encrypted Message: " + ByteConverter.GetString(encryptedData));
      }

      public void button2_Click(object sender, EventArgs e) // button2 = Decrypt message
      {
      //byte[] decryptedData;
      //decryptedData = RSAalg.Decrypt(encryptedData, false);
      textBox1.Text = ("Decrypted Message: " + ByteConverter.GetString(decryptedData));
      }
      }
      }

      Sorry for the bad naming. I was a bit lazy with the naming as I thought it was going to just be a quick application. Also, I can not see where to enter code properly, sorry if there is a way... im a newbie!