472,143 Members | 1,161 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,143 software developers and data experts.

Encrypt a string to a string and vice versa

Hi,

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.

--

kind regards,

matthias

--

I love deadlines. I like the whooshing sound they make as they fly by.
[Douglas Adams]
Nov 16 '05 #1
7 26614
Hi Matthias,

I haven't really done this myself only implement the classic ROT13
scrambler\descramber but check out,

http://www.dotnetspider.com/Technology/KBPages/611.aspx

http://www.dotnet247.com/247reference/msgs/5/28421.aspx

Hope this gives you some direction. One thing you should consider about
deadlines as well, if it wasn't for the last minute, nothing would get done.
Hope this assist you.

SpotNet.

"Matthias S." <matthias@_e_m_v_o_i_d_.de> wrote in message
news:%2****************@tk2msftngp13.phx.gbl...
Hi,

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.

--

kind regards,

matthias

--

I love deadlines. I like the whooshing sound they make as they fly by.
[Douglas Adams]

Nov 16 '05 #2
Hi Matthias,

..NET provides cryptographics services in class contained in the
System.Security.Cryptography namespace.

First, you need to decide on which type of encryption you would like to use
- basically, there are three: symmetric, asymmetric, and hashing. From what
you said, you should go for either of the first two. Go for symmetric for
more performance, and asymmetric if you need more security.

Once that is decided, you create an instance of the required
CryptoServiceProvider in the required class. For example,
System.Security.Cryptography.RSACryptoServiceProvi der which derives from
System.Security.Cryptography.AsymmetricAlgorithm and then use the required
method (eg. Enrypt). Note that most of these method accept a Stream or an
array of bytes, so you will have to use UTF8 encoding class to convert your
string.

Check out this MSDN link for a detailed implementation
http://msdn.microsoft.com/library/en...yptingdata.asp

Let me know if I could help more.

HTH,
Rakesh Rajan



"Matthias S." wrote:
Hi,

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.

--

kind regards,

matthias

--

I love deadlines. I like the whooshing sound they make as they fly by.
[Douglas Adams]

Nov 16 '05 #3
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 - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #4
hi john,

thanks for your reply which was of big help. So far I managed to encrypt
the string but running into problems when decrypting:

Here is how I encrypt:
+++
// sSource contains the string to be encrypted
UTF8Encoding utf8 = new UTF8Encoding();
byte[] utf8Bytes = utf8.GetBytes(sSource);

// encrypt
SymmetricAlgorithm rijn = SymmetricAlgorithm.Create();
MemoryStream ms = new MemoryStream();
CryptoStream cs = new CryptoStream(ms, rijn.CreateEncryptor(),
CryptoStreamMode.Write);

for (int i = 0; i < utf8Bytes.Length; i++) {
cs.WriteByte(utf8Bytes[i]);
}

cs.Flush();
// return the encrypted string
return Convert.ToBase64String(ms.ToArray());
+++

it seems to work fine. I will be knowing it once I've managed to decrypt
the whole thing again.

Here is how I'd like to decrypt:
+++
SymmetricAlgorithm rijn = SymmetricAlgorithm.Create();
MemoryStream ms = new MemoryStream(Convert.FromBase64String(sSource));
CryptoStream cs = new CryptoStream(ms, rijn.CreateDecryptor(),
CryptoStreamMode.Read);

for (int i = 0; i < ms.Length; i++) {
// the next line with throw a CryptographicException with the
// message: "Padding is invalid and can not be removed."
cs.ReadByte();
}
+++

Besides that I figured that the CryptoStream does not support a Length
property. So how am I can I get my cs variable into a byte[]?

Thanks for any help again!
kind regards,

matthias

--

I love deadlines. I like the whooshing sound they make as they fly by.
[Douglas Adams]

Jon Skeet [C# MVP] wrote:
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).

Nov 16 '05 #5
Matthias S. <matthias@_e_m_v_o_i_d_.de> wrote:
thanks for your reply which was of big help. So far I managed to encrypt
the string but running into problems when decrypting:

Here is how I encrypt:
+++
// sSource contains the string to be encrypted
UTF8Encoding utf8 = new UTF8Encoding();
byte[] utf8Bytes = utf8.GetBytes(sSource);

// encrypt
SymmetricAlgorithm rijn = SymmetricAlgorithm.Create();
MemoryStream ms = new MemoryStream();
CryptoStream cs = new CryptoStream(ms, rijn.CreateEncryptor(),
CryptoStreamMode.Write);

for (int i = 0; i < utf8Bytes.Length; i++) {
cs.WriteByte(utf8Bytes[i]);
}

cs.Flush();


You're neither calling Close() nor FlushFinalBlock() on the
CryptoStream. That may well be the problem.

By the way: using ReadByte and WriteByte is a pretty painful way of
reading and writing data. Use the forms which read and write blocks of
data at a time, paying attention to the return value from Read.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #6
hi john,

sorry if I didn't make myself clear enough. The encryption method works
fine (at least I don't get an exception anywhere, if the string is
correctly encryption is left aside). The problem lies in the
Decryption-part of the task, where I'm just creating a MemoryStream by
converting the result of my previously mentioned encryption method using
Convert.FromBase64String().

The problem is, as soon as I call ReadByte on the CryptoStream, I get the
CryptographicException mentioned below.

+++
SymmetricAlgorithm rijn = SymmetricAlgorithm.Create();
MemoryStream ms = new MemoryStream(Convert.FromBase64String(sSource));
CryptoStream cs = new CryptoStream(ms, rijn.CreateDecryptor(),
CryptoStreamMode.Read);

for (int i = 0; i < ms.Length; i++) {
// the next line with throw a CryptographicException with the
// message: "Padding is invalid and can not be removed."
cs.ReadByte();
+++

As to your answer on the Read-Method, I actually don't know how to Read
*anything* from the stream if I can't retrieve the current Position or
Length (both not provided in the CryptoStream).

btw, big thanks to you for helping me out on this one.

kind regards,

matthias

--

I love deadlines. I like the whooshing sound they make as they fly by.
[Douglas Adams]

Jon Skeet [C# MVP] wrote:
Matthias S. <matthias@_e_m_v_o_i_d_.de> wrote:
thanks for your reply which was of big help. So far I managed to encrypt
the string but running into problems when decrypting:

Here is how I encrypt:
+++
// sSource contains the string to be encrypted
UTF8Encoding utf8 = new UTF8Encoding();
byte[] utf8Bytes = utf8.GetBytes(sSource);

// encrypt
SymmetricAlgorithm rijn = SymmetricAlgorithm.Create();
MemoryStream ms = new MemoryStream();
CryptoStream cs = new CryptoStream(ms, rijn.CreateEncryptor(),
CryptoStreamMode.Write);

for (int i = 0; i < utf8Bytes.Length; i++) {
cs.WriteByte(utf8Bytes[i]);
}

cs.Flush();

You're neither calling Close() nor FlushFinalBlock() on the
CryptoStream. That may well be the problem.

By the way: using ReadByte and WriteByte is a pretty painful way of
reading and writing data. Use the forms which read and write blocks of
data at a time, paying attention to the return value from Read.

Nov 16 '05 #7
Matthias S. <matthias@_e_m_v_o_i_d_.de> wrote:
sorry if I didn't make myself clear enough. The encryption method works
fine (at least I don't get an exception anywhere, if the string is
correctly encryption is left aside). The problem lies in the
Decryption-part of the task, where I'm just creating a MemoryStream by
converting the result of my previously mentioned encryption method using
Convert.FromBase64String().

The problem is, as soon as I call ReadByte on the CryptoStream, I get the
CryptographicException mentioned below.


The problem is that the encryption *hasn't* worked - you've not got all
the data. You didn't get an exception, but you didn't get the right
data, either.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #8

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

19 posts views Thread by Espen Ruud Schultz | last post: by
2 posts views Thread by Steve - DND | last post: by
1 post views Thread by Eugene Anthony | last post: by
16 posts views Thread by Hugh Janus | last post: by
6 posts views Thread by =?Utf-8?B?TFBldGVy?= | last post: by
1 post views Thread by Maric Michaud | last post: by
reply views Thread by leo001 | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.