This is the problem: I do not get the output I need when encoding and
decoding data using rijndael alghoritm.
Look at the code and see what the problem is actually:
Please paste this code into your Visual Studio and compile it + run it; so
you can see what the actual problem is.
Thanks.
code:
using System;
using System.IO;
using System.Text;
using System.Security .Cryptography;
namespace ConsoleApplicat ion1
{
class MyMainClass
{
public static void Main()
{
string original = "Original string";
string roundtrip;
ASCIIEncoding textConverter = new ASCIIEncoding() ;
RijndaelManaged myRijndael = new RijndaelManaged ();
byte[] fromEncrypt;
byte[] encrypted;
byte[] toEncrypt;
byte[] key;
byte[] IV;
//Create a new key and initialization vector.
myRijndael.Gene rateKey();
myRijndael.Gene rateIV();
//Get the key and IV.
key = myRijndael.Key;
IV = myRijndael.IV;
//Get an encryptor.
ICryptoTransfor m encryptor = myRijndael.Crea teEncryptor(key , IV);
//Encrypt the data.
MemoryStream msEncrypt = new MemoryStream();
CryptoStream csEncrypt = new CryptoStream(ms Encrypt, encryptor,
CryptoStreamMod e.Write);
//Convert the data to a byte array.
toEncrypt = textConverter.G etBytes(origina l);
//Write all data to the crypto stream and flush it.
csEncrypt.Write (toEncrypt, 0, toEncrypt.Lengt h);
csEncrypt.Flush FinalBlock();
//Get encrypted array of bytes.
encrypted = msEncrypt.ToArr ay();
//Here I send data trough network stream
//create byte array to be sent trough tcp network
byte[] finalized = new byte[key.Length+IV.L ength+encrypted .Length];
//merge all values into single byte array
key.CopyTo(fina lized,0);
IV.CopyTo(final ized,32);
encrypted.CopyT o(finalized,48) ;
//here goes tcp code with sending the array trough network. it works fine,
and is no problem.
//For simplicitiy's sake, here i'll just simulate a new application that
uses values it got from the first application.
//SIMULATED NEW APPLICATION
//Create values that will be used in decryption process and that are passed
trough network
byte[] key1 = new byte[32];
byte[] IV1 = new byte[16];
byte[] encrypted1 = new byte[finalized.Lengt h-48];
//read all values from the passed byte array and divid those correctly.
for (int i=0; i<32; i++)
{
key1[i]=finalized[i];
}
for (int i=32; i<48; i++)
{
IV1[i-32]=finalized[i];
}
for (int i=48; i<finalized.Len gth; i++)
{
encrypted1[i-48]=finalized[i];
}
//now use values to get the result:
//Get a decryptor that uses the same key and IV as the encryptor.
ICryptoTransfor m decryptor = myRijndael.Crea teDecryptor(key 1, IV1);
//Now decrypt the previously encrypted message using the decryptor
MemoryStream msDecrypt = new MemoryStream(en crypted1);
CryptoStream csDecrypt = new CryptoStream(ms Decrypt, decryptor,
CryptoStreamMod e.Read);
fromEncrypt = new byte[encrypted1.Leng th];
//Read the data out of the crypto stream.
csDecrypt.Read( fromEncrypt, 0, fromEncrypt.Len gth);
//Convert the byte array back into a string.
roundtrip = textConverter.G etString(fromEn crypt);
//Display the original data and the decrypted data to see where the actual
problem is:
Console.WriteLi ne("Original string: {0}", original + "_");
Console.WriteLi ne("String I got to another application: {0}", roundtrip +
"_");
//Guess what! The result string has some dummy stuff at the end and it is
//just not the data I encoded. It is actually there, but I really don't want
//that sh*t at the end. I placed "_" sign just to see that there is a
problem with data I got.
}
}
}