473,763 Members | 7,719 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Rijndael Decrypt returning escape characters at end of string

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)

{

byte[] inputStringByte s = Encoding.ASCII. GetBytes(input) ;

byte[] outputBytes;

MemoryStream ms = new MemoryStream(in putStringBytes. Length);

RijndaelManaged rijndael = new RijndaelManaged ();

ICryptoTransfor m rdTransform = rijndael.Create Encryptor((byte[])key.Clone(),
(byte[])iv.Clone());

CryptoStream cs = new CryptoStream(ms , rdTransform, CryptoStreamMod e.Write);

cs.Write(inputS tringBytes, 0, inputStringByte s.Length);

cs.FlushFinalBl ock();

outputBytes = ms.ToArray();

ms.Close();

cs.Close();

rdTransform.Dis pose();

rijndael.Clear( );

return Convert.ToBase6 4String(outputB ytes);

}

public static string Decrypt(string input, byte[] key, byte[] iv)

{

byte[] inputStringByte s = Convert.FromBas e64String(input );

byte[] outputTextBytes = new byte[inputStringByte s.Length];

RijndaelManaged rijndael = new RijndaelManaged ();

MemoryStream ms = new MemoryStream(in putStringBytes) ;

ICryptoTransfor m rdTransform = rijndael.Create Decryptor((byte[])key.Clone(),
(byte[])iv.Clone());

CryptoStream cs = new CryptoStream(ms , rdTransform, CryptoStreamMod e.Read);

cs.Read(outputT extBytes, 0, outputTextBytes .Length);

ms.Close();

cs.Close();

rdTransform.Dis pose();

rijndael.Clear( );

return Encoding.ASCII. GetString(outpu tTextBytes);

}

Thanks

Kev
Nov 17 '05 #1
4 5539


Mantorok wrote:
Hi
Hi,

There is *much* more to proper cryptography that most people think and
it is *easy* to get it wrong -- and in cryptography anything is only as
strong as it's weakest link.
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:


Rinjdael is a block-cipher and the default padding used to obtain a
block-size mutiple of input from your byte inputStringByte s is
apparently: 0-padding, which is known as the worst choice possible :)

Try setting the padding to PaddingMode.PKC S7 which (besides crytographic
benefits) is a 1-1 paddingmode for all block-ciphers.

You are also missing randomization. Call GenerateIV and write the IV to
the start of the stream, and use it to initialize IV on the receiving
side. This will get you nondeterministi c encryption, where the same
massage is encrypted differently each time it is transmitted.

Also note that encryption only guarantees confidentiality , not integrity
(you and the receiver agree on the content of the entire message, not
just a prefix). This is the property that prevents a man in the middle
from changing "Attack at dawn on friday" to "Attack at dawn"

Other properties you might wish to consider important to the "security"
of your protocol is:

* Authentication: who sent the message
* Non-repudiation: prevent the sender from later postulating that he
sent something else
* Anti-replay: prevent someone from using a recording of encrypted
traffic to initiate accepted communication

--
Helge Jensen
mailto:he****** ****@slog.dk
sip:he********* *@slog.dk
-=> Sebastian cover-music: http://ungdomshus.nu <=-
Nov 17 '05 #2


Mantorok wrote:
Hi
Hi,

There is *much* more to proper cryptography that most people think and
it is *easy* to get it wrong -- and in cryptography anything is only as
strong as it's weakest link.
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:


Rinjdael is a block-cipher and the default padding used to obtain a
block-size mutiple of input from your byte inputStringByte s is
apparently: 0-padding, which is known as the worst choice possible :)

Try setting the padding to PaddingMode.PKC S7 which (besides crytographic
benefits) is a 1-1 paddingmode for all block-ciphers.

You are also missing randomization. Call GenerateIV and write the IV to
the start of the stream, and use it to initialize IV on the receiving
side. This will get you nondeterministi c encryption, where the same
massage is encrypted differently each time it is transmitted.

Also note that encryption only guarantees confidentiality , not integrity
(you and the receiver agree on the content of the entire message, not
just a prefix). This is the property that prevents a man in the middle
from changing "Attack at dawn on friday" to "Attack at dawn"

Other properties you might wish to consider important to the "security"
of your protocol is:

* Authentication: who sent the message
* Non-repudiation: prevent the sender from later postulating that he
sent something else
* Anti-replay: prevent someone from using a recording of encrypted
traffic to initiate accepted communication

--
Helge Jensen
mailto:he****** ****@slog.dk
sip:he********* *@slog.dk
-=> Sebastian cover-music: http://ungdomshus.nu <=-
Nov 17 '05 #3
Mantorok <no**@tiscali.c o.uk> wrote:
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?


You're not using the return value of Read, which tells you how many
bytes have *actually* been read.

You should also consider using Encoding.UTF8 instead of Encoding.ASCII,
unless you're absolutely *sure* that all the characters you need to
encode will be in ASCII.

You should also use using statements to make sure you always close your
streams even if an exception is thrown.

Finally, don't assume that a single call to Read will always read
everything you want it to. See
http://www.pobox.com/~skeet/csharp/readbinary.html

--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 17 '05 #4
Mantorok <no**@tiscali.c o.uk> wrote:
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?


You're not using the return value of Read, which tells you how many
bytes have *actually* been read.

You should also consider using Encoding.UTF8 instead of Encoding.ASCII,
unless you're absolutely *sure* that all the characters you need to
encode will be in ASCII.

You should also use using statements to make sure you always close your
streams even if an exception is thrown.

Finally, don't assume that a single call to Read will always read
everything you want it to. See
http://www.pobox.com/~skeet/csharp/readbinary.html

--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 17 '05 #5

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

Similar topics

3
2424
by: Ignacio De Marco | last post by:
I'm not very familiar with C, so I would like to ask you how can use the algorithm Rijndael, suppousing that I want two simple functions (in C ANSI) implementing the CBC or ECB Modes (is the same for me, because I have both implementations in Delphi, but i need the same implementations in C for an AS400) with an interface similiar to this (in Object Pascal): function Encrypt(PlainText: string; Key: string): string function...
18
7195
by: Steve Litvack | last post by:
Hello, I have built an XMLDocument object instance and I get the following string when I examine the InnerXml property: <?xml version=\"1.0\"?><ROOT><UserData UserID=\"2282\"><Tag1 QID=\"55111\"><Tag2 AID=\"5511101\"></Tag2></Tag1><Tag1 QID=\"55112\"><Tag2 AID=\"5511217\"></Tag2></Tag1><Tag1 QID=\"5512282\"><Tag2 AID=\"551228206\"></Tag2></Tag1><Tag1 QID=\"55114\"><Tag2 AID=\"5511406\"></Tag2></Tag1><Tag1 QID=\"55115\"><Tag2
5
6924
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
0
368
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)
1
6611
by: Nicholas Holder | last post by:
A client creates a connection to the server using the TCPListener/Client classes and transfers data via a NetworkStream. When the client connects, the server creates a process and redirects its StandardOut to traverse back over the network to the client. I want to encrypt this data and the code I have is below. However, occasionally during processing, I receive an exception stating the PKCS7 padding is invalid and cannot be removed. I have...
5
4184
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
1396
by: Vayse | last post by:
I need to encrypt some strings in serveral programs I have. So I grabbed some code from the MS help. I wrote an app that would help me generate the encrypted strings. Its s form with 4 text boxes txtBefore - the original string txtIV, txtKey - the Rijndael IV and Key txtAfter - the encrypted string For testing, I enter text into txtBefore, then press btnEncryptText
4
4799
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
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;
0
9563
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9386
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9822
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8821
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
6642
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
5270
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
5405
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3917
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
3522
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.