About Encryption ...

  • Thread starter José Pérez Hernández
  • Start date
J

José Pérez Hernández

Hi,

I'm testing a Rijndael Symetric Algorithm Implementation to encrypt data.
With that intention, i made use of code that i saw in
http://www.derkeiler.com/Newsgroups...t.framework.aspnet.security/2003-03/0223.html
that encapsulates very good the process of encryption regardless of the
CryptoGraphic Service, in a class called SymmCrypto.

As you can see in the last chunk of code:

int i = 0;
for (i = 0; i < bytOut.Length; i++)
if (bytOut == 0)
break;

where byOut is the buffer with the encryption resultant data. The intention
is to trim the finnaly '\0' innecessary bytes.

But the matter is that i must to know where to cut, because the Rijndael
Decryption seems only to allows an amount of data, and produces a data block
of fixed size from which i'm interested just partially ( i do not want the
trailing zeros).

What is the relation between the size of the data block to encript, the key
size, and the size of the result (encrypted data block)?

In simple words.. given a key size, wich must to be the length of the data
to encrypt ?

Here is the code...

public string EncString(string Source, string Key)
{
byte[] bytIn = System.Text.ASCIIEncoding.ASCII.GetBytes(Source);

// create a MemoryStream so that the process can be done without I/O
files
System.IO.MemoryStream ms = new System.IO.MemoryStream();
byte[] bytKey = GetLegalKey(Key);

// set the private key
mobjCryptoService.Key = bytKey;
mobjCryptoService.IV = bytKey;

// create an Encryptor from the Provider Service instance
ICryptoTransform encrypto = mobjCryptoService.CreateEncryptor();

// create Crypto Stream that transforms a stream using the encryption
CryptoStream cs = new CryptoStream(ms, encrypto, CryptoStreamMode.Write);

// write out encrypted content into MemoryStream
cs.Write(bytIn, 0, bytIn.Length);
cs.FlushFinalBlock();

// get the output and trim the '\0' bytes
byte[] bytOut = ms.GetBuffer();
int i = 0;
for (i = 0; i < bytOut.Length; i++)
if (bytOut == 0)
break;
// convert into Base64 so that the result can be used in xml
return System.Convert.ToBase64String(bytOut, 0, i);
}

Regards, José.
 
H

Hernan de Lahitte

Jose,

After giving a quick overview to this code I found several issues or bad
practices if you prefer like using the same IV as the Key (bad idea indeed).
First of all, let me recommend a far more simple sample of this, here:
http://ncrypto.sourceforge.net. The CryptoHelper class will have the helper
code you might looking for.
Second and regarding the padding issue, I think that is not a good idea to
trim the padding info (or the trailing zeros) because this is part of the
normal block algorithm used in CBC mode and you will need this padding for
decryption. If you don't want a zero bytes padding, you might use the Pkcs7
padding mode. This will produce the same length blocks but with "random"
bytes instead of zeros.
After what I said, if you still want to know the block size, then you can
check the BlockSize property that will give you this info in bits. So if
your last block has a length of 5 bytes, then the remaining padding will be
of (BlockSize/8) - 5 bytes. If you want to know how many bytes have your
last block you might compute: plaintext.Length mod (BlockSize/8), where mod
is the modulus "%" operator.

Regards,

Hernan de Lahitte
Lagash Systems S.A.
http://weblogs.asp.net/hernandl


This posting is provided "AS IS" with no warranties, and confers no rights.

José Pérez Hernández said:
Hi,

I'm testing a Rijndael Symetric Algorithm Implementation to encrypt data.
With that intention, i made use of code that i saw in
http://www.derkeiler.com/Newsgroups...t.framework.aspnet.security/2003-03/0223.html
that encapsulates very good the process of encryption regardless of the
CryptoGraphic Service, in a class called SymmCrypto.

As you can see in the last chunk of code:

int i = 0;
for (i = 0; i < bytOut.Length; i++)
if (bytOut == 0)
break;

where byOut is the buffer with the encryption resultant data. The intention
is to trim the finnaly '\0' innecessary bytes.

But the matter is that i must to know where to cut, because the Rijndael
Decryption seems only to allows an amount of data, and produces a data block
of fixed size from which i'm interested just partially ( i do not want the
trailing zeros).

What is the relation between the size of the data block to encript, the key
size, and the size of the result (encrypted data block)?

In simple words.. given a key size, wich must to be the length of the data
to encrypt ?

Here is the code...

public string EncString(string Source, string Key)
{
byte[] bytIn = System.Text.ASCIIEncoding.ASCII.GetBytes(Source);

// create a MemoryStream so that the process can be done without I/O
files
System.IO.MemoryStream ms = new System.IO.MemoryStream();
byte[] bytKey = GetLegalKey(Key);

// set the private key
mobjCryptoService.Key = bytKey;
mobjCryptoService.IV = bytKey;

// create an Encryptor from the Provider Service instance
ICryptoTransform encrypto = mobjCryptoService.CreateEncryptor();

// create Crypto Stream that transforms a stream using the encryption
CryptoStream cs = new CryptoStream(ms, encrypto, CryptoStreamMode.Write);

// write out encrypted content into MemoryStream
cs.Write(bytIn, 0, bytIn.Length);
cs.FlushFinalBlock();

// get the output and trim the '\0' bytes
byte[] bytOut = ms.GetBuffer();
int i = 0;
for (i = 0; i < bytOut.Length; i++)
if (bytOut == 0)
break;
// convert into Base64 so that the result can be used in xml
return System.Convert.ToBase64String(bytOut, 0, i);
}

Regards, José.
 

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

Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top