user2821178
user2821178

Reputation: 43

How do I decrypt encrypted text?

I found code that permits me to encrypt and decrypt a text:

cipherData = textBox2->Text;
plainbytes = Encoding::ASCII->GetBytes(cipherData);

plainKey = Encoding::ASCII->GetBytes("0123456789abcdef");

desObj->Key = plainKey;

desObj->Mode = CipherMode::CBC;

desObj->Padding = PaddingMode::PKCS7;

MemoryStream^ ms = gcnew MemoryStream();
CryptoStream^ cs = gcnew CryptoStream(ms,desObj->CreateEncryptor(),CryptoStreamMode::Write);

cs->Write(plainbytes,0,plainbytes->Length);
cs->Close();

chipherbytes = ms->ToArray();
ms->Close();

textBox3->Text = Encoding::ASCII->GetString(chipherbytes);

//decripto
     
MemoryStream^ ms1 = gcnew MemoryStream(chipherbytes);
CryptoStream^ cs1 = gcnew CryptoStream(ms1,desObj->CreateDecryptor(),CryptoStreamMode::Read);

cs1->Read(chipherbytes,0,chipherbytes->Length);
plainbytes2 = ms1->ToArray();
cs1->Close();
ms1->Close();

textBox4->Text = Encoding::ASCII->GetString(plainbytes2);

It is perfect and works very well. The problem is that I wish decrypt a previous encrypted text, starting from ASCII and not from MemoryStream.

I tried the code in this way:

cipherData = textBox2->Text;
plainbytes = Encoding::ASCII->GetBytes(cipherData);

plainKey = Encoding::ASCII->GetBytes("0123456789abcdef");

desObj->Key = plainKey;

desObj->Mode = CipherMode::CBC;

desObj->Padding = PaddingMode::PKCS7;

MemoryStream^ ms = gcnew MemoryStream();
CryptoStream^ cs = gcnew CryptoStream(ms,desObj->CreateEncryptor(),CryptoStreamMode::Write);

cs->Write(plainbytes,0,plainbytes->Length);
cs->Close();

chipherbytes = ms->ToArray();
ms->Close();

textBox3->Text = Encoding::ASCII->GetString(chipherbytes);

//DECRYPTION CODE
     

cipherData = textBox3->Text;
chipherbytes = Encoding::ASCII->GetBytes(cipherData);
     
MemoryStream^ ms1 = gcnew MemoryStream(chipherbytes);
CryptoStream^ cs1 = gcnew CryptoStream(ms1,desObj->CreateDecryptor(),CryptoStreamMode::Read);

cs1->Read(chipherbytes,0,chipherbytes->Length);
plainbytes2 = ms1->ToArray();
cs1->Close();
ms1->Close();

textBox4->Text = Encoding::ASCII->GetString(plainbytes2);

But when I try the code I receive this error:

An unhandled exception of type 'System.Security.Cryptography.CryptographicException' occurred in mscorlib.dll

Additional Information: The padding is invalid and can not be removed.

EDIT 1:

I switched off the wordwrap property and now I get a new error:

Eccezione non gestita di tipo 'System.Security.Cryptography.CryptographicException' in >mscorlib.dll Informazioni aggiuntive: Lunghezza dei dati da decrittografare non valida.

(An unhandled exception of type 'System.Security.Cryptography.CryptographicException' >occurred in mscorlib.dll Additional information: Length of the data to decrypt is invalid.)

Upvotes: 0

Views: 662

Answers (1)

user2821178
user2821178

Reputation: 43

SOLVED

Iused System::Convert::ToBase64String to write the encrypted text correctly into the textbox avoiding data loss; then I loaded the text from the textbox using System::Convert::FromBase64String

         //ENCRYPTION CODE

         cipherData = textBox2->Text;
         plainbytes = Encoding::Unicode->GetBytes(cipherData);

         plainKey = Encoding::Unicode->GetBytes("0123456789abcdef");

         desObj->Key = plainKey;

         desObj->Mode = CipherMode::CBC;

         desObj->Padding = PaddingMode::PKCS7;

         MemoryStream^ ms = gcnew MemoryStream();
         CryptoStream^ cs = gcnew CryptoStream(ms,desObj->CreateEncryptor(),CryptoStreamMode::Write);

         cs->Write(plainbytes,0,plainbytes->Length);
         cs->Close();

         chipherbytes = ms->ToArray();
         ms->Close();

         textBox3->Text = System::Convert::ToBase64String(chipherbytes);

         //DECRYPTION CODE


         chipherbytes = System::Convert::FromBase64String(textBox3->Text);

         MemoryStream^ ms1 = gcnew MemoryStream(chipherbytes);
         CryptoStream^ cs1 = gcnew CryptoStream(ms1,desObj->CreateDecryptor(),CryptoStreamMode::Read);

         cs1->Read(chipherbytes,0,chipherbytes->Length);
         plainbytes2 = ms1->ToArray();
         cs1->Close();
         ms1->Close();

         textBox4->Text = Encoding::Unicode->GetString(plainbytes2);

Upvotes: 1

Related Questions