Question

Create a Caesar Cipher in Java. Should be able to support full ascii table (0 to...

Create a Caesar Cipher in Java.

Should be able to support full ascii table (0 to infinity) with all symbols, characters, etc.

Using the Strings provided below, create an encryption and decryption method:

String basicText = "TOX!QL<];`\z6 L"n2=qafDkdp*a'S d 'SkQLq~+Ct*{8Y"4 ZnMxS.9e..&%9"

String password = "password1"

0 0
Add a comment Improve this question Transcribed image text
Answer #1

The java code for caesar cipher is given below

******************************Caesar.java**********************************


public class Caesar {
   //method for encrypting
   public static String createCipher(String p,int shift){
      
               //create a buffer string where we can append the
       //encrypted characters one by one
StringBuffer r= new StringBuffer();

        for (int j=0; j<p.length(); j++)
        {
                //The upper limit range of ASCII is 256
            // c= (value of character + shift)
            // and to keep it within range of ASCII LIMIT we use %256
                char cipherChar = (char)(((int)p.charAt(j) + shift) % 256);
                r.append(cipherChar);
           
        }
        //convert string buffer to final string cipher text
String ciphertext= r.toString();

return ciphertext;
  
      
   }
  
   //decryption is reverse of encryption
   public static String decrypt(String c,int shift){
      
      
StringBuffer r= new StringBuffer();

       for (int j=0; j<c.length(); j++)
       {
          
               char cipherChar = (char)(((int)c.charAt(j) - shift) % 256);
               r.append(cipherChar);
          
       }
String ciphertext= r.toString();
  
return ciphertext;

  
}

   public static void main(String[] args) {
       int shift=10;
       String s1= "TOX!QL<];`\\z6 L\"n2=qafDkdp*a'S d 'SkQLq~+Ct*{8Y\"4 ZnMxS.9e..&%9";
       String cipher1=createCipher(s1, shift);
       System.out.println("PlainText: "+s1);
       System.out.println("Cipher: "+cipher1);
       String decrypted1=decrypt(cipher1, shift);
       System.out.println("After decrypting: "+decrypted1);
      
       String s2="password1";
      
       String cipher2=createCipher(s2, shift);
       System.out.println("PlainText: "+s2);
       System.out.println("Cipher: "+cipher2);
       String decrypted2=decrypt(cipher2, shift);
       System.out.println("After decrypting: "+decrypted2);
}
}


Add a comment
Know the answer?
Add Answer to:
Create a Caesar Cipher in Java. Should be able to support full ascii table (0 to...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Language is java Create encryption and decryption that changes char by using ASCII by 5. How...

    Language is java Create encryption and decryption that changes char by using ASCII by 5. How to do this using threads? Let’s assume that our password string is “ABCDEFGHI”, once you run the encrypt function on this string you should get - “FGHIJKLMN” (since you are increasing the ASCII value by 5) If we call decrypt on this update string right away, it should give us the original string back - “ABCDEFGHI”

  • Using C++ Part C: Implement the modified Caesar cipher Objective: The goal of part C is...

    Using C++ Part C: Implement the modified Caesar cipher Objective: The goal of part C is to create a program to encode files and strings using the caesar cipher encoding method. Information about the caesar method can be found at http://www.braingle.com/brainteasers/codes/caesar.php.   Note: the standard caesar cipher uses an offset of 3. We are going to use a user supplied string to calculate an offset. See below for details on how to calculate this offset from this string. First open caesar.cpp...

  • create a Java class ShiftCipher The program ShiftCipher should take two inputs from the terminal. The...

    create a Java class ShiftCipher The program ShiftCipher should take two inputs from the terminal. The first should be a string of any length which contains any type of symbol (the plaintext). The second will be a shift value which should be between 0 and 25 inclusive (though you may design your program to be resilient to shifts beyond this value). The program should print the cipher text, in which each of the alphabetic characters in the string is shifted...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT