tags:

views:

536

answers:

6

Hi' I'm learning C# (as a hobbyist) and am trying to figure how to write a simple encryption program.

Converting characters from ASCII is fine, got that sorted, but it is the shift I am stuck on. If I want to code for (n + 3) for example, what line of thought should I be following?

A: 

Something like (char)('n' + 3) could give you the character you are looking for and then you can convert it to number once again, as you like..

Thomas Wanner
+4  A: 

Are you trying to do something like a Caesar cipher? (i.e. take every letter and shift it a certain number of letters to the left or right?)

If so, you will want to work with individual characters on your string. There's a datatype char that allows this, and you can do arithmetic operations. To get an array of chars from a string, call (string).ToCharArray(). Then you can iterate through the characters and shift each character.

char key = (char)3;
string toEncrypt = "abcdef";
char[] cArray = toEncrypt.ToCharArray();

for (var i = 0; i < cArray.Length; i++) {
   cArray[i] = (char)(cArray[i] + key);
}

One thing to keep in mind is that you'll need to "wrap" your characters if they go past the end of the alphabet. I'll leave this as a challenge to you. :)

Keep in mind that while this is a fun way to learn the language, it's not useful in the real world for encryption and is extremely easily broken.

Ryan Brunner
+2  A: 

There are tons of encryption options built into the .net framework. Since you are hobbying around, maybe you want to do it by hand, but your test program could simply use the framework.

System.Security.Cryptography Namespace

Also could use the Cryptography Application Block

Perhaps using these is more than you need, but using built in frameworks to meet your goal may help teach you the .net framework and c# as well.

Mike Ohlsen
A: 

What you are looking at is a Caesar cipher

In cryptography, a Caesar cipher, also known as a Caesar's cipher, the shift cipher, Caesar's code or Caesar shift, is one of the simplest and most widely known encryption techniques. It is a type of substitution cipher in which each letter in the plaintext is replaced by a letter some fixed number of positions down the alphabet. For example, with a shift of 3, A would be replaced by D, B would become E, and so on. The method is named after Julius Caesar, who used it to communicate with his generals.

This is a good way to start learning, but please read the section on Breaking the cipher

You could try something like

string val = "abc";
string encrypt = "";
for (int iChar = 0; iChar < val.Length; iChar++)
{
    encrypt += (char)(val[iChar] + 3);
}
string decrypt = "";
for (int iChar = 0; iChar < encrypt.Length; iChar++)
{
    decrypt += (char)(encrypt[iChar] - 3);
}

Another simple option to look at is

Simple XOR Encryption

What that means is that you only need one method, "EncryptDecrypt". Pass a string into it and get it back encrypted. Pass the encrypted string into it and you get back the original text, as long as the XOR character is the same.

astander
A: 

If you aren't trying to roll your own encryption, the framework has got encryption functionality built in. See this article for more info.

edit: fixed link

Tim
A: 

Thanks everyone.

Ripper
Instead of posting an answer, your just leave a comment on your original question.
Michael Kniskern
OK sorry! I'll know in future!
Ripper