-2
 public static void getkey() {

        char[] resultat = new char[256];

        for (char r = 0; r < 256; r++) {

            resultat[r] = r;
            System.out.print(r);
        }

    }

Hello , when i run this programm it shows me all the 256 characters in java.like thisjava console But my goal is to use a parameter of type String like this.

public static void getkey(String key)

this parameter are the letter of the alphabet. when i run the programm , it should replace the position of the caracters that are already stored as showed in the imagelink. And the result would be , if i add "ZYXWVUTSRQPONMLKJIHGFEDCBA" as a parameter , A would become Z , B would become Y , C = X , until Z becomes A.

And i will have to create a method encrypt , As i am trying to create a caesar cipher , and i will have to call this method getkey(String key) in other to encode my text, i am not using an int shift , the signature of the method looks like this:

public static void encrypt(String text , char[] key)

i am looking every where and i don't find any answer , please does somebody have an idea , i am a beginner at java programming . Thank you.

  • text is upper case, lowercase or both? in that case, what should be done? – Alberto Sinigaglia Nov 30 '19 at 01:54
  • text are uppercase. when i run for exemple the method encrypt with parameters , String text = "HALLO ANNA", char[] key = we get the key from the method getkey(string key), and if the key is "ZYXWVUTSRQPONMLKJIHGFEDCBA" the result should be ="SZOOL ZMMZ“ since A should become Z , H becomes S , L becomes O. Actualy it is a caesar cipher. As A becomes Z , B becomes Y , it continues until Z becomes A – Pierre Patrice Edimo Nkoe Nov 30 '19 at 02:34

1 Answers1

-1

You want to do something like this:

private static char[] key = ZYXWVUTSRQPONMLKJIHGFEDCBA".toCharArray(); //for reference

public String encrypt(char[] key, String plaintext){
    char[] encryptedText = new char[plaintext.size];
    for(int i = 0; i < plaintext.size(); i++){
       int charIndex = plaintext.charAt[i] - 'A'; //normalizes to A=0, Z=24
       encryptedText[i] = key[charIndex % key.length];
    }
    return new String(encryptedText);
}

It seems like you will have edge cases with ' ' characters and others so you may need to add some logic to ignore characters not within the range you are encrypting.

Doctor Parameter
  • 1,202
  • 2
  • 15
  • 21
  • Somehow my comment *explaining the down-vote*, i.e. that **subtracting 41 is wrong**, got deleted, so I'll say it again: subtracting 41 from a `char` value will not normalize `A` to `0`, because the value of `A` is not 41, it is 65. The confusion likely stems from the fact that [`'A' = 65 = 0x41`](http://www.asciitable.com/). – Andreas Dec 01 '19 at 03:20
  • I originally wrote 65 but then looked it up in the ascii table incorrectly when I doubted my self. – Doctor Parameter Dec 04 '19 at 14:56
  • So why not use `- 'A'` so there can be no mistake, and the code helps document itself? – Andreas Dec 04 '19 at 19:11