Click here to Skip to main content
15,892,161 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
Please Help to understand with the encryption algorithm code
Algorithm about the such:

(String) ---Encrypt---> (array<Byte>) ---Decode---> (String)

__________________________| Decrypt (*)___________| Encode
__________________________V____________________V
_______________________(String) <---Decrypt (**)--- (array<Byte>)

(*) Works correctly
(**) Deduces an error :(

private: System::Void button1_Click(System::Object^  sender, System::EventArgs^  e) {
				 
		String^ STR =tbString->Text->Trim();
		array<byte>^ buffer = Encrypt( STR, key );
		Encoding^ ascii = Encoding::ASCII;
		String^ decodedString = ascii->GetString( buffer );
		tb_Encrypted->Text = decodedString;

		String^ plaintext = Decrypt( buffer, key );
		tb_Decrypted->Text = plaintext;


			}

array<byte>^ Encrypt( String^ PlainText, SymmetricAlgorithm^ key )
{

   // Create a memory stream.
   MemoryStream^ ms = gcnew MemoryStream;

   // Create a CryptoStream using the memory stream and the 
   // CSP DES key.  
   CryptoStream^ encStream = gcnew CryptoStream( ms,key->CreateEncryptor(),CryptoStreamMode::Write );

   // Create a StreamWriter to write a string
   // to the stream.
   StreamWriter^ sw = gcnew StreamWriter( encStream );

   // Write the plaintext to the stream.
   sw->WriteLine( PlainText );

   // Close the StreamWriter and CryptoStream.
   sw->Close();
   encStream->Close();

   // Get an array of bytes that represents
   // the memory stream.
   array<byte>^buffer = ms->ToArray();
   //String^buffer = ms->ToString();
   // Close the memory stream.
   ms->Close();

   // Return the encrypted byte array.
   return buffer;
}


// Decrypt the byte array.
String^ Decrypt( array<byte>^CypherText, SymmetricAlgorithm^ key )
{

   // Create a memory stream to the passed buffer.
   MemoryStream^ ms = gcnew MemoryStream( CypherText );

   // Create a CryptoStream using the memory stream and the 
   // CSP DES key. 
   CryptoStream^ encStream = gcnew CryptoStream( ms,key->CreateDecryptor(),CryptoStreamMode::Read );

   // Create a StreamReader for reading the stream.
   StreamReader^ sr = gcnew StreamReader( encStream );

   // Read the stream as a string.
   String^ val = sr->ReadLine();

   // Close the streams.
   sr->Close();
   encStream->Close();
   ms->Close();
   return val;
}

</byte></byte></byte></byte>
Posted
Comments
Niklas L 8-Jun-11 5:32am    
"Deduces an error"
What error?
unix_area 8-Jun-11 6:11am    
Actually problem is: when I encode a string to Byte for Decrypting, I have an error like: Inadmissible length of the data for decoding.
exc:
you will see it, just replace button action
private: System::Void button1_Click(System::Object^ sender, System::EventArgs^ e) {

String^ STR =tbString->Text->Trim();
array<byte>^ buffer = Encrypt( STR, key );
Encoding^ ascii = Encoding::ASCII;
String^ decodedString = ascii->GetString( buffer );
tb_Encrypted->Text = decodedString;

array<byte>^ test = ascii->GetBytes( tbString->Text );

String^ plaintext = Decrypt( test, key );
tb_Decrypted->Text = plaintext;


}
Niklas L 8-Jun-11 7:05am    
What makes you think the encoded byte array can be used as a string of characters? E.g. does your Encrypt() method guarantee there are no null bytes in the output? Sounds unlikely.
ThatsAlok 8-Jun-11 5:52am    
Can't you debug it yourself!
unix_area 16-Jun-11 2:58am    
Thank you for all. I solve it

1 solution

I think the issue is nothing to do with encryption but is related to the decode/encode cycle shown on the right hand side of the diagram.

array<byte>(A) - Decode -> String (B) - Encode -> array<byte>(C)


Your code does not show the full details of this conversion but implies that you are using the ASCII encoder which can only handle byte values up to 127.

If the byte array (A) contains values in the range 128..255 these are decoded to ? characters in the intermediate string (B). Subsequent encoding back to a byte array (C) will not recreate the original data. See here http://msdn.microsoft.com/en-us/library/system.text.asciiencoding.aspx[^]

If you do want to get the array A as a string then take a look at the System.Convert class's To/FromBase64String methods.

Alan.
 
Share this answer
 
v3

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900