About Encryption ...

Ask a Question related to ASP.NET Security, Design and Development.

  1. #1

    Default 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

  2. Similar Questions and Discussions

    1. 128 Bit AES Encryption
      Hi All, Is it possible to implement 128 bit AES encryption in coldfusion 5.0? Regards cfdyn
    2. Encryption
      What 3rd party softwares are available for physically encrypting SQL server databases, either individual columns or rows? We have researched a few,...
    3. 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...
    4. 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...
    5. 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. ...
  3. #2

    Default 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...
    > 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é.
    >
    >
    >

    Hernan de Lahitte Guest

Posting Permissions

  • You may not post new threads
  • You may post replies
  • You may not post attachments
  • You may not edit your posts

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139