Question

In: Computer Science

Needs to be in basic JAVA Write a program that does basic encrypting of text. You...

Needs to be in basic JAVA

Write a program that does basic encrypting of text. You will ask the user the filename of a text file that contains a few sentences of text. You will read this text file one character at a time, and change each letter to another one and write out to an output file. The conversion should be done a -> b, b->c , … z->a, A->B, B->C, …. Z->A. This means just shift each letter by one, but Z goes back to A.

Example: Hello converts to Ifmmp

All other punctuation and spaces or symbols can stay as they are.  

If you have time you could write the reverse program that would take your encrypted file as the input and get back the original message

Solutions

Expert Solution

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;

public class EncryptDecrypt
{
   public static void encrypt(String inFile, String outFile) throws IOException {
       File f=new File(inFile); //Creation of File Descriptor for input file
   FileReader fr=new FileReader(f); //Creation of File Reader object
  
   //creating file writer object
   FileWriter fw = new FileWriter(new File(outFile));
   BufferedReader br=new BufferedReader(fr); //Creation of BufferedReader object
   int c = 0;   
   while((c = br.read()) != -1) //Read char by Char
   {
       //shifting by 1
           int ch = c;
           //checkking if upper case or lower case and converting accordingly
           if(Character.isUpperCase(ch)) {
               ch = ((c+1)-65 ) % 26 + 65;
           } else if(Character.isLowerCase(ch)) {
               ch = ((c+1)-97 ) % 26 + 97;
           }
   char character = (char) ch; //converting integer to char
   //qriting to output file
   fw.append(character);
   }
  
   br.close();
   fw.close();
   }
  
   public static void decrypt(String file) throws IOException {
       File f=new File(file); //Creation of File Descriptor for input file
   FileReader fr=new FileReader(f); //Creation of File Reader object
  
   BufferedReader br=new BufferedReader(fr); //Creation of BufferedReader object
   int c = 0;   
   while((c = br.read()) != -1) //Read char by Char
   {
       //shifting by 1
       int ch = c;
          
           if(Character.isUpperCase(ch)) {
               ch = ((c-1)-65 ) % 26 + 65;
           } else if(Character.isLowerCase(ch)) {
               ch = ((c-1)-97 ) % 26 + 97;
           }
   char character = (char) ch; //converting integer to char
   System.out.print(character);
   }
  
   br.close();
   }
public static void main(String[] args) throws IOException
{
encrypt("D://input.txt", "D://output.txt");
decrypt("D://output.txt");
  
}
}


Related Solutions

Write the program in java Write a program that does basic encrypting of text. You will...
Write the program in java Write a program that does basic encrypting of text. You will ask the user the filename of a text file which contains a few sentences of text. You will read this text file one character at a time, and change each letter to another one and write out to an output file. The conversion should be done a -> b, b->c , … z->a, A->B, B->C, …. Z->A. This means just shift each letter by...
Encrypting Text with a Caesar Cipher Write a C program caesar.c which reads characters from its...
Encrypting Text with a Caesar Cipher Write a C program caesar.c which reads characters from its input and writes the characters to its output encrypted with a Caesar cipher. A Caesar cipher shifts each letter a certain number of positions in the alphabet. The number of positions to shift will be given to your program as a command line argument. Characters other than letters should not be encrypted. Your program should stop only at the end of input. Your program...
Write Java program Lab52.java which reads in a line of text from the user. The text...
Write Java program Lab52.java which reads in a line of text from the user. The text should be passed into the method: public static String[] divideText(String input) The "divideText" method returns an array of 2 Strings, one with the even number characters in the original string and one with the odd number characters from the original string. The program should then print out the returned strings.
Write a Java program that will encode plain text into cipher text and decode cipher text...
Write a Java program that will encode plain text into cipher text and decode cipher text into plain text. Create following methods and add them to your class: • a method named encode(String p, int key) that takes a plain text p and encodes it to a cipher text by adding the key value to each alphabet character of plain text p using the ASCII chart. The method should return the encoded String. For example, if p = "attack at...
Please write a java program to write to a text file and to read from a...
Please write a java program to write to a text file and to read from a text file.
Needs to be in JAVA. Write a Java program that accepts the total amount of cars...
Needs to be in JAVA. Write a Java program that accepts the total amount of cars sold and total sales amount of a car salesperson for a given month. The salesperson’s paycheck is computed as follows: a. Every sales person gets 10% (commission) of total sales b. Sales totals greater than $50,000 get 5% of total sales amount c. 8 or more cars sold earns the salesperson an extra 3% Please remove 30% (taxes) of the gross pay and list...
Write a Java program that will test lines of text to see if they are palindromes....
Write a Java program that will test lines of text to see if they are palindromes. The program should prompt the user for the name of a file to process, then open and read the file of possible palindromes (one per line of text). The program should then print the total number of lines read, and total number of palindromes.
URGENT!! DO THIS CODE IN JAVA Write a complete Java FX program that displays a text...
URGENT!! DO THIS CODE IN JAVA Write a complete Java FX program that displays a text label and a button.When the program begins, the label displays a 0. Then each time the button is clicked, the number increases its value by 1; that is each time the user clicks the button the label displays 1,2,3,4............and so on.
Write a program in java processing. Write a program that does the following: · Assume the...
Write a program in java processing. Write a program that does the following: · Assume the canvas size of 500X500. · The program asks the user to enter a 3 digit number. · The program then checks the value of the first and last digit of the number. · If the first and last digits are even, it makes the background green and displays the three digit number at the mouse pointer. · If the two digits are odd, it...
Language: c++ using visual basic Write a program to open a text file that you created,...
Language: c++ using visual basic Write a program to open a text file that you created, read the file into arrays, sort the data by price (low to high), by box number (low to high), search for a price of a specific box number and create a reorder report. The reorder report should alert purchasing to order boxes whose inventory falls below 100. Sort the reorder report from high to low. Inventory data to input. Box number Number boxes in...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT