473,738 Members | 8,397 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

simple encryption

Hi
I'm looking for some .net example code on encryption: I don't want anything fancy, no complex third party components with sophisticated algorithms, explanations of principle, or "public/private key" confusion (which seems to be all google can come up with) - I just want a SIMPLE example of how I can encrypt a string and then get back the SAME STRING. I found an example on MSDN which involves compiling a DLL then using it in a test form, but about 1 in 5 times it throws an exception, this isn't good enough for me - I just need it to be simple. It doesn't need to be state of the art defence against hardened crackers, mainly just so it can't be read from glancing at it - not much more. But must be SIMPLE and ALWAYS GIVE THE SAME THING BACK.

Anyone?
Nov 16 '05 #1
11 6527
Beeeeeeeeeeeeve s <Be************ **@discussions. microsoft.com> wrote:
I'm looking for some .net example code on encryption: I don't want
anything fancy, no complex third party components with sophisticated
algorithms, explanations of principle, or "public/private key"
confusion (which seems to be all google can come up with) - I just
want a SIMPLE example of how I can encrypt a string and then get back
the SAME STRING. I found an example on MSDN which involves compiling
a DLL then using it in a test form, but about 1 in 5 times it
throwsan exception, this isn't good enough for me - I just need it to
be simple. It doesn't need to be state of the art defence against hardened
crackers, mainly just so it can't be read from glancing at it - not
much more. But must be SIMPLE and ALWAYS GIVE THE SAME THING BACK.


The docs for CryptoStream give a simple example of encrypting a byte
array. To use strings instead, just use Encoding.UTF8.G etBytes to
transform the string into a byte array, and Encoding.UTF8.G etString to
transform it back after decrypting.

--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #2
Cheers for the very quick response.

But why UTF8?

And any link to this example in the docs?

"Jon Skeet [C# MVP]" wrote:
Beeeeeeeeeeeeve s <Be************ **@discussions. microsoft.com> wrote:
I'm looking for some .net example code on encryption: I don't want
anything fancy, no complex third party components with sophisticated
algorithms, explanations of principle, or "public/private key"
confusion (which seems to be all google can come up with) - I just
want a SIMPLE example of how I can encrypt a string and then get back
the SAME STRING. I found an example on MSDN which involves compiling
a DLL then using it in a test form, but about 1 in 5 times it
throwsan exception, this isn't good enough for me - I just need it to
be simple. It doesn't need to be state of the art defence against hardened
crackers, mainly just so it can't be read from glancing at it - not
much more. But must be SIMPLE and ALWAYS GIVE THE SAME THING BACK.


The docs for CryptoStream give a simple example of encrypting a byte
array. To use strings instead, just use Encoding.UTF8.G etBytes to
transform the string into a byte array, and Encoding.UTF8.G etString to
transform it back after decrypting.

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

Nov 16 '05 #3
Beeeeeeeeeeeeve s <Be************ **@discussions. microsoft.com> wrote:
Cheers for the very quick response.

But why UTF8?
It's a lossless encoding which is compact for ASCII. If you're using a
lot of non-ASCII, it would make sense to use Encoding.Unicod e instead.
And any link to this example in the docs?


What, to using Encoding.UTF8? Not that I know of. Encryption
fundamentally acts on binary data, and the conversion between binary
data and text data is quite distinct from encryption itself.

--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #4
Link to the CryptoStream Example (MSDN):

http://msdn.microsoft.com/library/de...ClassTopic.asp

Tom.

"Beeeeeeeeeeeev es" wrote:
Cheers for the very quick response.

But why UTF8?

And any link to this example in the docs?

"Jon Skeet [C# MVP]" wrote:
Beeeeeeeeeeeeve s <Be************ **@discussions. microsoft.com> wrote:
I'm looking for some .net example code on encryption: I don't want
anything fancy, no complex third party components with sophisticated
algorithms, explanations of principle, or "public/private key"
confusion (which seems to be all google can come up with) - I just
want a SIMPLE example of how I can encrypt a string and then get back
the SAME STRING. I found an example on MSDN which involves compiling
a DLL then using it in a test form, but about 1 in 5 times it
throwsan exception, this isn't good enough for me - I just need it to
be simple. It doesn't need to be state of the art defence against hardened
crackers, mainly just so it can't be read from glancing at it - not
much more. But must be SIMPLE and ALWAYS GIVE THE SAME THING BACK.


The docs for CryptoStream give a simple example of encrypting a byte
array. To use strings instead, just use Encoding.UTF8.G etBytes to
transform the string into a byte array, and Encoding.UTF8.G etString to
transform it back after decrypting.

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

Nov 16 '05 #5
There may be characters such as digits, but there won't be anything like arabic characters or anything....

"Jon Skeet [C# MVP]" wrote:
Beeeeeeeeeeeeve s <Be************ **@discussions. microsoft.com> wrote:
Cheers for the very quick response.

But why UTF8?


It's a lossless encoding which is compact for ASCII. If you're using a
lot of non-ASCII, it would make sense to use Encoding.Unicod e instead.
And any link to this example in the docs?


What, to using Encoding.UTF8? Not that I know of. Encryption
fundamentally acts on binary data, and the conversion between binary
data and text data is quite distinct from encryption itself.

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

Nov 16 '05 #6
I'm confused... what is the difference between Encoding.ASCII. GetBytes and Encoding.UTF8.G etBytes ????

"Jon Skeet [C# MVP]" wrote:
Beeeeeeeeeeeeve s <Be************ **@discussions. microsoft.com> wrote:
Cheers for the very quick response.

But why UTF8?


It's a lossless encoding which is compact for ASCII. If you're using a
lot of non-ASCII, it would make sense to use Encoding.Unicod e instead.
And any link to this example in the docs?


What, to using Encoding.UTF8? Not that I know of. Encryption
fundamentally acts on binary data, and the conversion between binary
data and text data is quite distinct from encryption itself.

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

Nov 16 '05 #7
Beeeeeeeeeeeeve s <Be************ **@discussions. microsoft.com> wrote:
I'm confused... what is the difference between
Encoding.ASCII. GetBytes and Encoding.UTF8.G etBytes ????


One uses the ASCII encoding and the other uses UTF-8 - they're very
different encodings, which happen to give the same results for all
ASCII characters.

See http://www.pobox.com/~skeet/csharp/unicode.html for more
information.

--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #8
Beeeeeeeeeeeeve s <Be************ **@discussions. microsoft.com> wrote:
At the end of the day, my problem is far from solved. I wanted simple
encryption. What did I get? Confusion.

Here's my pot shot at encrypting... basically I've got a form with
four textboxes: txtPassword (to encrypt), txtEncryptedTex t (for what
it's encrypted to), txtDecryptedTex t (for the decrypted text - sadly
the program never makes it this far) and txtInitializati onVector.

Where am I going wrong, I'm getting weary.


Well, for one thing you've got a MemoryStream for no good reason when
encrypting. You can just use

cs.Write(b_raw, 0, b_raw.Length);

The second thing you're doing wrong is assuming that any random piece
of binary data can be converted to UTF-8 and back losslessly. Just as
the *input* to encryption is binary, so is the output. The easiest way
of converting binary data to a string and back is to use
Convert.To/FromBase64Strin g.

I'm not sure about your IV usage, either...

--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #9
Beeeeeeeeeeeeve s <Be************ **@discussions. microsoft.com> wrote:
The second thing you're doing wrong is assuming that any random piece
of binary data can be converted to UTF-8 and back losslessly.
Oh, well excuse me for assuming! Not that I'm blaming its presence on
you or anything, but would you possibly care to enlighten me on what
the BLOODY HELL UTF8 is GOOD FOR then??!!!??!!


Sure. It's a good way of encoding character data in binary. The fact
that any text string can be encoded to binary data using UTF-8
*doesn't* mean that every piece of binary data is a valid UTF-8
representation of a string.

By the way, shouting, being ungrateful and making pretty much repeat
posts isn't likely to get you answers any quicker. On the contrary,
people may well decide to ignore you instead of answering you.
Just as
the *input* to encryption is binary, so is the output. The easiest way
of converting binary data to a string and back is to use
Convert.To/FromBase64Strin g.


And this won't eat any of my information??


No. The resulting string will be 4/3rds the size of the original binary
data though.

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

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

Similar topics

3
14222
by: Myrilath | last post by:
How would I go about simple encryption of a string? The user info is stored in a file and I just wanted to encrypt the passwords and usernames. It doesn't need to be complex or even that difficult to decrypt as it is only about 5 users on a small intranet. The database user table controls the rights individual client apps have but it is just to give some protection to the local user lists for the client apps. Thanks in advance.
1
1691
by: David Bear | last post by:
I would like some advice on what crypto lib to use. I am writing a cgi app that will store sensative data in a backend postgresql server. I have some simple numeric data I would like to make as safe as possible, without too much overhead. The goal would be to store the information in a simple reversable encrypted form. The encryption key would live only in the python app that was accessing the data. So perhaps there are two general...
4
2104
by: Steven Blair | last post by:
I have the following number: 64521234567890 and need to apply some sort of simple encryption. Does c# have any classes for doing this. I cant use 3DES or anything as complex as. The size of the string cannot increase either (no more than 14 chars) and must be numeric only.
2
2122
by: David J Rose | last post by:
I am looking for a simple encryption method that is common to .net and php. Does anyone know off-hand of such a method? Thanks
2
1275
by: Fred Nelson | last post by:
I'm developing a web application and I'm trying to find a simple way to encrypt and decrypt a password field. A one-way encryption won't work since I need to be able to e-mail users their password if they loose it. I have searched MSDN and there are obviously many VERY complex encryption services available - I don't need certificates or PKI, Kerberos, etc - just something simple: * for example:
6
7872
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. thanks *** Sent via Developersdex http://www.developersdex.com *** Don't just participate in USENET...get rewarded for it!
11
5042
by: John Williams | last post by:
I've written a simple program to do XOR encryption as my first foray into understanding how encryption works. The code compiles fine, however it segmentation faults on every run. using gdb to debug it let me narrow the problem down to the Cipher function I think it faults at line 84 or 85. The program makes it's first read/cipher/write pass without issue but the second pass kills it. Using gdb to print the variables left showed me the...
3
9871
by: yottabyte | last post by:
Hey bytes, you may or may not remember but last time I was here a few months ago I got some help with making a hangman program which went well. Now I'm still doing okay in Java this year but I'm starting a new project for simple encryption and decryption. The way I set up the program to work is just to add 1 to each character to change a message... (example: "hello" becomes "ifmmp") or subtract 1 from each character to decrypt a message the...
2
3751
by: shafwan | last post by:
I want to know more about how to develop a encryption program simple columnar with random arrangement. can you show me how to do it in python because I'm very interested in this language. thanks for your help
0
9476
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9335
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9263
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
6053
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4570
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4825
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3279
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2745
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2193
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.