Re: Encrypt a string to a string and vice versa

From: Jon Skeet [C# MVP] (skeet_at_pobox.com)
Date: 10/20/04


Date: Wed, 20 Oct 2004 13:59:04 +0100

Matthias S. <matthias@_e_m_v_o_i_d_.de> wrote:
> I had a look at the vast information on encryption in the MSDN and got
> pretty confused. All I want to do is to encrypt a string into an encrypted
> string and later decrypt that (encrypted) string again to a human readable
> form. Can't be that difficult :).
>
> Could you send me please into the right direction. Thanks in advance.

The encryption libraries in .NET (like most encryption libraries) are
from binary to binary. So, you need to:

1) Convert your string to binary: use an Encoding and its GetBytes
method. I would suggest Encoding.UTF8.

2) Encrypt the binary data. Look at CryptoStream for some sample code.
You need to make sure you call FlushFinalBlock or Close - Dispose isn't
correctly implemented in CryptoStream.

3) Convert the resulting binary data into a string again. For this, I'd
suggest using Base64 - Convert.ToBase64String.

To decrypt, just reverse - use Convert.FromBase64String, then a
CryptoStream, then Encoding.UTF8.GetString(bytes).

-- 
Jon Skeet - <skeet@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too


Relevant Pages

  • Re: Byte array to string and back - newbie question
    ... // Create a symmetric algorithm. ... This is done to make encryption more ... // Encrypt a string into a string using a password ... // Decrypt a byte array into a byte array using a key and an IV ...
    (microsoft.public.dotnet.framework.aspnet.security)
  • Re: Using Python To Create An Encrypted Container
    ... an encrypted archive utility designed for secure archiving ... A match string allows you to only extract files matching a given ... Encrypt the string s using passwd and encryption cipher enc ...
    (comp.lang.python)
  • Re: How good an encryption algorithm is this?
    ... As long as the string can be converted to/from a byte stream, ... then you can apply that after the encryption. ... > So I decided to invent my own algorithm, and I just wanted anybody's> opinion on how secure this could be compared to the Win32 API version. ... > HCRYPTHASH hCryptHash; ...
    (microsoft.public.vc.language)
  • Length of the data to decrypt is invalid
    ... I found this code on a site for doing string encryption/decryption. ... // Create a symmetric algorithm. ... // This is done to make encryption more secure. ... // This will tell it that we have done our decryption ...
    (microsoft.public.dotnet.framework.aspnet)
  • Re: Web app security
    ... public static string EncryptToBase64String(string stringToEncrypt, ... we'd also like to move on to better encryption such ... to make the.NET AES encryption to work together with the AES encryption ... Microsoft MSDN Online Support Lead ...
    (microsoft.public.dotnet.framework.aspnet)

Loading