简体   繁体   中英

How can I use my encryption program to create a decryption program?

How can I convert my encryption code to be used for decryption? I do not want them in the same program this will be used in two different files. I need help figuring out how I would take parts of this code to use for my decryption code. BASICALLY I NEED TO USE THIS CODE AND FIND A WAY TO USE PARTS OF IT TO CREATE ANOTHER PROGRAM TO DECRYPT CODES CREATED BY THE CODE BELOW.

The code I am using for the encryption is:

import java.util.*;

public class Cipher //This class will encrpyt the program
{

  public static char cipher (int j){ //this mehtod generates the random letter
    char[] cipher1 = {'a','b','c','d','e','f','g'}; //characters for char array
    j = (int) (Math.random() * cipher1.length);//choose a random element from the array
    return cipher1[j]; //this will return the letter
  } //end of cipher method

  public static void main (String[] args){ //main method

    System.out.print("Please type a sentence to be encrypted\n");//asks user for their word to be encrypted
    Scanner inputScanner = new Scanner(System.in); //imports scanner reader
    String userinput = inputScanner.next(); //assigns the word entered by user to varible userinput
    userinput = userinput.toUpperCase(); //userinput in transferred to upper case letters
    int yu = userinput.length(); //yu finds the length of charceters in userinput
    char[] charArray = userinput.toCharArray(); //sends userinput to charArray

    int w=1; //used for try catch block
    System.out.println("please enter pattern"); //prompt for pattern
    String pattern = inputScanner.next(); //pattern will decide how many characters will be input in the encrypted code
    int pattern2 = Integer.parseInt(pattern); //changes the string value to integer value

    do{ 
      try{ //try block to catch if user enters letters or decimal numbers
        w=2;
        if(pattern2<0){
          System.out.println("please enter a number above 0"); //prompt if user enters somthing below zero
          w=1;
        }
      }catch (NumberFormatException f){
        System.out.println("PLEASE ENTER A NUMBER!"); //prompt if user enters something user than a number
      }

    }while (w==1); //end of do and try catch block

    System.out.print("your encrypted code is: "); //prompt to give user encrypted code

    for(int i = 0; i < yu; i++){
      System.out.print(charArray[i]);
      for(int q = 0; q < pattern2; q++){
        System.out.print( cipher(1));
      } //end of for loop
    } //end of for loop

  } //end of main method
} //cipher class

No, the code is for encyption only. From the code it seems like it is One Time Pad .

For decryption, you will need to know the reverse logic like knowing how the receiver knows what changes have been made and how to undo them. Start with reading the theory behind the practice :)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM