473,392 Members | 1,319 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,392 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 26730
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

19
by: Espen Ruud Schultz | last post by:
Lets say I have a char pointer and an std::string. Is it possible to get a pointer to the std::string's "content" so that the char pointer can point to the same text? And vice versa; can I give...
1
by: Dharmendra Singh | last post by:
Hi I'm using .Net(C#) and working on the form(Screen) which have text boxes for both arabic and english data to store. So i want to change the language at run time from arabic to english and...
2
by: Steve - DND | last post by:
Just wondering if anyone out there has any code to convert a plural word to it's singular form and vice versa. Most of our database tables are named in a plural fashion. When we go to create...
1
by: Eugene Anthony | last post by:
Private Function BStr2UStr(BStr) 'Byte string to Unicode string conversion Dim lngLoop BStr2UStr = "" For lngLoop = 1 to LenB(BStr) BStr2UStr = BStr2UStr & Chr(AscB(MidB(BStr,lngLoop,1))) Next...
16
by: Hugh Janus | last post by:
Hi all, I am using the below functions in order to convert strings to bytes and vice versa. I totally ans shamefully stole these functions from this group btw! Anyway, they work great but as...
6
yabansu
by: yabansu | last post by:
Hi all, I think most of you probably know the two .NET framework functions, namely Encoding.GetBytes(string) and Encoding.GetString(byte), to convert string into byte array and vice versa. Now,...
6
by: =?Utf-8?B?TFBldGVy?= | last post by:
Hi, I would copy the characters of a string variable to a fixed character buffer in a struct (and vice versa) in C#. public struct S { ... public fixed char cBuff; ...
1
by: Maric Michaud | last post by:
Le Tuesday 24 June 2008 07:08:46 swapna mudavath, vous avez écrit : This is not valid xml, there is no commas in attribute list in xml. You could try with minidom if your xml stream isn't too...
0
by: FarooqRafiq | last post by:
Hi, My requirement is that a string is encrypted in VB.NET and sent to PHP, PHP decrypts the string (till here the logic is working) and then the PHP should encrtyp (where i am having problems)...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

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.