Question

Change the following Shift Cipher program so that it uses OOP(constructor, ect.) import java.util...

Change the following Shift Cipher program so that it uses OOP(constructor, ect.)

import java.util.*;

import java.lang.*;

/**

*

* @author STEP

*/

public class ShiftCipher

{

/**

* @param args the command line arguments

*/

public static void main(String[] args) {

// TODO code application logic here

Scanner input = new Scanner(System.in);

String plainText;

System.out.print("Please enter your string: "); // get message

plainText = input.nextLine();

System.out.print("Please enter your shift cipher key: "); // get s

int s = input.nextInt();

int length = plainText.length(); // get length of msg

int[] cipherText = new int[length];

for (int i=0;i<length;i++) {

cipherText[i] = (int)plainText.charAt(i); //convert string to ascii

}

int[] encryption = cipherText.clone();

for(int i=0;i<length;i++) {

encryption[i] = (encryption[i] + s) % 127;//Encrypt message

}

String encryptedText = "";

for(int i=0;i<length;i++) {

encryptedText += (char)(encryption[i]); //initialize encryption string

}

String decryptedText = encryptedText; //decrypt message

for (int i=0;i<length;i++) {

cipherText[i] = 127 + (((int)(decryptedText.charAt(i))-s) % 127);//decrypt into cipher string

}

System.out.println("Decrypted message: " + plainText); //output results

System.out.println("Shift : " + s);

System.out.println("Encrypted message: " + encryptedText); //

System.out.print("Decrypted message: ");

for(int i=0;i<length;i++) {

System.out.print((char)cipherText[i]);

}

System.out.print("\n");

}

}

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

public class ShiftCipher {
  
   //class member which is key of encryption
   private int s;
  
   //constructor
   public ShiftCipher(int s)
   {
       this.s = s;
   }
   //getter methof
   public int getS() {
       return s;
   }
  
   //this method will encrypt argument passed with the key s
   public String encrypt(String plainText)
   {
       int length = plainText.length(); // get length of msg
       int[] cipherText = new int[length];
       for (int i=0;i<length;i++) {
           cipherText[i] = (int)plainText.charAt(i)
                   ; //convert string to ascii
       }

       int[] encryption = cipherText.clone();
       for(int i=0;i<length;i++) {
           encryption[i] = (encryption[i] + s) % 127;//Encrypt message
       }

       String encryptedText = "";
       for(int i=0;i<length;i++) {
           encryptedText += (char)(encryption[i]); //initialize encryption string
       }
      
       return encryptedText;
   }
  
   //this method will be used to decrypt the encrypted text
   public String decrypt(String encryptedText)
   {
       int length = encryptedText.length();
       int[] cipherText = new int[length];
       for (int i=0;i<length;i++) {
          
           cipherText[i] = ((127 + (int)(encryptedText.charAt(i))-s) % 127);
           //decrypt into cipher string
       }
       String decryptedText = "";
       for(int i=0;i<length;i++) {
           decryptedText += (char)(cipherText[i]); //initialize encryption string
       }
      
       return decryptedText;
   }
}

public class ShiftCipherTest {

   public static void main(String[] args) {
       // TODO Auto-generated method stub
       Scanner input = new Scanner(System.in);
       String plainText;
       System.out.print("Please enter your string: "); // get message
       plainText = input.nextLine();
       System.out.print("Please enter your shift cipher key: "); // get s
       int s = input.nextInt();
       ShiftCipher sc = new ShiftCipher(s);
      
       String encryptedText = sc.encrypt(plainText);
       String decryptedText = sc.decrypt(encryptedText);
      
       System.out.println("\n\nPlain Text: " + plainText);
      
       System.out.println("Shift : " + s);

       System.out.println("Encrypted message: " + encryptedText); //

       System.out.print("Decrypted message: " + decryptedText);

   }

}

If there is anything that you do not understand, or need more help then please mention it in the comments section.

Add a comment
Know the answer?
Add Answer to:
Change the following Shift Cipher program so that it uses OOP(constructor, ect.) import java.util...
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
  • Caesar Cipher v3 Decription Description A Caesar cipher is one of the first and most simple...

    Caesar Cipher v3 Decription Description A Caesar cipher is one of the first and most simple encryption methods. It works by shifting all letters in the original message (plaintext) by a certain fixed amount (the amounts represents the encryption key). The resulting encoded text is called ciphertext. Example Key (Shift): 3 Plaintext: Abc Ciphertext: Def Task Your goal is to implement a Caesar cipher program that receives the key and an encrypted paragraph (with uppercase and lowercase letters, punctuations, and...

  • // can someone explain the code line by line shortly.thanks import java.security.KeyPair; import ...

    // can someone explain the code line by line shortly.thanks import java.security.KeyPair; import java.security.KeyPairGenerator; import java.security.PrivateKey; import java.security.PublicKey; import java.util.Base64; import java.util.HashMap; import java.util.Map; import javax.crypto.Cipher; // Java 8 example for RSA encryption/decryption. // Uses strong encryption with 2048 key size. public class GlobalMembers { public static void main(String[] args) throws Exception { String plainText = "Hello World!"; // Generate public and private keys using RSA Map<String, Object> keys = getRSAKeys(); PrivateKey privateKey = (PrivateKey) keys.get("private"); PublicKey publicKey = (PublicKey)...

  • For this code after case 0, I need it to ask the user if they would...

    For this code after case 0, I need it to ask the user if they would like to see the menu again and some type of if statement or loop to ask them yes or no and if yes print again and if no end program. So it would ask if they would like to see the menu again then ask what their choice is. If yes print menu and if no end program. import java.util.InputMismatchException; import java.util.*; import java.io.*;...

  • I need help running this code. import java.util.*; import java.io.*; public class wordcount2 {     public...

    I need help running this code. import java.util.*; import java.io.*; public class wordcount2 {     public static void main(String[] args) {         if (args.length !=1) {             System.out.println(                                "Usage: java wordcount2 fullfilename");             System.exit(1);         }                 String filename = args[0];               // Create a tree map to hold words as key and count as value         Map<String, Integer> treeMap = new TreeMap<String, Integer>();                 try {             @SuppressWarnings("resource")             Scanner input = new Scanner(new File(filename));...

  • Why does my program generate [] when viewing all guests names? ----------------------------------------------- import java.util.*; public class...

    Why does my program generate [] when viewing all guests names? ----------------------------------------------- import java.util.*; public class Delete extends Info { String name; int Id; int del; public Delete() { } public void display() { Scanner key = new Scanner(System.in); System.out.println("Input Customer Name: "); name = key.nextLine(); System.out.println("Enter ID Number: "); Id = key.nextInt(); System.out.println("Would you like to Delete? Enter 1 for yes or 2 for no. "); del = key.nextInt(); if (del == 1) { int flag = 0; //learned...

  • Consider the encryption code  Encryption Code true false - stringContains is an encryption algorithm that can be...

    Consider the encryption code  Encryption Code true false - stringContains is an encryption algorithm that can be decrpyted true false - Running reverseString twice will have no effect true false - Running incLetters(s,4) then incLetters(s,-4) will have no effect true false - using maxDigit as an encryption algorighm, it can be decrypted USE CODE BELOW: package encryption; import java.util.Scanner; public class Encryption { public static void main(String[] args) { Scanner scanner = new Scanner (System.in); String password, encryptedPassword, salt; int increment;...

  • 8.16 Ch 8, Part 1: XOR Cipher Write this program using Eclipse. Comment and style the...

    8.16 Ch 8, Part 1: XOR Cipher Write this program using Eclipse. Comment and style the code according to CS 200 Style Guide. Submit the source code files (.java) below. Make sure your source files are encoded in UTF-8. Some strange compiler errors are due to the text encoding not being correct. This program will implement a simple XOR cipher on an integer array, where the data to be encoded is XOR'd with a key. This idea is often used...

  • Computer Science C++ Help, here's the question that needs to be answered (TASK D): Task D....

    Computer Science C++ Help, here's the question that needs to be answered (TASK D): Task D. Decryption Implement two decryption functions corresponding to the above ciphers. When decrypting ciphertext, ensure that the produced decrypted string is equal to the original plaintext: decryptCaesar(ciphertext, rshift) == plaintext decryptVigenere(ciphertext, keyword) == plaintext Write a program decryption.cpp that uses the above functions to demonstrate encryption and decryption for both ciphers. It should first ask the user to input plaintext, then ask for a right...

  • Please fix my code so I can get this output: Enter the first 12-digit of an...

    Please fix my code so I can get this output: Enter the first 12-digit of an ISBN number as a string: 978013213080 The ISBN number is 9780132130806 This was my output: import java.util.Scanner; public class Isbn { private static int getChecksum(String s) { // Calculate checksum int sum = 0; for (int i = 0; i < s.length(); i++) if (i % 2 == 0) sum += (s.charAt(i) - '0') * 3; else sum += s.charAt(i) - '0'; return 10...

  • Study the VIGENÈRE CIPHER and implemented it with c++ 1.In your program, you should have two...

    Study the VIGENÈRE CIPHER and implemented it with c++ 1.In your program, you should have two functions: encryption and decryption. 2. Use any key you like. (Don't use deceptive in the slides) 3. choose a sentence or a paragraph you like as the plaintext. I have the code I just need the implementation in a different way // C++ code to implement Vigenere Cipher #include<bits/stdc++.h> using namespace std; // This function generates the key in // a cyclic manner until...

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