By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,456 Members | 1,742 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,456 IT Pros & Developers. It's quick & easy.

Rijndael Decryption not working

P: n/a
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?

Jan 29 '08 #1
Share this Question
Share on Google+
3 Replies


P: n/a
I was trying to sift thru your code but there are undefined functions in
there and being the lazy kind, I wasn't motivated to write my own. Here is a
link that you can compare your code to.
http://www.obviex.com/samples/Encryption.aspx

--

Regards,
Alvin Bruney [MVP ASP.NET]

[Shameless Author plug]
The O.W.C. Black Book, 2nd Edition
Exclusively on www.lulu.com/owc $19.99
-------------------------------------------------------

"KBS Developer" <de*********@kbstours.comwrote in message
news:Og**************@TK2MSFTNGP03.phx.gbl...
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?
Jan 30 '08 #2

P: n/a
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.
"KBS Developer" <de*********@kbstours.comwrote in message
news:Og**************@TK2MSFTNGP03.phx.gbl...
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?

Jan 30 '08 #3

P: n/a
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" <de*********@kbstours.comescribió en el mensaje de
noticias:Og**************@TK2MSFTNGP03.phx.gbl...
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?

Jan 30 '08 #4

This discussion thread is closed

Replies have been disabled for this discussion.