Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Hey folks, I\'ve been having a project with Caesar cipher and Vinegere cipher. I

ID: 642521 • Letter: H

Question

Hey folks, I've been having a project with Caesar cipher and Vinegere cipher. I already figured out Caesar one but I'm struggling with Vineger cipher. My encryption code is divided into two parts: encrypt() and encrypt1(). AND ONLY THESE TWO METHODS ARE ALLOWED! NO arraylists, maps, arrays, double arrays, generics.. encrypt() method extracts every character and passes it to encrypt1()where all of the encryption takes place basically. I did here the code but it's not giving me any good results. For example, if

Java Code:

1

new VigenereCipher("SLIME").encrypt("GREEN")

where "SLIME" is a key password and "GREEN" is the String which should be encrypted as YcMQR. But my code gives something like this ?&|{. Consider that the alphabet object covers everything =

ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz 1234567890!@#$%^&*()_+-=[]{}\|;:'",./?<>

"` Could please smb help me with that? Thanks in advance!

Java Code:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

public class VigenereCipher extends SymmetricCipher {

       

      protected String password;

      protected int passwordPos;

       

      public VigenereCipher(String password, Alphabet alphabet)

      {

          super(alphabet);

          this.password = password;

      }

      public VigenereCipher(String password)

      {

          super(Alphabet.DEFAULT);

          this.password = password    ;

      }

       

      public String getPassword()

      {

          return this.password;

      }

       

       

       

      public String encrypt(String s)

      {

          passwordPos = 0;

          String encrypted = "";

          for(int i = 0; i < s.length(); i++)

          {

              char c = s.charAt(i);

              encrypted += encrypt1(c);

               

          }

           

           

          return encrypted;

      }

       

      protected char encrypt1(char c)

      {

          //Alphabet temp = new Alphabet(s);

          int index = 0;

          char result = 0;

           

          index = alphabet.indexOf(c); //Found index of a character

           

          if(index != -1)

          {

              int keyIndex = alphabet.get(passwordPos++ % password.length());

              result = alphabet.get((keyIndex + index) % alphabet.length());

          }

          else

              throw new NotInAlphabetException(c, alphabet);

          return result;

      }

       

      public String decrypt(String c)

      {

          return c;

      }

       

      protected char decrypt1(char c)

      {

          return c;

      }

       

      public String toString()

      {

          return "Vigenere Cipher (password ='"+this.password+"')";

      }

       

       

   

  }

1

new VigenereCipher("SLIME").encrypt("GREEN")

Explanation / Answer

The problem is in this line in the encrypt1 function:

Here you're trying to look up a key in the password, but you're actually getting it from the alphabet. What you want is to find the relevant character from the password (looping around when you reach the end), and then find out what the index of that character is in your alphabet, so you can take the plane text and shift it by that (index) many characters in the alphabet.

To do that, you can use this code:

When I change this line and run your code with SLIME as the key and GREEN as the plaintext, I get the resul YcMQR, which is what you said you expected.