473,748 Members | 2,602 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Rijndael Decryption not working

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 rijndaelAlgorit hm = Rijndael.Create ();
rijndaelAlgorit hm.IV = Convert.FromBas e64String(tbIV. Text);
rijndaelAlgorit hm.Key = Convert.FromBas e64String(tbKey .Text);
return rijndaelAlgorit hm;
}
private void bEncrypt_Click( object sender, EventArgs e)
{
Rijndael kbsAlogrithm = GetKBSAlgorithm ();
ICryptoTransfor m kbsTransformer =
kbsAlogrithm.Cr eateEncryptor() ;

Stream streamToConvert = ConvertStringIn toStream(tbPlai n.Text);
byte[] toConvertBytes = new byte[streamToConvert .Length];

CryptoStream kbsCryptoStream = new CryptoStream(st reamToConvert,
kbsTransformer, CryptoStreamMod e.Write);
kbsCryptoStream .Write(toConver tBytes, 0, toConvertBytes. Length);
kbsCryptoStream .FlushFinalBloc k();

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

tbEncrypted.Tex t = Convert.ToBase6 4String(convert edBytes);
}
private void bDecrypt_Click( object sender, EventArgs e)
{
Rijndael kbsAlogrithm = GetKBSAlgorithm ();
ICryptoTransfor m kbsTransformer =
kbsAlogrithm.Cr eateDecryptor() ;

Stream streamToConvert = new
MemoryStream(Co nvert.FromBase6 4String(tbEncry pted.Text));
byte[] toConvertBytes =
Convert.FromBas e64String(tbEnc rypted.Text);
byte[] convertedBytes = new byte[toConvertBytes. Length];
int convertedByteCo unt;
CryptoStream kbsCryptoStream = new CryptoStream(st reamToConvert,
kbsTransformer, CryptoStreamMod e.Read);
convertedByteCo unt = kbsCryptoStream .Read(converted Bytes, 0,
convertedBytes. Length);
kbsCryptoStream .Close();
streamToConvert .Close();

tbDecrypted.Tex t = Convert.ToBase6 4String(convert edBytes);
}

Anyone has any clues?

Jan 29 '08 #1
3 2570
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*********@kb stours.comwrote in message
news:Og******** ******@TK2MSFTN GP03.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 rijndaelAlgorit hm = Rijndael.Create ();
rijndaelAlgorit hm.IV = Convert.FromBas e64String(tbIV. Text);
rijndaelAlgorit hm.Key = Convert.FromBas e64String(tbKey .Text);
return rijndaelAlgorit hm;
}
private void bEncrypt_Click( object sender, EventArgs e)
{
Rijndael kbsAlogrithm = GetKBSAlgorithm ();
ICryptoTransfor m kbsTransformer =
kbsAlogrithm.Cr eateEncryptor() ;

Stream streamToConvert = ConvertStringIn toStream(tbPlai n.Text);
byte[] toConvertBytes = new byte[streamToConvert .Length];

CryptoStream kbsCryptoStream = new
CryptoStream(st reamToConvert, kbsTransformer, CryptoStreamMod e.Write);
kbsCryptoStream .Write(toConver tBytes, 0,
toConvertBytes. Length);
kbsCryptoStream .FlushFinalBloc k();

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

tbEncrypted.Tex t = Convert.ToBase6 4String(convert edBytes);
}
private void bDecrypt_Click( object sender, EventArgs e)
{
Rijndael kbsAlogrithm = GetKBSAlgorithm ();
ICryptoTransfor m kbsTransformer =
kbsAlogrithm.Cr eateDecryptor() ;

Stream streamToConvert = new
MemoryStream(Co nvert.FromBase6 4String(tbEncry pted.Text));
byte[] toConvertBytes =
Convert.FromBas e64String(tbEnc rypted.Text);
byte[] convertedBytes = new byte[toConvertBytes. Length];
int convertedByteCo unt;
CryptoStream kbsCryptoStream = new
CryptoStream(st reamToConvert, kbsTransformer, CryptoStreamMod e.Read);
convertedByteCo unt = kbsCryptoStream .Read(converted Bytes, 0,
convertedBytes. Length);
kbsCryptoStream .Close();
streamToConvert .Close();

tbDecrypted.Tex t = Convert.ToBase6 4String(convert edBytes);
}

Anyone has any clues?
Jan 30 '08 #2
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*********@kb stours.comwrote in message
news:Og******** ******@TK2MSFTN GP03.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 rijndaelAlgorit hm = Rijndael.Create ();
rijndaelAlgorit hm.IV = Convert.FromBas e64String(tbIV. Text);
rijndaelAlgorit hm.Key = Convert.FromBas e64String(tbKey .Text);
return rijndaelAlgorit hm;
}
private void bEncrypt_Click( object sender, EventArgs e)
{
Rijndael kbsAlogrithm = GetKBSAlgorithm ();
ICryptoTransfor m kbsTransformer =
kbsAlogrithm.Cr eateEncryptor() ;

Stream streamToConvert = ConvertStringIn toStream(tbPlai n.Text);
byte[] toConvertBytes = new byte[streamToConvert .Length];

CryptoStream kbsCryptoStream = new
CryptoStream(st reamToConvert, kbsTransformer, CryptoStreamMod e.Write);
kbsCryptoStream .Write(toConver tBytes, 0,
toConvertBytes. Length);
kbsCryptoStream .FlushFinalBloc k();

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

tbEncrypted.Tex t = Convert.ToBase6 4String(convert edBytes);
}
private void bDecrypt_Click( object sender, EventArgs e)
{
Rijndael kbsAlogrithm = GetKBSAlgorithm ();
ICryptoTransfor m kbsTransformer =
kbsAlogrithm.Cr eateDecryptor() ;

Stream streamToConvert = new
MemoryStream(Co nvert.FromBase6 4String(tbEncry pted.Text));
byte[] toConvertBytes =
Convert.FromBas e64String(tbEnc rypted.Text);
byte[] convertedBytes = new byte[toConvertBytes. Length];
int convertedByteCo unt;
CryptoStream kbsCryptoStream = new
CryptoStream(st reamToConvert, kbsTransformer, CryptoStreamMod e.Read);
convertedByteCo unt = kbsCryptoStream .Read(converted Bytes, 0,
convertedBytes. Length);
kbsCryptoStream .Close();
streamToConvert .Close();

tbDecrypted.Tex t = Convert.ToBase6 4String(convert edBytes);
}

Anyone has any clues?

Jan 30 '08 #3
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 = ConvertStringIn toStream(tbPlai n.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(st reamToConvert,
kbsTransformer, CryptoStreamMod e.Write);

// You write the content from the array....but what content?? It contains
only zeroes...
kbsCryptoStream .Write(toConver tBytes, 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.G etBytes().
* 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*********@kb stours.comescri bió en el mensaje de
noticias:Og**** **********@TK2M SFTNGP03.phx.gb l...
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 rijndaelAlgorit hm = Rijndael.Create ();
rijndaelAlgorit hm.IV = Convert.FromBas e64String(tbIV. Text);
rijndaelAlgorit hm.Key = Convert.FromBas e64String(tbKey .Text);
return rijndaelAlgorit hm;
}
private void bEncrypt_Click( object sender, EventArgs e)
{
Rijndael kbsAlogrithm = GetKBSAlgorithm ();
ICryptoTransfor m kbsTransformer =
kbsAlogrithm.Cr eateEncryptor() ;

Stream streamToConvert = ConvertStringIn toStream(tbPlai n.Text);
byte[] toConvertBytes = new byte[streamToConvert .Length];

CryptoStream kbsCryptoStream = new
CryptoStream(st reamToConvert, kbsTransformer, CryptoStreamMod e.Write);
kbsCryptoStream .Write(toConver tBytes, 0,
toConvertBytes. Length);
kbsCryptoStream .FlushFinalBloc k();

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

tbEncrypted.Tex t = Convert.ToBase6 4String(convert edBytes);
}
private void bDecrypt_Click( object sender, EventArgs e)
{
Rijndael kbsAlogrithm = GetKBSAlgorithm ();
ICryptoTransfor m kbsTransformer =
kbsAlogrithm.Cr eateDecryptor() ;

Stream streamToConvert = new
MemoryStream(Co nvert.FromBase6 4String(tbEncry pted.Text));
byte[] toConvertBytes =
Convert.FromBas e64String(tbEnc rypted.Text);
byte[] convertedBytes = new byte[toConvertBytes. Length];
int convertedByteCo unt;
CryptoStream kbsCryptoStream = new
CryptoStream(st reamToConvert, kbsTransformer, CryptoStreamMod e.Read);
convertedByteCo unt = kbsCryptoStream .Read(converted Bytes, 0,
convertedBytes. Length);
kbsCryptoStream .Close();
streamToConvert .Close();

tbDecrypted.Tex t = Convert.ToBase6 4String(convert edBytes);
}

Anyone has any clues?

Jan 30 '08 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
6923
by: William Stacey [MVP] | last post by:
The Decypt2() method below does not work. It completes, but does not do the right thing. The first transform request returns 0 bytes. The first Decypt() method works as we work on a stream instead of blocks. I would like to know how to get the block method working. TIA. using System; using System.IO; using System.Security.Cryptography; namespace SocketServers.Crypto
4
5537
by: Mantorok | last post by:
Hi I have a couple of encryption methods but when I call decrypt I get the string back but with a load \0 escape characters on the end? Any idea why? It is actually causing problems in some places, here are my methods: public static string Encrypt(string input, byte key, byte iv)
5
4182
by: ~~~ .NET Ed ~~~ | last post by:
Anybody has any idea why this simple thing is not working? I pass a text file as input to encrypt it, then pass the encrypted version to the same function and get some garbled data not at all resembling the input file. Rijndael rijndaelAlg = Rijndael.Create(); rijndaelAlg.BlockSize = 128; // 128 bits to comply with AES rijndaelAlg.Padding = PaddingMode.PKCS7;
0
2100
by: Jens Müller | last post by:
Hello, I try to program a Rijndael encryption in Windows which has to be compatible with php. In php I use the code below to encrypt with a 256 Bit Key and a 256 Bit block cipher. My windows code has the same specs, but the outcome is different. So far I use no iv to facilitate.
4
4798
by: Sylvie | last post by:
http://www.obviex.com/samples/Encryption.aspx According to this link, I am using Rijndael Encryption & Decryption Algorithms, But I want my encrypted strings just CAPS string and just alphanumeric values ABC...Z and 123...90, no other chars I want, what should I do ? or what other algos I must use, Thanks
10
7828
by: Iwan Budihalim | last post by:
Who can help? I'm trying to implement an encrypted (plain text) communication between a Delphi application and an ASP.NET. My choice is AES/rijndael-128. For both sides, i use standard modules: Delphi: TDCP_rijndael Component (DCPcrypt Cryptographic Component Library v2) from cityinthesky (www.cityinthesky.com) ASP.NET : Standard library of Rijndael both sides are working, BUT they do different! The results in encryption
4
5705
by: Fritjolf | last post by:
Hi. I've got a strange problem... I've made a simple program to test encryption/decryption. I use Rijndael encryption and here are the most important properties. RijndaelManaged cipher = new RijndaelManaged(); cipher.KeySize = 256; cipher.BlockSize = 256;
13
3591
by: Tom Andrecht | last post by:
I'm trying to get some encryption/decryption routines going to take care of my data, and while the encryption is working great, I keep running into the message "Padding is invalid and cannot be removed" on the decryption piece. From everything I can see, I am doing things correctly here My code is as follows: private const string PassStr = "MyPrivateKey"; private static readonly byte PassSalt = new byte { Byte Byte Byte Byte Byte};...
5
9527
by: Netwatcher | last post by:
well, i started messing around with dictionaries, yet, most of the pages i found about them always talk about getting only one word out of it and turning it vice versa, i've been playing with that code for a few hours: #dics Encryption={',':'hy{;',' ':'h4x0r2','':'zomg','?':'bko','a':'ika','b':'d0v','c':'ino', 'd':'maw', 'e':'aon', 'f':'que', 'g':'kip', 'h':'an', 'n':'ko print lol except KeyError: print 'These...
0
9530
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9363
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9312
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8237
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6073
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4593
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4864
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
2775
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2206
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.