Java 如何创建替换关键字密码

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/23267607/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 21:35:21  来源:igfitidea点击:

How to create a substitution keyword cipher

javaencryptionkeywordsubstitution

提问by user3382405

I am trying to develop a substitution cipher that uses a keyword to create a new cipher alphabet. I am new to Java (as I'm sure you will be able to tell!) and I am finding it hard to wrap my head around the code for what I need to do.

我正在尝试开发一种使用关键字创建新密码字母表的替换密码。我是 Java 的新手(因为我相信你会知道的!)而且我发现很难将我的头脑围绕在我需要做的代码上。

My understanding is as follows:

我的理解如下:

If for example, the keyword is javben, I should start by finding the index of the "j" in the plainText string array, which is 9. I then want to shift the plainText[9] into cipherText[0] and move each other element over by 1. So the first pass of this would result in:

例如,如果关键字是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"}

Then I would find the "a" and it's it's already where it should be so I'll need to account for this and not shift it -- somehow. The next character is the "v" and so the process would continue.

然后我会找到“a”,它已经在它应该在的地方,所以我需要考虑到这一点而不是改变它——不知何故。下一个字符是“v”,因此该过程将继续。

After shifting everything in the cipher I should end up with:

在转移密码中的所有内容后,我应该得到:

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"}

As you can see, I am reasonably sure that I understand the process of which to go through, however I am really struggling wrap my head around the code required for this. Help please!

正如您所看到的,我有理由确信我了解要经历的过程,但是我真的很努力地围绕着这个所需的代码。请帮忙!

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"};
}
}

回答by Basim Khajwal

This one is quite simple, just set up a function that for every letter in the key word it just takes it out of the alphabet array and then add the two arrays together with the array of letters at the beginning and the alphabet without those letters after it. E.g:

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

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()]);
}

回答by Kishor N R

I have done substitution cipher using following code

我使用以下代码完成了替换密码

import java.util.*;

class SubCipher
{

public static void main(String args[])
{
     String plainText = ",.<>;':\"[]{}-=_+)(*&^%$#\"@!),998683,1,x3x33,10~1,1,10~2,2,20";
    String strcipherText = Encrypt(plainText);
    String strdecryptedText = Decrypt(strcipherText);

System.out.println("Plain Text :"+plainText);
System.out.println("Encrypted Text :"+strcipherText);
System.out.println("Decrypted Text :"+strdecryptedText);     
}

private static String Encrypt(String text)
{
    byte[] textBytes = text.getBytes();
    for (int i = 0; i < textBytes.length; i++)
    {
        int currentByteValue = (int)textBytes[i];
        textBytes[i] = (byte)(currentByteValue > 255 ? currentByteValue - 255 + 2 : currentByteValue + 2);
    }
    String strbyte=new String(textBytes);
    return  strbyte;
}

private static String Decrypt(String text)
{
    byte[] textBytes = text.getBytes();
    for (int i = 0; i < textBytes.length; i++)
    {
        int currentByteValue = (int)textBytes[i];
        textBytes[i] = (byte)(currentByteValue < 0 ? currentByteValue + 255 - 2 : currentByteValue - 2);
    }
    String strbyte=new String(textBytes);
    return strbyte;
   }
}

回答by th3h3d

package Classes;

public class SubstitutionCipherClass {

    public static void main(String[] args) {
        char 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'};
        char cipherText[] = {'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'};

        String pt = "haci";

        for (int a = 0; a < pt.length(); a++) {
            for (int i = 0; i < plainText.length; i++) {
               if(plainText[i] == (pt.charAt(a))){
                   System.out.println(cipherText[i]);
               }
            }
        }
    }
}