I am in a grade 11 Computer Science class, and we had an assignment to create a method that's gets data from a text file, and encrypts it using a method called encrypt. It should have two inputs, one for the message being read from the file, and one for the cipher alphabet that the user enters that corresponds with the alphabet (e.g. regular Alphabet="ABCDEFGHIJKLMNOPQRSTUVWXYZ" User Cipher Code="TWKUOVJZIQHFRXYSDAPLCNMBGE" , so A = T ). is should also be able to run in Dr.java . Here is what I have so far :
import java.util.Scanner;
import java.io.*;
/** The "EncryptionTest" class for Dr.Java
* Purpose: To encrypt a message using a monoalphabetic substitution
*/
// String x = "ABCDEFGHIJKLMNOPQRSTUVWXYZ" ;
// String y = "TWKUOVJZIQHFRXYSDAPLCNMBGE" ;
public class encryption1
{
/****
* Put your encrypt method here
*****/
public static String encrypt( String message, String cipherAlphabet)
{
message="DID YOU GET THIS?";//This is a Test to see if it works
message= message.toUpperCase();
cipherAlphabet= cipherAlphabet.toUpperCase();
for (int i=0;i<message.length() ;i++)
{
message.replace ('A',cipherAlphabet.charAt(0));
message.replace ('B',cipherAlphabet.charAt(1));
message.replace ('C',cipherAlphabet.charAt(2));
message.replace ('D',cipherAlphabet.charAt(3));
message.replace ('E',cipherAlphabet.charAt(4));
message.replace ('F',cipherAlphabet.charAt(5));
message.replace ('G',cipherAlphabet.charAt(6));
message.replace ('H',cipherAlphabet.charAt(7));
message.replace ('I',cipherAlphabet.charAt(8));
message.replace ('J',cipherAlphabet.charAt(9));
message.replace ('K',cipherAlphabet.charAt(10));
message.replace ('L',cipherAlphabet.charAt(11));
message.replace ('M',cipherAlphabet.charAt(12));
message.replace ('N',cipherAlphabet.charAt(13));
message.replace ('O',cipherAlphabet.charAt(14));
message.replace ('P',cipherAlphabet.charAt(15));
message.replace ('Q',cipherAlphabet.charAt(16));
message.replace ('R',cipherAlphabet.charAt(17));
message.replace ('S',cipherAlphabet.charAt(18));
message.replace ('T',cipherAlphabet.charAt(19));
message.replace ('U', cipherAlphabet.charAt(20));
message.replace ('V',cipherAlphabet.charAt(21));
message.replace ('W', cipherAlphabet.charAt(22));
message.replace ('X',cipherAlphabet.charAt(23));
message.replace ('Y',cipherAlphabet.charAt(24));
message.replace ('Z',cipherAlphabet.charAt(25));
}
String cipherCode=message;
return cipherCode; };
/***main method that controls the flow of this program *************************/
public static void main(String[] args) throws Exception
{
Scanner input = new Scanner (System.in);
// TextInputFile inFile = new TextInputFile ("plainText1a.txt");
//
// String cipherAlphabet = inFile.nextLine ();
// System.out.println (cipherAlphabet);
//
// TextOutputFile outFile = new TextOutputFile ("cipherText1a.txt");
// outFile.println (cipherAlphabet);
//
//
//declaring String objects to read and write to file.
// String plainText;
// String cipherText;
// String message;
// while (!inFile.eof ())
// {
// plainText = inFile.nextLine ();
// cipherText = encrypt (message);
// System.out.println (cipherText);
// outFile.println (cipherText);
// }
//
// inFile.close ();
// outFile.close ();
String Alphabet="ABCDEFGHIJKLMNOPQRSTUVWXYZ";
////////////READ FROM FILE////////////////////////////////////////////////////////
// File myFile = new File("plainText.txt");
// // Create a Scanner and associate it with the file
// Scanner infile = new Scanner(myFile);
//
// //Read the first line of the file and output it to the console (this is the title)
//
// // Loop through the contents of the file
// while (infile.hasNextLine())
// {
// //Store the Data from the file and display it the the console in a meaningful way
// plainText = infile.nextLine();
// }
//
// //We have read all the data in the file. Close it.
// infile.close();
///////// END READING FROM FILE////////////////////////////////////////////////////
//output welcome statement
System.out.println ("Welcome to the Encryption Machine!");
System.out.println ("this is the regular Alphabet (in order): ");
System.out.println("ABCDEFGHIJKLMNOPQRSTUVWXYZ");
System.out.println ("Please input YOUR cipher Alphabet in accordance so each letter coresponds to the regular Alphabet");
String cipherAlphabet= input.next();
String message="DID YOU GET THIS?";
//Encryption Proccess
String cipherCode= encrypt(message,cipherAlphabet);
System.out.println (cipherCode);
} // main method
} // end of EncryptionTest class
Aucun commentaire:
Enregistrer un commentaire