TripleDES Encryption issue

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

  1. #1

    Default TripleDES Encryption issue

    I'm new to using this part of the framework, so I'm hoping I've done
    something obviously stupid, which someone will be able to point out in
    an obvious manner.

    Most of the samples I've seen involved encrypting and decrypting to and
    from a file, but that's not what I want. I want to be able to insert a
    string into an encryption function which outputs that encrypted string,
    which could then be sent into a decryption function and spit out the
    original string.

    As you might expect, ultimately, I want to be able to put this string
    into a database table.

    Anyways, I set up a simple test page where I enter a string in a
    textbox, and then run it through an encrypt, then decrypt, function, and
    print out to some labels to make sure it is doing it correctly.

    The encrypt part seems to work (at least the function doesn't fail), but
    the decrypt function fails, with an error saying that the length of the
    encrypted string is invalid. Here are the functions:

    ************************************************** ************

    public static string Encrypt(string StringToEncrypt){
    string EncryptedString;
    UTF8Encoding utf8encoder = new UTF8Encoding();
    byte[] inputInBytes = utf8encoder.GetBytes(StringToEncrypt);
    TripleDESCryptoServiceProvider tdesProvider = new
    TripleDESCryptoServiceProvider();
    ICryptoTransform cryptoTransform =
    tdesProvider.CreateEncryptor(tdeskey, tdesIV);
    MemoryStream encryptedStream = new MemoryStream();
    CryptoStream cryptStream = new CryptoStream(encryptedStream,
    cryptoTransform, CryptoStreamMode.Write);

    cryptStream.Write(inputInBytes, 0, inputInBytes.Length);
    cryptStream.FlushFinalBlock();
    encryptedStream.Position = 0;

    byte[] result = new byte[encryptedStream.Length - 1];
    encryptedStream.Read(result, 0, (int)(encryptedStream.Length - 1));
    cryptStream.Close();

    UTF8Encoding myutf = new UTF8Encoding();
    EncryptedString = myutf.GetString(result);
    return EncryptedString;
    }//end Encrypt

    public static string Decrypt(string StringToDecrypt){
    string DecryptedString;


    UTF8Encoding utf8encoder = new UTF8Encoding();
    byte[] inputInBytes = utf8encoder.GetBytes(StringToDecrypt);
    TripleDESCryptoServiceProvider tdesProvider = new
    TripleDESCryptoServiceProvider();
    ICryptoTransform cryptoTransform =
    tdesProvider.CreateDecryptor(tdeskey, tdesIV);
    MemoryStream decryptedStream = new MemoryStream();
    CryptoStream cryptStream = new CryptoStream(decryptedStream,
    cryptoTransform, CryptoStreamMode.Write);

    cryptStream.Write(inputInBytes, 0, (inputInBytes.Length));
    cryptStream.FlushFinalBlock();
    decryptedStream.Position = 0;

    byte[] result = new byte[decryptedStream.Length - 1];
    decryptedStream.Read(result, 0, (int)(decryptedStream.Length - 1));
    cryptStream.Close();

    DecryptedString = result.ToString();

    return DecryptedString;
    }//end Decrypt

    ************************************************** **********************

    cryptStream.FlushFinalBlock() in the decrypt function is what fails.

    Any ideas would be greatly welcomed.

    jdn
    [email]kingcrim@earthlink.net[/email]

    jdn 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. ASP .Net Encryption
      Hi I want to hide my soure codes of ASP .NET. I heard that with encryption it can be managed. Could anyone give me an url for this topic let...
    3. Encryption
      What 3rd party softwares are available for physically encrypting SQL server databases, either individual columns or rows? We have researched a few,...
    4. encrypt with TripleDES, decrypt with DES, doesnt work ?
      Hi I have such a problem - I've tried to encrypt a file with TripleDES and then decrypt it with DES, but failed. AFAIK, TripleDES uses 3x DES...
    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: TripleDES Encryption issue

    Change the stream type. The encryption samples use a file stream, but any
    type of stream can be used to encrypt/decrypt. I have seen plenty of samples
    with strings, as well as files, but you are correct that many of the samples
    are file based.

    Hope this helps.

    --
    Gregory A. Beamer
    MPV; MCP: +I, SE, SD, DBA

    ************************************************** ********************
    Think outside the box!
    ************************************************** ********************
    "jdn" <kingcrim@earthlink.net> wrote in message
    news:uAMyKjEdDHA.904@TK2MSFTNGP11.phx.gbl...
    > I'm new to using this part of the framework, so I'm hoping I've done
    > something obviously stupid, which someone will be able to point out in
    > an obvious manner.
    >
    > Most of the samples I've seen involved encrypting and decrypting to and
    > from a file, but that's not what I want. I want to be able to insert a
    > string into an encryption function which outputs that encrypted string,
    > which could then be sent into a decryption function and spit out the
    > original string.
    >
    > As you might expect, ultimately, I want to be able to put this string
    > into a database table.
    >
    > Anyways, I set up a simple test page where I enter a string in a
    > textbox, and then run it through an encrypt, then decrypt, function, and
    > print out to some labels to make sure it is doing it correctly.
    >
    > The encrypt part seems to work (at least the function doesn't fail), but
    > the decrypt function fails, with an error saying that the length of the
    > encrypted string is invalid. Here are the functions:
    >
    > ************************************************** ************
    >
    > public static string Encrypt(string StringToEncrypt){
    > string EncryptedString;
    > UTF8Encoding utf8encoder = new UTF8Encoding();
    > byte[] inputInBytes = utf8encoder.GetBytes(StringToEncrypt);
    > TripleDESCryptoServiceProvider tdesProvider = new
    > TripleDESCryptoServiceProvider();
    > ICryptoTransform cryptoTransform =
    > tdesProvider.CreateEncryptor(tdeskey, tdesIV);
    > MemoryStream encryptedStream = new MemoryStream();
    > CryptoStream cryptStream = new CryptoStream(encryptedStream,
    > cryptoTransform, CryptoStreamMode.Write);
    >
    > cryptStream.Write(inputInBytes, 0, inputInBytes.Length);
    > cryptStream.FlushFinalBlock();
    > encryptedStream.Position = 0;
    >
    > byte[] result = new byte[encryptedStream.Length - 1];
    > encryptedStream.Read(result, 0, (int)(encryptedStream.Length - 1));
    > cryptStream.Close();
    >
    > UTF8Encoding myutf = new UTF8Encoding();
    > EncryptedString = myutf.GetString(result);
    > return EncryptedString;
    > }//end Encrypt
    >
    > public static string Decrypt(string StringToDecrypt){
    > string DecryptedString;
    >
    >
    > UTF8Encoding utf8encoder = new UTF8Encoding();
    > byte[] inputInBytes = utf8encoder.GetBytes(StringToDecrypt);
    > TripleDESCryptoServiceProvider tdesProvider = new
    > TripleDESCryptoServiceProvider();
    > ICryptoTransform cryptoTransform =
    > tdesProvider.CreateDecryptor(tdeskey, tdesIV);
    > MemoryStream decryptedStream = new MemoryStream();
    > CryptoStream cryptStream = new CryptoStream(decryptedStream,
    > cryptoTransform, CryptoStreamMode.Write);
    >
    > cryptStream.Write(inputInBytes, 0, (inputInBytes.Length));
    > cryptStream.FlushFinalBlock();
    > decryptedStream.Position = 0;
    >
    > byte[] result = new byte[decryptedStream.Length - 1];
    > decryptedStream.Read(result, 0, (int)(decryptedStream.Length - 1));
    > cryptStream.Close();
    >
    > DecryptedString = result.ToString();
    >
    > return DecryptedString;
    > }//end Decrypt
    >
    > ************************************************** **********************
    >
    > cryptStream.FlushFinalBlock() in the decrypt function is what fails.
    >
    > Any ideas would be greatly welcomed.
    >
    > jdn
    > [email]kingcrim@earthlink.net[/email]
    >

    Cowboy \(Gregory A Beamer\) Guest

  4. #3

    Default Re: TripleDES Encryption issue

    Cowboy (Gregory A Beamer) wrote:
    > Change the stream type. The encryption samples use a file stream, but any
    > type of stream can be used to encrypt/decrypt. I have seen plenty of samples
    > with strings, as well as files, but you are correct that many of the samples
    > are file based.
    >
    > Hope this helps.
    >
    Change the stream type to what?

    Thanks.

    jdn

    jdn Guest

  5. #4

    Default Re: TripleDES Encryption issue

    Cowboy (Gregory A Beamer) wrote:
    > Change the stream type. The encryption samples use a file stream, but any
    > type of stream can be used to encrypt/decrypt. I have seen plenty of samples
    > with strings, as well as files, but you are correct that many of the samples
    > are file based.
    >
    > Hope this helps.
    >

    Why would changing the stream type fix the issue? It seems to have to
    do with how the decrypt function is written or maybe with how the
    encrypt function writes the string.

    jdn

    jdn Guest

  6. #5

    Default RE: TripleDES Encryption issue

    Hi Jdn,

    I replied the following post in the
    microsoft.public.dotnet.framework.aspnet group. It is the same issue as
    this thread. Please check it when you have time.

    "TripleDES Encryption issue"

    If I have misunderstood your concern, please feel free to let me know.

    Best regards,

    Jacob Yang
    Microsoft Online Partner Support
    <MCSD>
    Get Secure! ¨C [url]www.microsoft.com/security[/url]
    This posting is provided "as is" with no warranties and confers no rights.

    Jacob Yang [MSFT] Guest

  7. #6

    Default Re: TripleDES Encryption issue

    There are several kinds of streams, IO streams, memory streams, XML streams.. etc.. so if you are dealing with a filestream - then you will only really be able to read/write from a file.. if you want to read/write to a variable, you likely need a MemoryStream or some equivalent..


    "jdn" <kingcrim@earthlink.net> wrote in message news:uTuNAWYdDHA.2932@tk2msftngp13.phx.gbl...
    Cowboy (Gregory A Beamer) wrote:
    > Change the stream type. The encryption samples use a file stream, but any
    > type of stream can be used to encrypt/decrypt. I have seen plenty of samples
    > with strings, as well as files, but you are correct that many of the samples
    > are file based.
    >
    > Hope this helps.
    >

    Why would changing the stream type fix the issue? It seems to have to
    do with how the decrypt function is written or maybe with how the
    encrypt function writes the string.

    jdn

    Drebin Guest

  8. #7

    Default Re: TripleDES Encryption issue

    Drebin wrote:
    > There are several kinds of streams, IO streams, memory streams, XML streams.. etc.. so if you are dealing with a filestream - then you will only really be able to read/write from a file.. if you want to read/write to a variable, you likely need a MemoryStream or some equivalent..
    >
    >
    > "jdn" <kingcrim@earthlink.net> wrote in message news:uTuNAWYdDHA.2932@tk2msftngp13.phx.gbl...
    > Cowboy (Gregory A Beamer) wrote:
    >
    >
    >>Change the stream type. The encryption samples use a file stream, but any
    >>type of stream can be used to encrypt/decrypt. I have seen plenty of samples
    >>with strings, as well as files, but you are correct that many of the samples
    >>are file based.
    >>
    >>Hope this helps.
    >>
    >
    >
    >
    > Why would changing the stream type fix the issue? It seems to have to
    > do with how the decrypt function is written or maybe with how the
    > encrypt function writes the string.
    >
    > jdn
    >
    No, I know that. Since I am trying to Encrypt and Decrypt using
    MemoryStream and CryptoStream both places, I don't see how changing that
    will solve the issue. Though maybe I'm missing something.

    jdn 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