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

Simple Encryption and Decryption

Hi,
I would like to enrypt and decrypt a simple line of text, with a
private(symmetric) key.

I have tried searching in the System.Cryptography class, but I can't
find a simple way of doing this job.

Can someone recommend me an easy way on how to do it.
Thanks in Advance
Nov 16 '05 #1
14 6538
"Xarky" <be*********@yahoo.com> wrote in message
news:bc*************************@posting.google.co m...
Hi,
I would like to enrypt and decrypt a simple line of text, with a
private(symmetric) key.

I have tried searching in the System.Cryptography class, but I can't
find a simple way of doing this job.

Can someone recommend me an easy way on how to do it.
Thanks in Advance


There is a Microsoft way to do this. However you can also do it
naively, since every character is representable by an ascii number,
your line of text evaluates to a line of numbers, on which you can
let loose some algorithm to scramble it.
Nov 16 '05 #2
Zach <wa**********@all.here> wrote:
"Xarky" <be*********@yahoo.com> wrote in message
news:bc*************************@posting.google.co m...
Hi,
I would like to enrypt and decrypt a simple line of text, with a
private(symmetric) key.

I have tried searching in the System.Cryptography class, but I can't
find a simple way of doing this job.

Can someone recommend me an easy way on how to do it.
Thanks in Advance


There is a Microsoft way to do this. However you can also do it
naively, since every character is representable by an ascii number,
your line of text evaluates to a line of numbers, on which you can
let loose some algorithm to scramble it.


No, *not* every character is representable by an ascii number. ASCII
has no Unicode values over 127, whereas there are *loads* of Unicode
characters over 127...

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #3
"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om...
Zach <wa**********@all.here> wrote:
"Xarky" <be*********@yahoo.com> wrote in message
news:bc*************************@posting.google.co m...
Hi,
I would like to enrypt and decrypt a simple line of text, with a
private(symmetric) key.

I have tried searching in the System.Cryptography class, but I can't
find a simple way of doing this job.

Can someone recommend me an easy way on how to do it.
Thanks in Advance


There is a Microsoft way to do this. However you can also do it
naively, since every character is representable by an ascii number,
your line of text evaluates to a line of numbers, on which you can
let loose some algorithm to scramble it.


No, *not* every character is representable by an ascii number. ASCII
has no Unicode values over 127, whereas there are *loads* of Unicode
characters over 127...


Oh dear, the OP is talking about "a simple line of text".
Nov 16 '05 #4
Xarky,

Check out any of the classes that derive from
System.Security.Cryptography.SymmetricAlgorithm.

Regards,
Joakim

Xarky wrote:
Hi,
I would like to enrypt and decrypt a simple line of text, with a
private(symmetric) key.

I have tried searching in the System.Cryptography class, but I can't
find a simple way of doing this job.

Can someone recommend me an easy way on how to do it.
Thanks in Advance

Nov 16 '05 #5
"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om...
Zach <wa**********@all.here> wrote:
"Xarky" <be*********@yahoo.com> wrote in message
news:bc*************************@posting.google.co m...
Hi,
I would like to enrypt and decrypt a simple line of text, with a
private(symmetric) key.

I have tried searching in the System.Cryptography class, but I can't
find a simple way of doing this job.

Can someone recommend me an easy way on how to do it.
Thanks in Advance


There is a Microsoft way to do this. However you can also do it
naively, since every character is representable by an ascii number,
your line of text evaluates to a line of numbers, on which you can
let loose some algorithm to scramble it.


No, *not* every character is representable by an ascii number. ASCII
has no Unicode values over 127, whereas there are *loads* of Unicode
characters over 127...

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


If thew OPs line of text is in Unified Canadian Aboriginal Syllabics
this is where he will find the unicode
http://www.geocities.com/Athens/Acad.../fontset.htm#a

Nov 16 '05 #6
Zach <wa**********@all.here> wrote:
No, *not* every character is representable by an ascii number. ASCII
has no Unicode values over 127, whereas there are *loads* of Unicode
characters over 127...


Oh dear, the OP is talking about "a simple line of text".


So are you 100% positive that a "simple line of text" can't contain any
non-ASCII characters? That's a view which is very much biased towards
the English-speaking world.

Basically, assuming that ASCII is good enough for text files is a
recipe for disaster unless you have specifications which you can
guarantee won't change to allow "real" text. (It's usually good enough
for header names, but not always header values, for instance.)

There are perfectly good ways of encrypting text without making such
assumptions - just using UTF-8 instead of ASCII to convert the text
data into binary data would be my suggestion.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #7
I have written an article that may help you:

http://www.dalepreston.com/Blog/Arch...0_Archive.html

DalePres
MCAD, MCDBA, MCSE

"Xarky" <be*********@yahoo.com> wrote in message
news:bc*************************@posting.google.co m...
Hi,
I would like to enrypt and decrypt a simple line of text, with a
private(symmetric) key.

I have tried searching in the System.Cryptography class, but I can't
find a simple way of doing this job.

Can someone recommend me an easy way on how to do it.
Thanks in Advance

Nov 16 '05 #8
DalePres <don-t-spa-m-me@lea-ve-me-a-lone--.com> wrote:
I have written an article that may help you:

http://www.dalepreston.com/Blog/Arch...0_Archive.html


Unfortunately that makes the same mistake that Zach did - assuming that
all text is ASCII.

I suggest using UTF-8 instead. Just use Encoding.UTF8.GetBytes instead
of Encoding.ASCII. (I'm also not sure why you're using Encoding.ASCII
in one place and manually creating an instance of ASCIIEncoding in
another.)

Also note that the call to Substring at the end isn't required if you
do things properly - don't assume that the decrypted data is the same
length as the encrypted data, but read the whole of the stream instead.
(Also don't assume that a single call to Stream.Read will do everything
you want it to. See http://www.pobox.com/~skeet/csharp/readbinary.html
for more on this.)

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

DalePres

"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om...
DalePres <don-t-spa-m-me@lea-ve-me-a-lone--.com> wrote:
I have written an article that may help you:

http://www.dalepreston.com/Blog/Arch...0_Archive.html


Unfortunately that makes the same mistake that Zach did - assuming that
all text is ASCII.

I suggest using UTF-8 instead. Just use Encoding.UTF8.GetBytes instead
of Encoding.ASCII. (I'm also not sure why you're using Encoding.ASCII
in one place and manually creating an instance of ASCIIEncoding in
another.)

Also note that the call to Substring at the end isn't required if you
do things properly - don't assume that the decrypted data is the same
length as the encrypted data, but read the whole of the stream instead.
(Also don't assume that a single call to Stream.Read will do everything
you want it to. See http://www.pobox.com/~skeet/csharp/readbinary.html
for more on this.)

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

Nov 16 '05 #10
Again, thanks for the feedback on my article. I've updated it based on your
suggestions.

DalePres

"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om...
DalePres <don-t-spa-m-me@lea-ve-me-a-lone--.com> wrote:
I have written an article that may help you:

http://www.dalepreston.com/Blog/Arch...0_Archive.html


Unfortunately that makes the same mistake that Zach did - assuming that
all text is ASCII.

I suggest using UTF-8 instead. Just use Encoding.UTF8.GetBytes instead
of Encoding.ASCII. (I'm also not sure why you're using Encoding.ASCII
in one place and manually creating an instance of ASCIIEncoding in
another.)

Also note that the call to Substring at the end isn't required if you
do things properly - don't assume that the decrypted data is the same
length as the encrypted data, but read the whole of the stream instead.
(Also don't assume that a single call to Stream.Read will do everything
you want it to. See http://www.pobox.com/~skeet/csharp/readbinary.html
for more on this.)

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

Nov 16 '05 #11
Hi,
First of all thanks for all your replies, but I think I was not clear
in my query.

What I have to encrypt is users passwords. These are to be stored in
an XML file. The password to be used both for encrypting and decrypting
is to be kept as a constant string.

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 16 '05 #12
To encrypt passwords. exctract the SHA-256 hash for the password using the
methods in the example I already gave you. Of course, you could use other
hash methods available in the framework, but the example I gave you uses
SHA-256 and is secure enough to last the next few years. Alternatives are
MD5, SHA-1 (now obsolete), SHA-384, or SHA-512.

To store the results in an XML file, use the Convert.ToBase64String method.
To convert back to the hash, use the Convert.FromBase64String method. Then
to validate the password in code, the user should type in the password, your
code creates a SHA-256 hash from what the user types in and then compares it
to the stored hash.
HTH

DalePres


"xarky d_best" <be*********@yahoo.com> wrote in message
news:%2****************@TK2MSFTNGP10.phx.gbl...
Hi,
First of all thanks for all your replies, but I think I was not clear
in my query.

What I have to encrypt is users passwords. These are to be stored in
an XML file. The password to be used both for encrypting and decrypting
is to be kept as a constant string.

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

Nov 16 '05 #13
DalePres <don-t-spa-m-me@lea-ve-me-a-lone--.com> wrote:
To encrypt passwords. exctract the SHA-256 hash for the password using the
methods in the example I already gave you. Of course, you could use other
hash methods available in the framework, but the example I gave you uses
SHA-256 and is secure enough to last the next few years. Alternatives are
MD5, SHA-1 (now obsolete), SHA-384, or SHA-512.


When you say that SHA-1 is now obsolete - the attacks recently have
reduced the strength of it, but for most purposes it may well still be
good enough.

From http://www.schneier.com/blog/archive...nalysis_o.html

<quote>
They can find collisions in SHA-1 in 2^69 calculations, about 2,000
times faster than brute force. Right now, that is just on the far edge
of feasibility with current technology. Two comparable massive
computations illustrate that point.

In 1999, a group of cryptographers built a DES cracker. It was able to
perform 2^56 DES operations in 56 hours. The machine cost $250K to
build, although duplicates could be made in the $50K-$75K range.
Extrapolating that machine using Moore's Law, a similar machine built
today could perform 2^60 calculations in 56 hours, and 2^69
calculations in three and a quarter years. Or, a machine that cost
$25M-$38M could do 2^69 calculations in the same 56 hours.
</quote>

I suspect most apps don't really need to anticipate that level of
attack.

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

Hi,
Thanks for your help.... query solved by your replies :)
*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 16 '05 #15

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

Similar topics

1
by: D. Alvarado | last post by:
On my Fedora Core 2 Linux dev box, I have installed mcrypt and compiled PHP with the --with-mcrypt option. I am concerned that when I move to another hosting enviornment mcrypt will not be...
1
by: Jase H | last post by:
Hello, I have a ASP.NET web application problem involving the data encryption and decryption assembly(DLL) used on the connection string value that is set in the webconfig file. The problem...
2
by: Dave Bailey | last post by:
I have developed a web app using DPAPI to encrypt a connection string in the web.config file. The application works perfectly on the development machine but when deployed to the server when...
2
by: sushant.bhatia | last post by:
Hi All. I'm using the NCrypto dll for RSA Encryption/Decryption (http://sourceforge.net/projects/ncrypto/). My encryption code in .Net is pretty simple. The dataToEncrypt length is 1024. The...
3
by: Mike | last post by:
Hi, I have been experimenting with the RijndaelManaged Cryptography class in C# and have stumbled upon a "peculiarity". Following code is standalone Console App that demonstrates using...
2
by: almurph | last post by:
Hi everyone, Can you help me please? I am having a problem with the encryption/decryption of words with the Irish fada in them. The Irish fada is like this: áéíóú/ÁÉÍÓÚ. It's kind of like the...
3
by: =?Utf-8?B?TG9yZW4=?= | last post by:
I’m trying to encrypt and decrypt a file in vb.net. I am using the TripleDESCryptoServiceProvider encryption found in System.Security.Cryptography. Below is the code for my Encrypt and Decrypt...
5
by: Netwatcher | last post by:
well, i started messing around with dictionaries, yet, most of the pages i found about them always talk about getting only one word out of it and turning it vice versa, i've been playing with that...
9
by: Betikci Boris | last post by:
I get bored last night and wrote a script that uses xor for encrypt- decrypt, however it woks fine under linux 2.6.25, text and documents are ok, but fails on compressed files *.jpg, *.pdf , etc ....
9
by: Alan M Dunsmuir | last post by:
In my (PHP-5) application I have to write some records to a table in my database, which I don't want even my clients using the system to be able to read. This is not a problem in National...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
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:
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
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...

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.