About Encryption ...

Discussion in 'ASP .Net Security' started by José Pérez Hernández, Apr 14, 2004.

  1. 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é.
     
    José Pérez Hernández, Apr 14, 2004
    #1
    1. Advertising

  2. 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" <> wrote in message
    news:...
    > 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é.
    >
    >
    >
     
    Hernan de Lahitte, Apr 14, 2004
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Robert Vabo

    Encryption

    Robert Vabo, Oct 8, 2003, in forum: ASP .Net
    Replies:
    1
    Views:
    1,297
    Ed Kaim [MSFT]
    Oct 14, 2003
  2. abhinav

    encryption algorithms

    abhinav, Dec 26, 2004, in forum: VHDL
    Replies:
    2
    Views:
    657
  3. Guenther Sohler

    Encryption Problem

    Guenther Sohler, Oct 17, 2003, in forum: Perl
    Replies:
    1
    Views:
    515
    Jim Gibson
    Oct 20, 2003
  4. Kelvin
    Replies:
    2
    Views:
    602
    Andrew Balmos (abalmos)
    Nov 9, 2004
  5. Acebravo

    About Encryption Question

    Acebravo, Mar 28, 2005, in forum: Perl
    Replies:
    1
    Views:
    2,717
    Jürgen Exner
    Mar 28, 2005
Loading...

Share This Page