Ceasar Cipher Logic Problem

I'll first state out that this is an assignment for a class and I have it working I just need some help on the logic. Also side note, I know this is written in java, but logic behind it anyway.

It's a program that takes a string of text, and encrypts it using ceasar cipher. For you that don't know ceasar cipher is basically taking a character and moving it x amount of spaces ahead. For example if X = 5 then:

String text = "abcdefghijklmnopqrstuvwxyz"

the cipher would be

String cipher = "fghijklmnopqrstuvwxyzabcde"

Now here's my problem. using the alphabet like above my program works. When I enter a custom string is where the value doesn't turn out right. The program is only suppose to cipher letters so white spaces and for example "!" don't get ciphered.

An example the teacher gave us was:

String text = "Nice to meet you";

Expected output:

String cipher = "Sbvj mt fxjm dhn";

The output above is following the algorithm C1C2C2C1C2 where C1 = 5 and C2 = 19. When I run my code my output is "wxrt id btti ndj!" which is obviously wrong. One problem I'm having is I don't know how to separate uppercase letters from lowercase. And another is I'm confused on how to achieve the Expected output.

Here's my code:

1
2
3
4
5
6
7
while(count < 5)
{    
   text = pa.CeasarCipher(text, count);
   System.out.println(text);
   count++;

}

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
public String CeasarCipher( String text, int count )
{
   int c1 = 5;
   int c2 = 19;
            
   // Convert to char array.
   char[] buffer = text.toCharArray();

   // Loop over characters.
   for (int i = 0; i < buffer.length; i++) {

   // Shift letter, moving back or forward 26 places if needed.
   char letter = buffer[i];
   if( !(letter == ' ' || letter < 'A'))
   {
      if(count == 0 || count == 3)
         letter = (char) (letter + c1);
      else if(count == 1 || count == 2 || count == 4)
         letter = (char) (letter + c2);
      if (letter > 'z') 
      {
          letter = (char) (letter - 26);
      } 
      else if (letter < 'a') 
      {
          letter = (char) (letter + 26);
      }
     }
     buffer[i] = letter;
    }
    text = new String(buffer);
    return text;
}
Last edited on
1
2
3
4
public String CeasarCipher( String text, int count )
{
   int c1 = 5; //these ought to be arguments of your function, it would also simplify your select
   int c2 = 19;
if( !(letter == ' ' || letter < 'A')) ¿don't you have an `isalpha()' function?

> String text = "Nice to meet you";
> Expected output:
> String cipher = "Sbvj mt fxjm dhn";
meet -> fxjm
`e' transform first to `x' and then to `j'. One of the biggest weakness of Caesar cipher is that the output character depends only on the input character (all the `e' would transform to the same symbol), and so the process doesn't change the frequency of the symbols.
If that's really the expected output, then you are misunderstanding how the cipher should be applied.


> following the algorithm C1C2C2C1C2 where C1 = 5 and C2 = 19
you may as well add all the offset and apply just one cipher
(3*C1 + 3*C2) mod ('z'-'a'+1)
there's another weakness. A brute-force approach only needs to try 'z'-'a'+1 times.


Edit: taking the distance between the strings, I've got this
5 19 19 5 0 19 5 0 19 19 5 19 0 5 19 19
were 0 is the white space.
Then it becomes clearer what you need to do:
- the first character gets moved C1 places
- the second character, C2 places
- third, C2
- fourth C1,
- fifth, C2
- then repeat that sequence until you had traversed the entire string.
Last edited on
closed account (48T7M4Gy)
http://stackoverflow.com/questions/16458564/convert-string-to-ascii-numeric-value-in-java

http://www.asciitable.com/

Have a look at the above. The key to your Caesar cipher is threefold:
1. Converting a character in your String to an ASCII code and vice versa. Note that upper and lower cases of the same character have a constant difference.

2. Use modular arithmetic to advantage

3. If you encounter a character in the string outside the standard alphabet eg a space, then print a space.
Thank you both! It turned out I was misunderstanding the whole situation. After following the way @ne555 explained it, I achieve the right output. Thanks you!
Topic archived. No new replies allowed.