Re: Encrypt a string to a string and vice versa
From: Jon Skeet [C# MVP] (skeet_at_pobox.com)
Date: 10/20/04
- Next message: Ignacio Machin \( .NET/ C# MVP \): "Re: newbie coming from vb: what happened to "with"? (found link )"
- Previous message: Ignacio Machin \( .NET/ C# MVP \): "Re: newbie coming from vb: what happened to "with"?"
- In reply to: Matthias S.: "Encrypt a string to a string and vice versa"
- Next in thread: Matthias S.: "Re: Encrypt a string to a string and vice versa"
- Reply: Matthias S.: "Re: Encrypt a string to a string and vice versa"
- Messages sorted by: [ date ] [ thread ]
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
- Next message: Ignacio Machin \( .NET/ C# MVP \): "Re: newbie coming from vb: what happened to "with"? (found link )"
- Previous message: Ignacio Machin \( .NET/ C# MVP \): "Re: newbie coming from vb: what happened to "with"?"
- In reply to: Matthias S.: "Encrypt a string to a string and vice versa"
- Next in thread: Matthias S.: "Re: Encrypt a string to a string and vice versa"
- Reply: Matthias S.: "Re: Encrypt a string to a string and vice versa"
- Messages sorted by: [ date ] [ thread ]
Relevant Pages
|