Ask a Question related to ASP.NET Security, Design and Development.
-
José Pérez Hernández #1
About Encryption ...
Hi,
I'm testing a Rijndael Symetric Algorithm Implementation to encrypt data.
With that intention, i made use of code that i saw in
[url]http://www.derkeiler.com/Newsgroups/microsoft.public.dotnet.framework.aspnet.security/2003-03/0223.html[/url]
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[i] == 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[i] == 0)
break;
// convert into Base64 so that the result can be used in xml
return System.Convert.ToBase64String(bytOut, 0, i);
}
Regards, José.
José Pérez Hernández Guest
-
128 Bit AES Encryption
Hi All, Is it possible to implement 128 bit AES encryption in coldfusion 5.0? Regards cfdyn -
Encryption
What 3rd party softwares are available for physically encrypting SQL server databases, either individual columns or rows? We have researched a few,... -
Encryption using X.509
I have writen a class that uses certificates to encrypt data on my web servers prior to storing it in a database and would like to know if there... -
URL Encryption
Hi, Is there a way to securely ecrypt and decrypte a URL? for e.g encrypting and decrypting using Triple-DES algorithm. Are there any security... -
IDS 9.40.UC2, Encryption
I try to configure encryption with IDS 9.40.UC2 on an AIX platform and I'm a little bit confused about the different error messages I receive. ... -
Hernan de Lahitte #2
Re: About Encryption ...
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:
[url]http://ncrypto.sourceforge.net[/url]. 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.
[url]http://weblogs.asp.net/hernandl[/url]
This posting is provided "AS IS" with no warranties, and confers no rights.
"José Pérez Hernández" <joseperhe@yahoo.com> wrote in message
news:O9qdjmbIEHA.3840@TK2MSFTNGP11.phx.gbl...[url]http://www.derkeiler.com/Newsgroups/microsoft.public.dotnet.framework.aspnet.security/2003-03/0223.html[/url]> Hi,
>
> I'm testing a Rijndael Symetric Algorithm Implementation to encrypt data.
> With that intention, i made use of code that i saw in
>intention> 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[i] == 0)
> break;
>
> where byOut is the buffer with the encryption resultant data. Theblock> 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 datakey> 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, theCryptoStreamMode.Write);> 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,>
> // 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[i] == 0)
> break;
> // convert into Base64 so that the result can be used in xml
> return System.Convert.ToBase64String(bytOut, 0, i);
> }
>
> Regards, José.
>
>
>
Hernan de Lahitte Guest



Reply With Quote

