0

I have a program that uses encryption text strings, I basically just want to write a simple program that will print out all of the decrypted text for me.

For example: Say that the letter "A" = the byte "2C"; I would want to type the letter A in to the program and have it print out "2C" for me.

Does anybody know an easy way to do this?

Many thanks!

  • 3
    Note, any 'encryption' that consists of mapping letters like "A" = "2C" is *terrible* encryption. – p.s.w.g Sep 09 '14 at 20:32
  • 3
    probably need to do your homework yourself :) – codebased Sep 09 '14 at 20:32
  • I agree, sadly I did not create the encryption myself. – user3809661 Sep 09 '14 at 20:32
  • 1
    You could use a `Dictionary` to map one to the other (assuming that there isn't a simple function that would work), but note that you don't have enough space to account for all possible Unicode characters. Consider encoding the string as UTF-8 and then performing the encryption on the resulting byte array. – cdhowie Sep 09 '14 at 20:32
  • @cdhowie's suggestion of just creating a dictionary is a perfectly good solution as long as you're dealing with a fairly small set of characters (e.g. ASCII). – p.s.w.g Sep 09 '14 at 20:57
  • possible duplicate of [Converting a String to byte Array](http://stackoverflow.com/questions/472906/converting-a-string-to-byte-array) – Rezo Megrelidze Sep 09 '14 at 21:12

2 Answers2

1

By 2C I think you mean the hex representation of the letter A?

That would be something like String.Format("{0:X}", Convert.ToInt32('A'));

Update after clarification from OP

You either need to predefine your entire supported character set like this.

    static Dictionary<char, int> cyper = new Dictionary<char, int>
{
{'A', 44},
{'B', 45},
{'C', 46},
{'D', 47},
{'E', 48},
{'F', 49},
// .. etc
};

// ...

        Console.WriteLine(string.Format("{0:X}", cyper['A'])); // will print 2C

But that doesn't seem like a very good encryption if everything is just off by a few values.

Another approach would be to apply an encoding scheme. A runtime mathematical evaluation on the input that will evaluate to 2C (encrypt) and be able to take 2C and evaluate to A (decrypt).

Ternary
  • 2,401
  • 3
  • 31
  • 54
  • That's exactly what I mean, yes. Thank you. – user3809661 Sep 09 '14 at 20:33
  • Ah yes good call, forgot a step, you gotta convert it to the underlying int first. – Ternary Sep 09 '14 at 20:40
  • 1
    Now a format exception. Try this: `String.Format("{0:X}", (int)'A');` – Simon Farshid Sep 09 '14 at 20:42
  • Thanks, sorry about that. – Ternary Sep 09 '14 at 20:43
  • 1
    You corrected the main issue, but you still have the same problem as the other answer. The hex value of the ASCII / Unicode `A` is `41`, not `2C`. – p.s.w.g Sep 09 '14 at 20:44
  • What is the encoding being used? That's what will drive the underlying value. – Ternary Sep 09 '14 at 20:46
  • According to the question, it's not an encoding but some kind of '*encryption*'. – p.s.w.g Sep 09 '14 at 20:47
  • Gotcha, well then there's not enough information to provide an answer. – Ternary Sep 09 '14 at 20:48
  • The program encrypts all text. So instead of the letter "A" being 0x41 within the program, the letter "A" is 0x2C. If I want to change the text, I have to use that format. All I am trying to do is create a program that I can input all of the letters into with their proper hex representation (within the scope of this project) so that when I type the letter it prints the hex. You've all encountered exactly what I have; the value of A is 0x41 by default. I need a way to redefine that value for each letter of the alphabet so that it would print A = 0x2C instead of A = 0x41, etc. – user3809661 Sep 09 '14 at 21:01
  • 1
    @user3809661 I'll amend my answer – Ternary Sep 09 '14 at 21:06
0

I would suggest you to try like this:

byte[] b = System.Text.Encoding.UTF8.GetBytes (yourString);
Rahul Tripathi
  • 168,305
  • 31
  • 280
  • 331
  • 1
    With this method, `"A"` → `[0x41]`. If OP expects a different mapping, e.g. `"A"` → `[0x2C]`, this won't work. – p.s.w.g Sep 09 '14 at 20:39
  • 1
    @p.s.w.g:- Hmm..makes a sense. But I thought this is the correct way to proceed with when you want to **convert** a string to byte otherwise it would be just a format change like `String.Format("{0:X}", (int)'A');` is not the conversion in the real sense. Stumped :( – Rahul Tripathi Sep 09 '14 at 20:41
  • Minus 41 (I think) from the result to get the numerically-based index (1 - 52) if it's UNICODE – Dan Sep 09 '14 at 21:09