473,386 Members | 1,674 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,386 software developers and data experts.

string encryption

I need a way of encrypting strings that provides a fairly high level of
encryption. I've looked at System.Security.Cryptography at
http://msdn.microsoft.com/library/de.../en-us/cpref/h
tml/frlrfsystemsecuritycryptography.asp, but there are so many different
algorithms I don't know which one would be most suitable. Also some
sample code would be very helpful.

Cheers,

Mike

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 16 '05 #1
2 1975
For a sample: http://www.codeproject.com/dotnet/en...decryption.asp
http://www.developer.com/net/net/article.php/3077901
http://aspnet.4guysfromrolla.com/articles/103002-1.aspx

"Mike P" <mr*@telcoelectronics.co.uk> wrote in message
news:eG**************@TK2MSFTNGP11.phx.gbl...
I need a way of encrypting strings that provides a fairly high level of
encryption. I've looked at System.Security.Cryptography at
http://msdn.microsoft.com/library/de.../en-us/cpref/h
tml/frlrfsystemsecuritycryptography.asp, but there are so many different
algorithms I don't know which one would be most suitable. Also some
sample code would be very helpful.

Cheers,

Mike

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 16 '05 #2
You need an 'Initialization vector'. My method below generates it on
encrypting, and you have to supply the same one back when decrypting, aswell
as the same key.
Encryption basically works on byte streams, so I have to use unicode
encoding to convert it to a string. This is apparently the best encoding in
terms of displayability and round-trip-ability - although I'm not sure how
UTF8 rates. I'm not that hot on my encoding technology, perhaps somebody else
could explain this...

public static string EncryptString(string raw, string key, out string iv)
{
byte[] b_raw = Encoding.Unicode.GetBytes(raw),
b_key = Convert.FromBase64String(key),
b_iv = new byte[16],
b_int = new byte[64];
RNGCryptoServiceProvider rng = new RNGCryptoServiceProvider();
rng.GetBytes(b_iv);
MemoryStream ms_raw = new MemoryStream(b_raw, false),
ms_enc = new MemoryStream();
SymmetricAlgorithm sa = SymmetricAlgorithm.Create();
ICryptoTransform ict = sa.CreateEncryptor(b_key, b_iv);
CryptoStream cs = new CryptoStream(ms_enc, ict, CryptoStreamMode.Write);

int totread = 0;
while(totread < ms_raw.Length)
{
int read = ms_raw.Read(b_int, 0, 64);
cs.Write(b_int, 0, read);
totread += read;
}
cs.Close();
iv = Convert.ToBase64String(b_iv);
byte[] b_enc = ms_enc.ToArray();
return Convert.ToBase64String(b_enc);
}

public static string DecryptString(string enc, string key, string iv)
{
byte[] b_enc = Convert.FromBase64String(enc),
b_key = Convert.FromBase64String(key),
b_iv = Convert.FromBase64String(iv),
b_int = new byte[64];
MemoryStream ms_enc = new MemoryStream(b_enc, false),
ms_raw = new MemoryStream();
SymmetricAlgorithm sa = SymmetricAlgorithm.Create();
CryptoStream cs = new CryptoStream(ms_enc, sa.CreateDecryptor(b_key, b_iv),
CryptoStreamMode.Read);
int totread = 0;
while(totread < ms_enc.Length)
{
int read = cs.Read(b_int, 0, 64);
if(read == 0) break;
ms_raw.Write(b_int, 0, read);
totread += read;
}
return Encoding.Unicode.GetString(ms_raw.ToArray());
}
"Mike P" wrote:
I need a way of encrypting strings that provides a fairly high level of
encryption. I've looked at System.Security.Cryptography at
http://msdn.microsoft.com/library/de.../en-us/cpref/h
tml/frlrfsystemsecuritycryptography.asp, but there are so many different
algorithms I don't know which one would be most suitable. Also some
sample code would be very helpful.

Cheers,

Mike

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!

Nov 16 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Nathan | last post by:
Is there a way to convert a string to a CipherMessage? I am calling a function that decrypts a CipherMessage and returns the value. The only problem is when I want to use an encrypted value stored...
14
by: msnews.microsoft.com | last post by:
How can I encrypt and decrypt string?
7
by: Matthias S. | last post by:
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)...
12
by: Charlie | last post by:
Hi: My host will not allow me use a trusted connection or make registry setting, so I'm stuck trying find a way to hide connection string which will be stored in web.config file. If I encrypt...
6
by: larry mckay | last post by:
Hi, Does anyone have any simple text string encryption routines that are easy to implement? I'm trying to prevent users and system administrators from figuring out how I implement things....
14
by: WebMatrix | last post by:
Hello, I have developed a web application that connects to 2 different database servers. The connection strings with db username + password are stored in web.config file. After a code review,...
4
by: JJ | last post by:
I need to encrypt credit card # and store that in a database (and be able to decrypt it). Any codes that use strong encyption algorithm like AES 256 on the web that I can copy and paste? Thanks
8
by: Jeremy Kitchen | last post by:
I have encoded a string into Base64 for the purpose of encryption. I then later decrypted it and converted it back from Base64 the final string returns with four nothing characters. "pass" what...
22
by: j1mb0jay | last post by:
I have had to create a simple string encryption program for coursework, I have completed the task and now have to do a write up on how it could be improved at a later date. If you could look...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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
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: 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
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.