Rijndael Decryption not working

  • Thread starter Thread starter KBS Developer
  • Start date Start date
K

KBS Developer

Hi,

I can encrypt without any problem but while decrypting I got junk.
I've read the other thread about getting junk but that is not my case.

Here is the sample code:
private Rijndael GetKBSAlgorithm()
{
Rijndael rijndaelAlgorithm = Rijndael.Create();
rijndaelAlgorithm.IV = Convert.FromBase64String(tbIV.Text);
rijndaelAlgorithm.Key = Convert.FromBase64String(tbKey.Text);
return rijndaelAlgorithm;
}
private void bEncrypt_Click(object sender, EventArgs e)
{
Rijndael kbsAlogrithm = GetKBSAlgorithm();
ICryptoTransform kbsTransformer =
kbsAlogrithm.CreateEncryptor();

Stream streamToConvert = ConvertStringIntoStream(tbPlain.Text);
byte[] toConvertBytes = new byte[streamToConvert.Length];

CryptoStream kbsCryptoStream = new CryptoStream(streamToConvert,
kbsTransformer, CryptoStreamMode.Write);
kbsCryptoStream.Write(toConvertBytes, 0, toConvertBytes.Length);
kbsCryptoStream.FlushFinalBlock();

byte[] convertedBytes = new byte[(int)streamToConvert.Length];
streamToConvert.Position = 0;
streamToConvert.Read(convertedBytes, 0,
(int)streamToConvert.Length);
kbsCryptoStream.Close();
streamToConvert.Close();

tbEncrypted.Text = Convert.ToBase64String(convertedBytes);
}
private void bDecrypt_Click(object sender, EventArgs e)
{
Rijndael kbsAlogrithm = GetKBSAlgorithm();
ICryptoTransform kbsTransformer =
kbsAlogrithm.CreateDecryptor();

Stream streamToConvert = new
MemoryStream(Convert.FromBase64String(tbEncrypted.Text));
byte[] toConvertBytes =
Convert.FromBase64String(tbEncrypted.Text);
byte[] convertedBytes = new byte[toConvertBytes.Length];
int convertedByteCount;


CryptoStream kbsCryptoStream = new CryptoStream(streamToConvert,
kbsTransformer, CryptoStreamMode.Read);
convertedByteCount = kbsCryptoStream.Read(convertedBytes, 0,
convertedBytes.Length);
kbsCryptoStream.Close();
streamToConvert.Close();

tbDecrypted.Text = Convert.ToBase64String(convertedBytes);
}

Anyone has any clues?
 
KBS,

Just as Alvin (the other poster), I looked at your code but unfortunately it
wouldn't compile and so I didn't bother going through it to see if I could
spot the problem.

If you want people to be able to help you, I would recommend you create a
sample (that compiles) that someone can easily copy and paste into a console
application.

Just my 2 cents.
 
As others said before, hard to tell you the problem bacause there's some
method missing, but it seems the problem is the encryption method.
Here's the fragment of your method i can't understand.

// This seems you write the text into a Stream
Stream streamToConvert = ConvertStringIntoStream(tbPlain.Text);

// You create an array...where you fill this array???
byte[] toConvertBytes = new byte[streamToConvert.Length];

// You create the CryptoStream, but the encrypted output will be written
into the Stream with the text to encrypt????
CryptoStream kbsCryptoStream = new CryptoStream(streamToConvert,
kbsTransformer, CryptoStreamMode.Write);

// You write the content from the array....but what content?? It contains
only zeroes...
kbsCryptoStream.Write(toConvertBytes, 0, toConvertBytes.Length);


Instead of this, you should:
* convert the text to encrypt into bytes using some encoder from the
System.Text namespace. For example, Encoding.UTF8.GetBytes().
* for the constructor of CryptoStream, use a new and empty MemoryStream
object.
* after writing the encoded string (the bytes) into the CryptoStream, the
encrypted data can be extracted from the MemoryBuffer (method ToArray()).

Greetings,


KBS Developer said:
Hi,

I can encrypt without any problem but while decrypting I got junk.
I've read the other thread about getting junk but that is not my case.

Here is the sample code:
private Rijndael GetKBSAlgorithm()
{
Rijndael rijndaelAlgorithm = Rijndael.Create();
rijndaelAlgorithm.IV = Convert.FromBase64String(tbIV.Text);
rijndaelAlgorithm.Key = Convert.FromBase64String(tbKey.Text);
return rijndaelAlgorithm;
}
private void bEncrypt_Click(object sender, EventArgs e)
{
Rijndael kbsAlogrithm = GetKBSAlgorithm();
ICryptoTransform kbsTransformer =
kbsAlogrithm.CreateEncryptor();

Stream streamToConvert = ConvertStringIntoStream(tbPlain.Text);
byte[] toConvertBytes = new byte[streamToConvert.Length];

CryptoStream kbsCryptoStream = new
CryptoStream(streamToConvert, kbsTransformer, CryptoStreamMode.Write);
kbsCryptoStream.Write(toConvertBytes, 0,
toConvertBytes.Length);
kbsCryptoStream.FlushFinalBlock();

byte[] convertedBytes = new byte[(int)streamToConvert.Length];
streamToConvert.Position = 0;
streamToConvert.Read(convertedBytes, 0,
(int)streamToConvert.Length);
kbsCryptoStream.Close();
streamToConvert.Close();

tbEncrypted.Text = Convert.ToBase64String(convertedBytes);
}
private void bDecrypt_Click(object sender, EventArgs e)
{
Rijndael kbsAlogrithm = GetKBSAlgorithm();
ICryptoTransform kbsTransformer =
kbsAlogrithm.CreateDecryptor();

Stream streamToConvert = new
MemoryStream(Convert.FromBase64String(tbEncrypted.Text));
byte[] toConvertBytes =
Convert.FromBase64String(tbEncrypted.Text);
byte[] convertedBytes = new byte[toConvertBytes.Length];
int convertedByteCount;


CryptoStream kbsCryptoStream = new
CryptoStream(streamToConvert, kbsTransformer, CryptoStreamMode.Read);
convertedByteCount = kbsCryptoStream.Read(convertedBytes, 0,
convertedBytes.Length);
kbsCryptoStream.Close();
streamToConvert.Close();

tbDecrypted.Text = Convert.ToBase64String(convertedBytes);
}

Anyone has any clues?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top