gpt4 book ai didi

java - 如何创建替换关键字密码

转载 作者:行者123 更新时间:2023-11-30 03:55:38 24 4
gpt4 key购买 nike

我正在尝试开发一种替换密码,它使用关键字来创建新的密码字母表。我是 Java 新手(我相信你会知道!)并且我正在找到它很难理解我需要做什么的代码。

我的理解如下:

例如,如果关键字是 javben,我应该首先在纯文本字符串数组中查找“j”的索引,即 9。然后我想移动纯文本[9 ] 到 cipherText[0] 中,并将每个其他元素移动 1。因此,第一遍将导致:

cipherText[] = {"j","a","b","c","d","e","f","g","h","i","k","l","m","n","o","p","q","r","s","t","u","v","w","r","x","y","z"}

然后我会找到“a”,它已经在它应该在的位置了,所以我需要考虑到这一点,而不是以某种方式移动它。下一个字符是“v”,因此该过程将继续。

在改变密码中的所有内容后,我最终应该得到:

plainText []= {"a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","r","x","y","z"}
cipherText[]= {"j","a","v","b","e","n","c","d","f","g","h","i","k","l","m","o","p","q","r","s","t","u","w","r","x","y","z"}

正如您所看到的,我相当确定我了解要经历的过程,但是我真的很难理解所需的代码。请帮助!

import java.util.Scanner;
import java.io.*;
/**
* This program uses a keyword for a simple substitution cipher.
*
* @author Bryan
* @version Programming Project
*/

public class Cipher
{
// The main method removes duplicate characters in a word input by the user.
public static void main(String[] args) throws IOException
{
// Creatae a new scanner object for keyboard input
Scanner keyboard = new Scanner(System.in);

// prompt the user to enter a word
System.out.println("Please enter your keyword: ");
// and get their input
String input = keyboard.nextLine();
// the keyword will be built up here
String keyword = "";

while(input.length() > 0)
{
// get the first letter
char letter = input.charAt(0);
// if the letter is not already in the output
if (keyword.indexOf(letter) == -1)
{
// add it to the end
keyword = keyword + letter;
}

// that letter is processed : discard it
input = input.substring(1);
}

//this is just to confirm the duplicate letters in the keyword are removed
System.out.println(keyword);
getFile();

}

/**
* This asks the user to specify a filename which is then
* read into the program for enciphering
*/

public static void getFile()throws IOException
{
// Creatae a new scanner object for keyboard input
Scanner keyboard = new Scanner(System.in);

// Get the file name
System.out.println("Enter the file name: ");
String filename = keyboard.nextLine();

//Open the file
File file = new File(filename);
Scanner inputFile = new Scanner(file);

// Read the lines from the file until no more are left
while (inputFile.hasNext())
{
//Read the next line
String allText = inputFile.nextLine();

// Display the text
System.out.println(allText);
}




//Close the file
inputFile.close();

}

public static void alphabet()
{
String[] plainText = {"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"};
String[] cipherText = {"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"};
}
}

最佳答案

这个很简单,只需设置一个函数,对于关键字中的每个字母,只需将其从字母表数组中取出,然后将两个数组与开头的字母数组和不带字母的字母表相加。后面的那些字母。例如:

String[] cipherKeyWord(String keyWord, String[] alphabet){
ArrayList<String> finalCipher = (ArrayList) Arrays.asList(keyWord.split("(?!^)"));
//^ This splits it into a string of every word using regular expressions

ArrayList<String> newAlphabet = (ArrayList) Arrays.asList(alphabet);

newAlphabet.removeAll(finalCipher);

finalCipher.addAll(newAlphabet);

return finalCipher.toArray(new String[finalCipher.size()]);
}

关于java - 如何创建替换关键字密码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23267607/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com