Subscribe to CodeTalk        RSS Feed
-----

ASCII Shift - back and forth

Icon 10 Comments
Sometimes you probably wanted to encrypt your message the easiest way possible. Maybe just for fun, maybe not. The ASCII shift method is probably the least secure "encryption" (if you can call it so) mechanism, but sometimes it is more than enough. So here is the function I came up with to perform this operation (written in C#):

		/// <summary>
		/// A function that decrypts a string that was modified by
		/// performing an ASCII shift.
		/// </summary>
		/// <param name="input">Input string (to decrypt).</param>
		/// <param name="shift">The value of the shift. </param>
		/// <returns>Decrypted string. (Type: string)</returns>
		public static string GetFromShifted(string input, int shift)
		{
			string decrypted = string.Empty;
			char[] characters = input.ToCharArray();

			for (int i = 0; i < characters.Length; i++)
			{
				int asciiCode = Convert.ToInt32(characters[i]);
				
				if (shift > 65355)
				{
					decrypted = "Cannot perform operation. Shift value too big.";
					break;
				}
				else
				{
					if ((asciiCode >= shift) || (shift>0))
					{
							char c = Convert.ToChar(asciiCode + shift);
							characters[i] = c;
					}
					else
					{
						decrypted = "Cannot perform operation. Invalid shift value.";
						break;
					}

				}
			}

			if (decrypted == string.Empty)
			{
				foreach (char c in characters)
					decrypted += c.ToString();
			}

			return decrypted;
		}



I tried to keep the code as simple as possible - there are not too many comments in here (well, except those for the XML documentation), but the variable naming pretty much shows the logic behind the code. The shift value is checked inside the function (since it cannot go over +65,355 - this is the limit for the char type). At the same time, the shift value cannot go too negative, since some characters can have their ASCII value equal to 1, for example, therefore subtracting 255 (or any other value) will result in an error (once again, the limits for the char type are 0 to 65,355). Even when a single character breaks this rule, this means that the message wasn't "encrypted" using the ASCII shift method and therefore there is no need to continue the operation.

The returned string can be either the decrypted message or an error message, depending on the situation.

10 Comments On This Entry

Page 1 of 1

finaiized Icon

03 January 2010 - 10:23 AM
So once we convert the string to a char, can we put it in a text file in hopes of re-reading it? Since without a list or something to tell it where a word stops or ends, how can we read it from a textfile assuming a string was turned into a char there (in number form)?
0

Core Icon

03 January 2010 - 11:16 AM
You don't actually convert a string to a char. You split the string in character, therefore you create a character array.

If you look more into the logic of the above function, you will see that later on each character has its ASCII code switched.

You can surely store the contents of the encoded/decoded string to a file. The File.WriteAllText method is what you need to write the string to a file, and the File.ReadAllText method is what you need to read the file contents.
0

finaiized Icon

03 January 2010 - 11:22 AM

Core, on 3 Jan, 2010 - 10:16 AM, said:

You don't actually convert a string to a char. You split the string in character, therefore you create a character array.

If you look more into the logic of the above function, you will see that later on each character has its ASCII code switched.

You can surely store the contents of the encoded/decoded string to a file. The File.WriteAllText method is what you need to write the string to a file, and the File.ReadAllText method is what you need to read the file contents.

Yes, but once you do take the character and turn it into an int, if I wanted to store it (as that's why I would encrypt it anyways), how would I read it back?

For example:
Here's a string- "Me"
Converted to ASII, that's 77 101.
Say I write that to a text file.
How would I re-read it, taking 77 and turning it back into M and 101 and turning it back into e?
0

Core Icon

03 January 2010 - 11:29 AM
Well that's a bit of a different question. If you are positive that you will use only ints, use a specific symbol as a separator, for example the comma, or a slash symbol ( / ). Then, when reading the data, you will have to use the String.Split method to split the whole string read from the file by the separating character. Note that this method will return you an array of string, so you have to assign it properly.
0

finaiized Icon

03 January 2010 - 11:33 AM

Core, on 3 Jan, 2010 - 10:29 AM, said:

Well that's a bit of a different question. If you are positive that you will use only ints, use a specific symbol as a separator, for example the comma, or a slash symbol ( / ). Then, when reading the data, you will have to use the String.Split method to split the whole string read from the file by the separating character. Note that this method will return you an array of string, so you have to assign it properly.

Okay, got it, thanks Core! I think the space character when converted into ASII is 0, but not positive 32 it seems, so I might use your idea of using a slash or something.

EDIT: I just thought of something- I would have to enter a space between every character so as to not mix up which character is which to make a word (You can't be sure that 77101 = Me, it could be divided like 771 and 01), and a separate character for a space right?
0

Core Icon

03 January 2010 - 12:57 PM
Leave the transformed string like it is. Just add the needed character as a separator when writing to file without transforming it as well.
0

finaiized Icon

03 January 2010 - 01:39 PM

Core, on 3 Jan, 2010 - 11:57 AM, said:

Leave the transformed string like it is. Just add the needed character as a separator when writing to file without transforming it as well.

But when I add words to a text file, I need a space/character between each word as well.

Say "Azor" is 34234 (random). How will the computer know that 34 is A? It could be 342 as well. So if I made it more like 34-2-3-4, then you'd know which letter it is.
0

Core Icon

03 January 2010 - 01:53 PM
Well this is exactly what I was talking about - having a separator.
0

finaiized Icon

03 January 2010 - 02:13 PM

Core, on 3 Jan, 2010 - 12:53 PM, said:

Well this is exactly what I was talking about - having a separator.

Yeah, but you need a separator for the actual space too.
0

Core Icon

03 January 2010 - 02:55 PM
The space has its own ASCII code, therefore if you convert all the chars in a string to ASCII codes, you will have only numeric values. This means that you won't get any spaces and/or letters/symbols. Now, this means that you can use the space itself as a separator.
0
Page 1 of 1

About this place

Who am I?

I am Dennis Delimarsky, and I am a software developer. Although I am still learning a lot of things, I already have some experience developing client applications. I am working with C#. I totally enjoy coding, but I am also passionate about reading and technical writing. And by the way, I am also an administrator here on Dream.In.Code.

What is this blog about?

This blog is about software development and some tools that I make.

How can I talk to the author?

Just go to http://ohai.im/dennis to find out about my web presence. Or just send me an email: codetalk@live.com

Search My Blog

July 2014

S M T W T F S
  12345
6789101112
13141516171819
202122232425 26
2728293031  

0 user(s) viewing

0 Guests
0 member(s)
0 anonymous member(s)