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!

6
  • 3
    Note, any 'encryption' that consists of mapping letters like "A" = "2C" is terrible encryption. Commented Sep 9, 2014 at 20:32
  • 3
    probably need to do your homework yourself :) Commented Sep 9, 2014 at 20:32
  • I agree, sadly I did not create the encryption myself. Commented Sep 9, 2014 at 20:32
  • 1
    You could use a Dictionary<char, byte> 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. Commented Sep 9, 2014 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). Commented Sep 9, 2014 at 20:57

2 Answers 2

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).

Sign up to request clarification or add additional context in comments.

10 Comments

That's exactly what I mean, yes. Thank you.
Ah yes good call, forgot a step, you gotta convert it to the underlying int first.
Now a format exception. Try this: String.Format("{0:X}", (int)'A');
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.
@user3809661 I'll amend my answer
|
0

I would suggest you to try like this:

byte[] b = System.Text.Encoding.UTF8.GetBytes (yourString);

3 Comments

With this method, "A"[0x41]. If OP expects a different mapping, e.g. "A"[0x2C], this won't work.
@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 :(
Minus 41 (I think) from the result to get the numerically-based index (1 - 52) if it's UNICODE

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.