If you mean every character in a string, then try this:
string s = ...
byte[] bytes = System.Text.Encoding.ASCII.GetBytes(s);
"lol i'm just want know how encryption work!"
OK, so start by looking at how encryption works: and with the exception of the most basic Caesar (or Substitution) Cyphers, none of them work with characters - it's bytes all the way.
Substitution is easy: Set up an array of valid inputs, and a array of valid outputs, and convert them. (I'm going to do these in C#, not VB as it's a language I think better in - there are online converters if you need them)
char[] CaesarIP = new char[] { ' ', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z' };
char[] CaesarOP = new char[] { 'N', 'M', 'D', 'U', 'L', 'W', 'C', 'P', 'H', 'Q', 'X', 'R', 'K', 'T', ' ', 'I', 'F', 'G', 'Y', 'O', 'A', 'V', 'Z', 'J', 'S', 'E', 'B' };
private string CaesarEncrypt(string input)
{
char[] data = input.ToCharArray();
for (int index = 0; index < data.Length; index++)
{
data[index] = CaesarOP[Array.IndexOf(CaesarIP, data[index])];
}
return new string(data);
}
private void button1_Click(object sender, EventArgs e)
{
Console.WriteLine(CaesarEncrypt("HELLO WORLD"));
...
The loop becomes a lot simpler to read if you use a Dictionary instead.
But these are very, very simple to decrypt: every month dozens of "books" are published and old ladies who have never touched a computer all over the country are solving them without breaking into a sweat:
http://www.puzzlemagazines.co.uk/code-breakers[
^]
To get more secure, you need to ignore characters and start applying math operations which can be reversed. This means handling data as bytes instead of characters, because not all the values you generate will be valid characters! So now, you need two methods:
private byte[] BasicEncrypt(string input)
{
byte[] bytes = System.Text.Encoding.Unicode.GetBytes(input);
return bytes;
}
private string BasicDecrypt(byte[] bytes)
{
string output = System.Text.Encoding.Unicode.GetString(bytes);
return output;
}
private void button1_Click(object sender, EventArgs e)
{
Console.WriteLine(BasicDecrypt(BasicEncrypt("Hello World!\nThis is text?")));
Which show us that the basic conversion works both ways.
The simplest encrypt is ADD - if you add a fixed value to each byte, you can subtract it and get back to where you started:
private byte[] BasicEncrypt(string input)
{
byte[] bytes = System.Text.Encoding.Unicode.GetBytes(input);
for (int index = 0; index < bytes.Length; index++)
{
bytes[index] += 42;
}
return bytes;
}
private string BasicDecrypt(byte[] bytes)
{
for (int index = 0; index < bytes.Length; index++)
{
bytes[index] -= 42;
}
string output = System.Text.Encoding.Unicode.GetString(bytes);
return output;
}
But the data still isn't exactly secure - mostly because it's still effectively a substitution cypher.
A slightly better way is to use more complex math, and to use the index in the math as well - that way the two 'L' characters in "HELLO" don't generate the same output:
private byte[] BasicEncrypt(string input)
{
byte[] bytes = System.Text.Encoding.Unicode.GetBytes(input);
for (int index = 0; index < bytes.Length; index++)
{
bytes[index] += (byte)(42 + index);
bytes[index] ^= 123;
}
return bytes;
}
private string BasicDecrypt(byte[] bytes)
{
for (int index = 0; index < bytes.Length; index++)
{
bytes[index] ^= 123;
bytes[index] -= (byte)(42 + index);
}
string output = System.Text.Encoding.Unicode.GetString(bytes);
return output;
}
private void button1_Click(object sender, EventArgs e)
{
Console.WriteLine(BasicDecrypt(BasicEncrypt("Hello World!\nThis is text?")));
Notice that we have to do the math in the reversed order, or we don't get the right output.
And if you look at the intermediate stream of bytes, you'll see it looks like total gobbledegook! From this point on, you can't display the encrypted data as a string at all - it just doesn't work, and if you try to convert it to a string the chances are very, very good that you won't be able to use the string to decrypt it back to the original input, unless you use something like a "Hex character pair" representation of each byte, or maybe Base64 to get a "transmittable" string that can be converted back to an array of bytes.
From here, the next step is to start using a Key: basically a password which controls how the data to convert is changed. The simplest is to add the key value to the input value, but lets combine the basic stuff as well:
private byte[] KeyedEncrypt(string input, string key)
{
byte[] bytes = System.Text.Encoding.Unicode.GetBytes(input);
byte[] keys = System.Text.Encoding.Unicode.GetBytes(key);
int keyIndex = 0;
for (int index = 0; index < bytes.Length; index++)
{
if (keyIndex >= keys.Length)
{
keyIndex = 0;
}
bytes[index] += (byte)(keys[keyIndex++] + index);
bytes[index] ^= 123;
}
return bytes;
}
private string KeyedDecrypt(byte[] bytes, string key)
{
byte[] keys = System.Text.Encoding.Unicode.GetBytes(key);
int keyIndex = 0;
for (int index = 0; index < bytes.Length; index++)
{
if (keyIndex >= keys.Length)
{
keyIndex = 0;
}
bytes[index] ^= 123;
bytes[index] -= (byte)(keys[keyIndex++] + index);
}
string output = System.Text.Encoding.Unicode.GetString(bytes);
return output;
}
private void button1_Click(object sender, EventArgs e)
{
Console.WriteLine(KeyedDecrypt(KeyedEncrypt("Hello World!\nThis is text?", "My secret password"), "My secret password"));
This isn't bad - it would probably take the average PC quite a while to decrypt this if the NSA were interested in it: maybe even a couple of microseconds! :laugh:
But that's the basic idea: use a math operation you can reverse, and "secret information" that controls how the math works. That's all any encryption does, it just uses much, much more complex math: have a look at Wiki and it'll start to explain (in horrible math that I'd rather avoid myself!)
Make sense?