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.
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 ConsoleApplication1
{
class MyMainClass
{
public static void Main()
{
string original = "Original string to be sent.";
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.GenerateKey();
myRijndael.GenerateIV();
//Get the key and IV.
key = myRijndael.Key;
IV = myRijndael.IV;
//Get an encryptor.
ICryptoTransform encryptor = myRijndael.CreateEncryptor(key, IV);
//Encrypt the data.
MemoryStream msEncrypt = new MemoryStream();
CryptoStream csEncrypt = new CryptoStream(msEncrypt, encryptor,
CryptoStreamMode.Write);
//Convert the data to a byte array.
toEncrypt = textConverter.GetBytes(original);
//Write all data to the crypto stream and flush it.
csEncrypt.Write(toEncrypt, 0, toEncrypt.Length);
csEncrypt.FlushFinalBlock();
//Get encrypted array of bytes.
encrypted = msEncrypt.ToArray();
//Here I send data trough network stream
//create byte array to be sent trough tcp network
byte[] finalized = new byte[key.Length+IV.Length+encrypted.Length];
//merge all values into single byte array
key.CopyTo(finalized,0);
IV.CopyTo(finalized,32);
encrypted.CopyTo(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 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.Length-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.Length; 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.
ICryptoTransform decryptor = myRijndael.CreateDecryptor(key1, IV1);
//Now decrypt the previously encrypted message using the decryptor
MemoryStream msDecrypt = new MemoryStream(encrypted1);
CryptoStream csDecrypt = new CryptoStream(msDecrypt, decryptor,
CryptoStreamMode.Read);
fromEncrypt = new byte[encrypted1.Length];
//Read the data out of the crypto stream.
csDecrypt.Read(fromEncrypt, 0, fromEncrypt.Length);
//Convert the byte array back into a string.
roundtrip = textConverter.GetString(fromEncrypt);
//Display the original data and the decrypted data to see where the actual
problem is:
Console.WriteLine("Original string: {0}", original + "_");
Console.WriteLine("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 shit at the end. I placed "_" sign just to see that there is a problem
with data I got.
}
}
}