Java 如何使用加密程序创建解密程序?

Java 如何使用加密程序创建解密程序?,java,encryption,methods,Java,Encryption,Methods,如何将加密代码转换为用于解密?我不希望他们在同一个程序,这将在两个不同的文件中使用。我需要帮助,弄清楚我将如何将这段代码的一部分用于我的解密代码。基本上,我需要使用这段代码,并找到一种方法,使用它的一部分创建另一个程序来解密下面代码创建的代码 我用于加密的代码是: import java.util.*; public class Cipher //This class will encrpyt the program { public static char cipher (int j)

如何将加密代码转换为用于解密?我不希望他们在同一个程序,这将在两个不同的文件中使用。我需要帮助,弄清楚我将如何将这段代码的一部分用于我的解密代码。基本上,我需要使用这段代码,并找到一种方法,使用它的一部分创建另一个程序来解密下面代码创建的代码

我用于加密的代码是:

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
import java.util.*;
公共类密码//此类将加密程序
{
公共静态字符密码(intj){//该方法生成随机字母
char[]cipher1={'a','b','c','d','e','f','g'};//字符数组的字符数
j=(int)(Math.random()*cipher1.length);//从数组中选择一个随机元素
return cipher1[j];//这将返回字母
}//密码方法结束
公共静态void main(字符串[]args){//main方法
System.out.print(“请键入要加密的句子”;//要求用户输入要加密的单词
Scanner inputScanner=新扫描仪(System.in);//导入扫描仪读取器
String userinput=inputScanner.next();//将用户输入的单词分配给可变的userinput
userinput=userinput.toUpperCase();//将userinput转换为大写字母
int yu=userinput.length();//yu在userinput中查找字符的长度
char[]charArray=userinput.toCharArray();//将userinput发送到charArray
int w=1;//用于try-catch块
System.out.println(“请输入图案”);//提示输入图案
String pattern=inputScanner.next();//模式将决定在加密代码中输入多少个字符
int pattern2=Integer.parseInt(pattern);//将字符串值更改为整数值
做{
如果用户输入字母或十进制数字,请尝试{//try block捕获
w=2;

如果(pattern2No,则代码仅用于加密。从代码看,它似乎是一次键盘


对于解密,您需要了解反向逻辑,如了解接收者如何知道所做的更改以及如何撤销更改

您尝试了什么?您遇到了什么问题?我不确定程序的哪些部分将读取加密的代码并解密it@user3331406这是家庭作业吗?你知道加密算法的名称吗?例如凯撒密码等等?我想是凯撒密码,但我的老师没有说完全相同的我没有按照你说的做r说