-2

i have made simple encryptor i need to encrypt words not only one letter and after that how can i decrypt word

        string[] Alpha = new string[]
        {"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" };
        string[][] Encryptor = new string[][]
        {
          new string[] {"4", "/=", "@", "/-"},
          new string[] {"8", "13", "|3", "&" },
          new string[] {"(", "¢", "<", "[", },
          new string[] {"[)", "|>", "|)", "|]"},
          new string[] {"3", "€", "[-", "[=" },
          new string[] {"|=", "ƒ", "/="},
          new string[] {"6", "(_+"},
          new string[] {"#", "/-/", "[-]", "]-[", ")-(", "(-)"},
          new string[] {"1", "!", "|", "][", ":"},
          new string[] {"_|", "_/"},
          new string[] {"|<", "|{"},
          new string[] {"1_", "|_", "[_"},
          new string[] {"|V|", "|V/|"},
          new string[] {"|V", "/v"},
          new string[] {"[]", "0", "()"},
          new string[] {"|*", "|0", "|o" },
          new string[] {"(_,)", "()_", "0_", "O_"},
          new string[] {"2", "|?", "|2"},
          new string[] {"5", "$", "_/~" },
          new string[] {"7", "¯|¯" },
          new string[] {"(_)", "!_!", "|_|"},
          new string[] {"|/", "!/"},
          new string[] {"`//", "\v/", "|_|_/"},
          new string[] {"><", "}{", ")("},
          new string[] {"`/", "!/"},
          new string[] {"≥", "7_", ">_"}
        };
        Random rand = new Random();
        string str = Console.ReadLine();
        int index = Array.IndexOf(Alpha, str);
        string rez = Encryptor[index][rand.Next(Encryptor[index].Length)];

Here i'm Getting Error If Length is More then 1 "Index Is Out Of Range" Console.Write(rez);

        Console.ReadKey();
  • 2
    Duplicate: [What is an IndexOutOfRangeException / ArgumentOutOfRangeException and how do I fix it?](https://stackoverflow.com/questions/20940979/what-is-an-indexoutofrangeexception-argumentoutofrangeexception-and-how-do-i-f) – Ňɏssa Pøngjǣrdenlarp Sep 12 '20 at 16:22
  • Your index is most likely -1. Use debugger to check whats going on – Jawad Sep 12 '20 at 16:25
  • yes it's -1 and how can i fix that – P4R4B3LLUM Sep 12 '20 at 16:28
  • Don't input something that's not in `Alpha`? What if the user inputs upper case letters? Or numbers? Or whitespace characters? Or anything else? – Corak Sep 12 '20 at 16:37
  • If your input is `"aa"` for example, the `index` will be `-1`, because `Alpha` does not contain `"aa"`. You probably want something like `string rez = string.Empty; foreach(char c in str) { int index = Array.IndexOf(Alpha, c.ToString()); if (index >= 0) { rez += Encryptor[index][rand.Next(Encryptor[index].Length)]; } }`. Which is not optimized at all, but should give you a starting point. – Corak Sep 12 '20 at 16:45

1 Answers1

0

The problem is when you have more than one character, because each value of alpha array is only one character. So you should loop through each character. I made a fiddle below is the code change that would loop through each character of the input string.

Random rand = new Random();
string str = Console.ReadLine();
    
StringBuilder rez = new StringBuilder();
for (int i = 0; i < str.Length; i++)
{
    int index = Array.IndexOf(Alpha, str[i].ToString());
    string[] curEnc = Encryptor[index];
    int r = rand.Next(curEnc.Length-1);
    rez.Append(curEnc[r]);
}

//Console.WriteLine(rez.ToString());
Jpsh
  • 1,697
  • 12
  • 17