从文本文件中将一行文本转换为java中的密文

时间:2013-03-22 02:09:49

标签: java file text encryption

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

假设我的文字行是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

3 个答案:

答案 0 :(得分: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'。

答案 1 :(得分:0)

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

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

打印aei qu c k rvzdhmptxbfjn y glosw,强烈暗示您的预期输出错误。

答案 2 :(得分:0)

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);
        for (int i = 0; i < text.length(); i++) {
            System.out.print(text.charAt((i * key) % text.length()));
        }

    }
}

}

非常感谢EJP和Pai!

我学到了很多东西!