0

I'am trying to encrypt and decrypt a text string by calling a SimpleAES class which i got from this accepted anwser Simple 2 way encryption

My problem is how to call this class from Form1 and get the encrypted/decrypted anwser returned?

I tried the following:

private void encryptbtn_Click(object sender, EventArgs e)
{
    string encryptkey = inputtxt.Text;
    SimpleAES simpleAES1 = new SimpleAES();
    simpleAES1.EncryptToString(encryptkey);
    decrypttxt.Text = encryptkey.ToString();
}

Tried to find some basics on classes but couldn't find any covering returning from a class.

Community
  • 1
  • 1
PandaNL
  • 828
  • 5
  • 18
  • 40

2 Answers2

2

You are ignoring the return value of the function SimpleAES.EncryptToString. Store the result in a temporary variable called cipherText and then assign that to the TextBox.Text property.

private void encryptbtn_Click(object sender, EventArgs e)
{
    string encryptkey = inputtxt.Text;
    SimpleAES simpleAES1 = new SimpleAES();
    string cipherText = simpleAES1.EncryptToString(encryptkey);
    decrypttxt.Text = cipherText ;
}
User 12345678
  • 7,714
  • 2
  • 28
  • 46
  • +1 - I think you understood what `decrypttxt` was for better than I did (OP wants to write the output to the TextBox). An even shorter way would be to simply do `decrypttxt.Text = simpleAES1.EncryptToString(encryptKey);` – Tim Jul 03 '13 at 08:03
1

If you look at the class in the accepted answer, you'll see the EncryptToString() returns a string, so:

string encryptedText = simpleAES1.EncryptToString(encryptkey);

In general, you can get values from classes via properties and methods (if the method specifies a return type).

Also, you don't need to call ToString() on encryptkey, as it's already a string.

Tim
  • 28,212
  • 8
  • 63
  • 76