Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java, How to implement a Shift Cipher (Caesar Cipher)

I want to implement a Caesar Cipher shift to increase each letter in a string by 3.

I am receiving this error:

possible loss of precision required char; found int

Here is my code so far:

import java.util.Scanner;
import java.io.*;

public class CaesarCipher
{
    public static void main (String [] args) {

        char[] letters = {'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'};

        char[] message = "onceuponatime".toCharArray();
        char[] eMessage = new char[message.length];
        char shift = 3;

        //encrypting message
        for(int i = 0; i <= message.length; ++i)
        {
            eMessage[i] = (message[i] + shift) % (char) letters.length;
            System.out.println(x);               
        }              
    }
}

What causes this error? How can I implement a caesar Cipher shift to increase each letter in a string by 3?

like image 669
Sophia Ali Avatar asked Oct 01 '13 05:10

Sophia Ali


People also ask

How do you create a shift cipher in Java?

Pseudocode: Loop through each character in the string. Add shift to the character and if it falls off the end of the alphabet then subtract shift from the number of letters in the alphabet (26) If the shift does not make the character fall off the end of the alphabet, then add the shift to the character.

Is Caesar cipher and shift cipher same?

The Caesar Cipher is a type of shift cipher. Shift Ciphers work by using the modulo operator to encrypt and decrypt messages. The Shift Cipher has a key K, which is an integer from 0 to 25.

How do you encrypt using Caesar cipher which is shifted of 3?

How to encrypt using Caesar cipher? Example: Crypt DCODEX with a shift of 3 . To encrypt D , take the alphabet and look 3 letters after: G . So D is encrypted with G . To encrypt X , loop the alphabet: after X : Y , after Y : Z , after Z : A . So X is coded A .


1 Answers

Java Shift Caesar Cipher by shift spaces.

Restrictions:

  1. Only works with a positive number in the shift parameter.
  2. Only works with shift less than 26.
  3. Does a += which will bog the computer down for bodies of text longer than a few thousand characters.
  4. Does a cast number to character, so it will fail with anything but ascii letters.
  5. Only tolerates letters a through z. Cannot handle spaces, numbers, symbols or unicode.
  6. Code violates the DRY (don't repeat yourself) principle by repeating the calculation more than it has to.

Pseudocode:

  1. Loop through each character in the string.
  2. Add shift to the character and if it falls off the end of the alphabet then subtract shift from the number of letters in the alphabet (26)
  3. If the shift does not make the character fall off the end of the alphabet, then add the shift to the character.
  4. Append the character onto a new string. Return the string.

Function:

String cipher(String msg, int shift){
    String s = "";
    int len = msg.length();
    for(int x = 0; x < len; x++){
        char c = (char)(msg.charAt(x) + shift);
        if (c > 'z')
            s += (char)(msg.charAt(x) - (26-shift));
        else
            s += (char)(msg.charAt(x) + shift);
    }
    return s;
}

How to invoke it:

System.out.println(cipher("abc", 3));  //prints def
System.out.println(cipher("xyz", 3));  //prints abc
like image 91
Eric Leschinski Avatar answered Sep 21 '22 14:09

Eric Leschinski