0

我有一行文本需要解密为密文。

假设我的文本行是abc def ghijklm n opq rstu vwx yz

我想要这样的输出:aei qu c k rvzdhmptxbfjn y glosm

假设我输入了我的“密钥”为 5。然后代码将输入文本文件中文本字符串数组的每个第 5 个元素。

这是我想出的代码,我在做什么方面遇到了困难。

import java.io.*;
import java.util.*;

public class Files1 {


public static void main(String[] args) {
    // TODO Auto-generated method stub
    Scanner input = new Scanner(System.in);
    int key;

    System.out.print("Enter file: ");
    String fileName = input.nextLine();
    System.out.print("Please enter your Cipher Key: ");
    key = input.nextInt();

    Scanner inputStream = null;
    System.out.println("File name is: " + fileName);

    try {
        inputStream = new Scanner(new File(fileName));
    } catch (FileNotFoundException e) {
        System.out.println("Error opening the file" + fileName);
        System.exit(0);
    }

    while (inputStream.hasNextLine()) {
        String text = inputStream.nextLine();
        System.out.println(text);

        char arrayText[] = text.toCharArray();
        for (int i = 0; i < arrayText.length; i += key) {
            System.out.print("\n" + arrayText[i]);
        }

    }
}

}

这是控制台中发生的事情:

Enter file: abc.txt

File name is: abc.txt
abc def ghijklm n opq rstu vwx yz

a

e

i



q

u
4

3 回答 3

1

你需要的是一个循环列表。

这是一个使用数组的循环列表的非常简单粗暴的实现。

import java.util.Iterator;
import java.util.List;

public class CircularList implements Iterator<String> {

    private String[] list;

    private int pointerIndex;

    private int key;

    public CircularList(String[] list, int key) {
        this.list = list;
        pointerIndex = 1 - key;
        this.key = key;
    }

    @Override
    public boolean hasNext() {
        if(list.length == 0){
            return false;
        }
        return true;
    }

    @Override
    public String next() {
        if(pointerIndex + key > list.length) {
            int diff = (list.length-1) - pointerIndex;
            pointerIndex = key - diff;
            return  list[pointerIndex];
        }else {
            pointerIndex = pointerIndex + key;
            return list[pointerIndex];
        }
    }

    @Override
    public void remove() {
        //Do Nothing
    }

}

一旦你有了一个可以循环迭代的列表,你就可以将现有的实现更改为 -

import java.io.*;
import java.util.*;

public class Files1 {

    public static void main(String[] args) {

        System.out.print("Enter file: ");
        Scanner input = new Scanner(System.in);
        String fileName = input.nextLine();
        Scanner inputStream = null;
        System.out.println("" + fileName);

        try {
            inputStream = new Scanner(new File(fileName));
        } catch (FileNotFoundException e) {
            System.out.println("Error opening the file: " + fileName);
            System.exit(0);
        }

        while (inputStream.hasNextLine()) {
            String text = inputStream.nextLine();
            System.out.println(text);

            String[] splits = text.split("");
            CircularList clist = new CircularList(splits, 5);

            for (int i = 0; i < splits.length -1; i += 1) {
                System.out.print("" + clist.next());
            }

        }
    }

}

输出 -

Enter file: resources\abc.txt
resources\abc.txt
abc def ghijklm n opq rstu vwx yz
aei qu c k rvzdhmptxbfjn  y glosw

此外,密码中的最后一个字符应该是“w”而不是“m”。

于 2013-03-22T02:42:23.723 回答
0

您没有指定空格应该发生什么,或者在需要环绕时会发生什么,但假设空格很重要并且环绕自然会发生:

for (int i = 0; i < text.length(); i++)
{
    System.out.print(text.charAt((i*5) % text.length()));
}

prints aei qu c k rvzdhmptxbfjn y glosw,这强烈表明您的预期输出中有错误。

于 2013-03-22T03:13:33.527 回答
0

导入 java.io。; 导入 java.util。;

公共类文件1 {

public static void main(String[] args) {
    // TODO Auto-generated method stub
    Scanner input = new Scanner(System.in);
    int key;

    System.out.print("Enter file: ");
    String fileName = input.nextLine();
    System.out.print("Please enter your Cipher Key: ");
    key = input.nextInt();

    Scanner inputStream = null;
    System.out.println("File name is: " + fileName);

    try {
        inputStream = new Scanner(new File(fileName));
    } catch (FileNotFoundException e) {
        System.out.println("Error opening the file" + fileName);
        System.exit(0);
    }

    while (inputStream.hasNextLine()) {
        String text = inputStream.nextLine();
        System.out.println(text);
        for (int i = 0; i < text.length(); i++) {
            System.out.print(text.charAt((i * key) % text.length()));
        }

    }
}

}

非常感谢 EJP 和 Pai!

我学到了很多!

于 2013-03-22T13:17:08.663 回答