title: How do I decrypt encrypted text?
body: <p>I found code that permits me to encrypt and decrypt a text:</p>
  <pre><code>cipherData = textBox2-&gt;Text;
  plainbytes = Encoding::ASCII-&gt;GetBytes(cipherData);
  
  plainKey = Encoding::ASCII-&gt;GetBytes(&quot;0123456789abcdef&quot;);
  
  desObj-&gt;Key = plainKey;
  
  desObj-&gt;Mode = CipherMode::CBC;
  
  desObj-&gt;Padding = PaddingMode::PKCS7;
  
  MemoryStream^ ms = gcnew MemoryStream();
  CryptoStream^ cs = gcnew CryptoStream(ms,desObj-&gt;CreateEncryptor(),CryptoStreamMode::Write);
  
  cs-&gt;Write(plainbytes,0,plainbytes-&gt;Length);
  cs-&gt;Close();
  
  chipherbytes = ms-&gt;ToArray();
  ms-&gt;Close();
  
  textBox3-&gt;Text = Encoding::ASCII-&gt;GetString(chipherbytes);
  
  //decripto
       
  MemoryStream^ ms1 = gcnew MemoryStream(chipherbytes);
  CryptoStream^ cs1 = gcnew CryptoStream(ms1,desObj-&gt;CreateDecryptor(),CryptoStreamMode::Read);
  
  cs1-&gt;Read(chipherbytes,0,chipherbytes-&gt;Length);
  plainbytes2 = ms1-&gt;ToArray();
  cs1-&gt;Close();
  ms1-&gt;Close();
  
  textBox4-&gt;Text = Encoding::ASCII-&gt;GetString(plainbytes2);
  </code></pre>
  <p>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.</p>
  <p>I tried the code in this way:</p>
  <pre><code>cipherData = textBox2-&gt;Text;
  plainbytes = Encoding::ASCII-&gt;GetBytes(cipherData);
  
  plainKey = Encoding::ASCII-&gt;GetBytes(&quot;0123456789abcdef&quot;);
  
  desObj-&gt;Key = plainKey;
  
  desObj-&gt;Mode = CipherMode::CBC;
  
  desObj-&gt;Padding = PaddingMode::PKCS7;
  
  MemoryStream^ ms = gcnew MemoryStream();
  CryptoStream^ cs = gcnew CryptoStream(ms,desObj-&gt;CreateEncryptor(),CryptoStreamMode::Write);
  
  cs-&gt;Write(plainbytes,0,plainbytes-&gt;Length);
  cs-&gt;Close();
  
  chipherbytes = ms-&gt;ToArray();
  ms-&gt;Close();
  
  textBox3-&gt;Text = Encoding::ASCII-&gt;GetString(chipherbytes);
  
  //DECRYPTION CODE
       
  
  cipherData = textBox3-&gt;Text;
  chipherbytes = Encoding::ASCII-&gt;GetBytes(cipherData);
       
  MemoryStream^ ms1 = gcnew MemoryStream(chipherbytes);
  CryptoStream^ cs1 = gcnew CryptoStream(ms1,desObj-&gt;CreateDecryptor(),CryptoStreamMode::Read);
  
  cs1-&gt;Read(chipherbytes,0,chipherbytes-&gt;Length);
  plainbytes2 = ms1-&gt;ToArray();
  cs1-&gt;Close();
  ms1-&gt;Close();
  
  textBox4-&gt;Text = Encoding::ASCII-&gt;GetString(plainbytes2);
  </code></pre>
  <p>But when I try the code I receive this error:</p>
  <blockquote>
  <p>An unhandled exception of type
  'System.Security.Cryptography.CryptographicException' occurred in
  mscorlib.dll</p>
  <p>Additional Information: The padding is invalid and can not be removed.</p>
  </blockquote>
  <p><strong>EDIT 1:</strong></p>
  <p>I switched off the wordwrap property and now I get a new error:</p>
  <blockquote>
  <p>Eccezione non gestita di tipo 'System.Security.Cryptography.CryptographicException' in &gt;mscorlib.dll
  Informazioni aggiuntive: Lunghezza dei dati da decrittografare non valida.</p>
  <p>(An unhandled exception of type 'System.Security.Cryptography.CryptographicException' &gt;occurred in mscorlib.dll
  Additional information: Length of the data to decrypt is invalid.)</p>
  </blockquote>