Re: TripleDES Encryption issue

From: Cowboy \(Gregory A Beamer\) (NoSpamMgbworld_at_comcast.netNoSpamM)
Date: 09/06/03

  • Next message: Sam Page: "Thread Unable to Impersonate (Workaround)"
    Date: Sat, 6 Sep 2003 12:08:40 -0500
    
    

    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
    > kingcrim@earthlink.net
    >
    

  • Next message: Sam Page: "Thread Unable to Impersonate (Workaround)"

    Relevant Pages

    • Re: Encrypt and Decrypt a file using .NET 2.0?
      ... public static string GenerateKey() ... DES des = DES.Create; ... // Distribute this key to the user who will decrypt this file. ... // Get the Key for the file to Encrypt. ...
      (microsoft.public.dotnet.languages.csharp)
    • Re: .NET Crypto Classes Interoperability with Win32 Crypto APIs
      ... >the hash of the string you are feeding in. ... when i encrypt a string using .NET classes and try ... >> decrypt it using Win32 APIs, ...
      (microsoft.public.dotnet.security)
    • Re: Encrypt and Decrypt a file using .NET 2.0?
      ... public static string GenerateKey() ... using (CryptoStream cryptoStream = new CryptoStream(outFile, ... // Distribute this key to the user who will decrypt this file. ... // Get the Key for the file to Encrypt. ...
      (microsoft.public.dotnet.languages.csharp)
    • RE: 2007 User Level Security
      ... encrypted string. ... the way I use it is to encrypt user names and passwords and store the ... Dim prp As Property ... Dim dbs As Object, prp As Variant ...
      (microsoft.public.access.modulesdaovba)
    • Re: Workable encryption in Tcl??
      ... The longest string would be about the length ... How to encrypt a string ... nothing about binary formats. ... ># Perform ECB mode encryption on a plaintext block of BINARY data. ...
      (comp.lang.tcl)