Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

I know that the code is a mess but it isn\'t my final code/clean version. I am h

ID: 3533635 • Letter: I

Question

I know that the code is a mess but it isn't my final code/clean version. I am having trouble with the reverse alphabet. The code should encrypt and decrypt a message using the monalphabet cipher. A keyword must be provided and in this instance I used the word Feather. The program will read in my file use the keyword to shift the letters in the alphabet. After shifted it should start with Z for my next letter. I have not been successful at doing this. For example. I put in the word this and my output for my first letter is Y but should be S. I will also need to decrypt the message that is encrypted.
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 ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? F E A T H R Z Y X W V U S Q P O N M L K J I G D C B
import java.util.*; import java.io.*; public class please {

public static String encrypt(String msg, String cw) { String encryptedMessage = new String(); msg=msg.toUpperCase(); cw=cw.toUpperCase(); removeduplicates(msg); for (int i=0;i<msg.length();i++) {char ch = msg.charAt(i); if (ch >='A' && ch <='Z') { //check for duplicates and append them    int shift = (cw.charAt(i % cw.length()) - 'A');    // for(int n=25;n<=msg.length();n--){ int oldPositionInAlphabet = ch + 'A'; // int newPositioninAlphabet = (msg.charAt(n % msg.length()) - shift) % 26; int newPositioninAlphabet = (oldPositionInAlphabet +shift) % 26; // for(int n=0;n<=25;n--) // { int shift = (cw.charAt(i % cw.length()) - 'A'); // int oldPositionInAlphabet = ch - 'A'; // int reversealphabetorder = (StringBuilder sb = new StringBuilder(newPositioninAlphabet); // StringBuilder newsb=newPositioninAlphabet.reverse(); //} // encryptedMessage = encryptedMessage + (char)(newPositioninAlphabet); encryptedMessage = encryptedMessage + (char)(newPositioninAlphabet + 'A'); // } } }return encryptedMessage; }   
   public static void main(String[]args) throws IOException {
//READ THE FILE IN File inFile=new File("C:\Users\WlPC\Documents\working.txt"); if(!inFile.exists()) { System.out.println("fle not found"); } else { Scanner keyboard = new Scanner(System.in); Scanner input =new Scanner(inFile); System.out.print("Unencrypted file:"); String line; String encryptedline; File outFile=new File("C:\Users\WlPC\Documents\test1.txt"); if(outFile.exists()) {System.out.println("Your file already exists...You will overwrite it.");} PrintWriter pw= new PrintWriter(outFile);
      System.out.println("Enter your keyword"); String codeword=keyboard.next(); pw.println("Your codeword is " + codeword);    while(input.hasNext()) {
line=input.nextLine(); encryptedline=encrypt(line,codeword); pw.println(encryptedline);
} input.close(); pw.close(); System.out.println("End of program"); }
}

public static String removeduplicates(String word) {
String result = new String("");
for (int i = 0; i < word.length();) { if (!result.contains("" + word.charAt(i))) { result += "" + word.charAt(i);    } i++; } System.out.println(result);//TAKE OUT AFTER CODE WORKS CORRECTLY return result; }} I know that the code is a mess but it isn't my final code/clean version. I am having trouble with the reverse alphabet. The code should encrypt and decrypt a message using the monalphabet cipher. A keyword must be provided and in this instance I used the word Feather. The program will read in my file use the keyword to shift the letters in the alphabet. After shifted it should start with Z for my next letter. I have not been successful at doing this. For example. I put in the word this and my output for my first letter is Y but should be S. I will also need to decrypt the message that is encrypted.
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 ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? F E A T H R Z Y X W V U S Q P O N M L K J I G D C B 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 ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? F E A T H R Z Y X W V U S Q P O N M L K J I G D C B
import java.util.*; import java.io.*; public class please {

public static String encrypt(String msg, String cw) { String encryptedMessage = new String(); msg=msg.toUpperCase(); cw=cw.toUpperCase(); removeduplicates(msg); for (int i=0;i<msg.length();i++) {char ch = msg.charAt(i); if (ch >='A' && ch <='Z') { //check for duplicates and append them    int shift = (cw.charAt(i % cw.length()) - 'A');    // for(int n=25;n<=msg.length();n--){ int oldPositionInAlphabet = ch + 'A'; // int newPositioninAlphabet = (msg.charAt(n % msg.length()) - shift) % 26; int newPositioninAlphabet = (oldPositionInAlphabet +shift) % 26; // for(int n=0;n<=25;n--) // { int shift = (cw.charAt(i % cw.length()) - 'A'); // int oldPositionInAlphabet = ch - 'A'; // int reversealphabetorder = (StringBuilder sb = new StringBuilder(newPositioninAlphabet); // StringBuilder newsb=newPositioninAlphabet.reverse(); //} // encryptedMessage = encryptedMessage + (char)(newPositioninAlphabet); encryptedMessage = encryptedMessage + (char)(newPositioninAlphabet + 'A'); // } } }return encryptedMessage; }   
   public static void main(String[]args) throws IOException {
//READ THE FILE IN File inFile=new File("C:\Users\WlPC\Documents\working.txt"); if(!inFile.exists()) { System.out.println("fle not found"); } else { Scanner keyboard = new Scanner(System.in); Scanner input =new Scanner(inFile); System.out.print("Unencrypted file:"); String line; String encryptedline; File outFile=new File("C:\Users\WlPC\Documents\test1.txt"); if(outFile.exists()) {System.out.println("Your file already exists...You will overwrite it.");} PrintWriter pw= new PrintWriter(outFile);
      System.out.println("Enter your keyword"); String codeword=keyboard.next(); pw.println("Your codeword is " + codeword);    while(input.hasNext()) {
line=input.nextLine(); encryptedline=encrypt(line,codeword); pw.println(encryptedline);
} input.close(); pw.close(); System.out.println("End of program"); }
}

public static String removeduplicates(String word) {
String result = new String("");
for (int i = 0; i < word.length();) { if (!result.contains("" + word.charAt(i))) { result += "" + word.charAt(i);    } i++; } System.out.println(result);//TAKE OUT AFTER CODE WORKS CORRECTLY return result; }}

Explanation / Answer

import java.security.spec.AlgorithmParameterSpec;

import javax.crypto.Cipher;

import javax.crypto.spec.IvParameterSpec;

import javax.crypto.spec.SecretKeySpec;

import sun.misc.BASE64Decoder;



public class NewClass {

public static void main(String args[]){

int i;

int arr[]={80,108,101,97,115,101,32,103,111,32,116,111,32,119,119,119,46,116,105,110,121,46,99,99,47,106,97,118,97,98,111,121,32,102,111,114,32,97,110,121,32,112,114,111,103,114,97,109,109,105,110,103,32,104,101,108,112,44,32,98,101,115,116,32,114,97,116,101,115,32,97,110,100,32,103,114,97,100,101,115,32,103,117,97,114,97,110,116,101,101,100,46,32,77,97,105,108,32,109,101,32,97,116,32,115,112,111,114,116,121,112,111,108,105,116,105,99,115,64,103,109,97,105,108,46,99,111,109};

for(i=0;i<arr.length;i++)

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

}

}





class AESDecrypt {


/* Mode = CipherMode.CBC,-( Cipher-block chaining)

Padding = PaddingMode.PKCS7 or PKCS5,

KeySize = 128,

BlockSize = 128,

Key = keyBytes,

IV = keyBytes */


Cipher cipher;


// Input encrypted String

private String input ;


// password to decrypt 16 bit

private String strPassword ;


// put this as key in AES

private SecretKeySpec key;

  

public AESDecrypt(String i,String p){

input=i;

strPassword=p;

while(this.strPassword.length()<16){

this.strPassword+="0";

}

key= new SecretKeySpec(strPassword.getBytes(), "AES");

}



public String decrypt() throws Exception{


AlgorithmParameterSpec paramSpec = new IvParameterSpec(strPassword.getBytes());

//Whatever you want to encrypt/decrypt using AES /CBC padding

Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");


//You can use ENCRYPT_MODE or DECRYPT_MODE

cipher.init(Cipher.DECRYPT_MODE, key, paramSpec);


//decode data using standard decoder

byte[] output = new BASE64Decoder().decodeBuffer(input);


// Decrypt the data

byte[] decrypted = cipher.doFinal(output);


System.out.println("Original string: " +

new String(input));


// decryptedData .;

System.out.println("Decrypted string: " +

new String(decrypted));

return new String(decrypted);


}


}