Click here to Skip to main content
15,917,456 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
How Convert each char to ASCII code?
Posted
Comments
PIEBALDconsult 23-Apr-14 13:52pm    
Why? What are you going to do with them? It sounds like homework.
JaironLanda 23-Apr-14 14:00pm    
nope, isn't not homework...
JaironLanda 23-Apr-14 14:01pm    
i'm just want to know more about ASCII code..

If you mean every character in a string, then try this:
VB
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)
C#
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:
C#
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:
C#
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:
C#
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:
C#
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?
 
Share this answer
 
v2
Comments
JaironLanda 23-Apr-14 14:08pm    
doesn't work...
JaironLanda 23-Apr-14 14:09pm    
Dim s As String = txtInput.Text
Dim bytes() As Byte

bytes = System.Text.Encoding.ASCII.GetBytes(s)
txtOutput.Text = System.Text.Encoding.ASCII.GetString(bytes)
OriginalGriff 23-Apr-14 14:14pm    
And what do you expect that to do?
You convert the string to ASCII bytes, then convert those back to Unicode - so you end up with exactly what you started with!

What are you trying to achieve with this? Why do you think ASCII is necessary at all?
JaironLanda 23-Apr-14 14:17pm    
then how to show the output after convert to ASCII code?
OriginalGriff 23-Apr-14 14:22pm    
:laugh:
What are you expecting to see?
ASCII is just a way to display characters, like Unicode is: just there are less characters in an ASCII set than there are in Unicode.
This is the basic ASCII set:
http://www.asciitable.com/index/asciifull.gif
Unicode can have over a million distinct displayable characters!
You will find your solution on one of the following links https://www.google.co.uk/?gfe_rd=cr&ei=7f1XU-WoH6bR8gfb-ICYDA#q=How+Convert+each+char+to+ASCII+code+vb.net[^]

Please, at least try to make an attempt to do your own research before posting a question here
 
Share this answer
 
It is impossible in .NET and many modern platforms. A character in .NET is the Unicode character. Only few of those characters match some ASCII codes, only the characters of the code points below 128. All characters have different presentations, depending on UTF, including those matching the ASCII range. Unicode only defines correspondence between "characters" understood as cultural entities abstracted from fonts and other details, and integers values (called code points) which are understood in their mathematical sense, abstracted from their binary representations (which are defined by UTFs). ASCII is obsolete, face it.
For some background, please see:
http://en.wikipedia.org/wiki/Unicode[^],
http://www.unicode.org[^],
http://en.wikipedia.org/wiki/Unicode_Transformation_Format#Unicode_Transformation_Format_and_Universal_Character_Set[^],
http://www.unicode.org/faq/utf_bom.html[^].

—SA
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900