Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/375.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 自定义密码加密/解密流_Java_Encryption_Cryptography - Fatal编程技术网

Java 自定义密码加密/解密流

Java 自定义密码加密/解密流,java,encryption,cryptography,Java,Encryption,Cryptography,这是我在这里的第一个问题,所以如果您有任何我可以改进的地方(帖子和编码风格),我很高兴知道 我知道我的编码是非常有效的,所以我很乐意接受您的建议和改进 目前,我正在编写一个atbashciper程序,它必须对输入的字符串进行加密和解密。对于大学里的每一项任务,都有一些指导原则,我们需要遵循,但我将首先解释我当前的代码: 导入java.util.Scanner public class AtbashCipher { StringBuilder code = new StringBuild

这是我在这里的第一个问题,所以如果您有任何我可以改进的地方(帖子和编码风格),我很高兴知道

我知道我的编码是非常有效的,所以我很乐意接受您的建议和改进

目前,我正在编写一个
atbashciper
程序,它必须对输入的
字符串进行加密和解密。对于大学里的每一项任务,都有一些指导原则,我们需要遵循,但我将首先解释我当前的代码:

导入java.util.Scanner

public class AtbashCipher {

    StringBuilder code = new StringBuilder("");
    String inputString;
    String coded;

    public String getInputString() {
        return this.inputString;
    }

    public void setInputString(String inputString) {
        this.inputString = inputString;
    }

    public String getCoded() {
        return this.coded;
    }

    public void setCoded(String coded) {
        this.coded = coded;
    }

    public char encode(char c) {
        String alphabet = "abcdefghijklmnopqrstuvwxyz";
        char help = Character.toLowerCase(c);
        if (alphabet.indexOf(help) >= 0) {
            for (int i = 0; i < alphabet.length(); i++) {
                if (help == alphabet.toLowerCase().charAt(i)) {
                    c = alphabet.charAt(26 - i - 1);
                    code.append(c);
                }
            }
        } else {
            code.append(c);
        }

        if (inputString.length() == code.length()) {
            System.out.println("Encode: " + code);
            coded = code.toString();
            inputString = coded;
            code.setLength(0);
        }
        return c;
    }

    public char decode(char c) {
        String alphabet = "abcdefghijklmnopqrstuvwxyz";
        char help = Character.toLowerCase(c);
        if (alphabet.indexOf(help) >= 0) {
            for (int i = 0; i < alphabet.length(); i++) {
                if (help == alphabet.toLowerCase().charAt(i)) {
                    c = alphabet.charAt(26 - i - 1);
                    code.append(c);
                }
            }
        } else {
            code.append(c);
        }

        if (inputString.length() == code.length()) {
            coded = code.toString();
            inputString = coded;
            System.out.println("Decode: " + code);
            code.setLength(0);
        }
        return c;
    }

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        AtbashCipher atb = new AtbashCipher();
        System.out.print("To be translated: ");
        atb.setInputString(sc.nextLine());
        sc.close();
        for (int j = 0; j < atb.getInputString().length(); j++) {
            char help = atb.getInputString().charAt(j);
            atb.encode(help);
        }
        for (int k = 0; k < atb.getInputString().length(); k++) {
            char help = atb.getInputString().charAt(k);
            atb.decode(help);
        }
    }
}
此外,我们还需要重写Java给出的
write
flush
close
方法

public void write(char[] cbuf, int off, int len)
public void flush()
public void close()
对于
阅读器
我们也需要这样做

我真的很难理解创建自己的
流并将其实现到代码中的概念

你能给我解释一下它是如何工作的,以及我的代码是如何更高效的吗?错误等

请不要仅仅链接java库,因为我在理解库中的概念时有一些困难,我总是查找一些示例

这项任务来自大学,但我没有得到任何分数,我只想做它并为考试进行练习

感谢您的帮助。 谢谢:)

编辑:我不能编辑我文章的第一行,所以。。。嘿,伙计们。 Edit2:我刚刚看到我的方法没有完全按照我想要的方式运行,我会尝试纠正它们。 编辑3:我编辑了这个节目,想把作者发到今天

Edit4:很晚了,但仍然没有忘记:我完成了并添加到这里,问题解决了:)

私人读者库;
私有密码;
公共AtbashReader(阅读器库,AtbashCipher){
this.base=base;
this.cipher=密码;
}
公共整数读取(char[]cbuf,int off,int len)引发IOException{
int result=base.read(cbuf、off、len);
对于(int i=off;i<(off+len);i++){
cbuf[i]=密码解码(cbuf[i]);
}
返回结果;
}
public void close()引发IOException{
base.close();
}

我试着打个比方。假设你已经有了一台可以重新油漆汽车的机器。很简单:你给它一辆车,机器给它上漆。这台机器相当于一个
编写器
,它接受一个字符数组并将其写入

现在的问题是,汽车在喷漆前需要清洗。那么,你怎么能制造一台洗车然后再喷漆的机器呢。你可以从头开始造一台新机器。或者你可以建造一台重复使用喷漆机的机器。你的机器会取一辆车,清洗它(这是你的工作),然后把清洗过的车交给已经存在的机器。这台大机器就是你的
AtbashWriter
:它写字符数组,但在写它们之前,它对它们进行编码。它是如何做到的?通过对字符数组进行编码,然后将其传递给已经知道如何写入的机器:基本写入程序

因此,基本上,您的write()方法应该如下所示:

public void write(char[] cbuf, int off, int len) {
    // transform the dirty car into a washed car
    char[] encodedChars = encode(cbuf, off, len);

    // paint the washed car
    baseWriter.write(encodedChars, 0, encodedChars.length);
}

希望这能让您开始。

谢谢您的回复,我明天会研究一下,因为我现在太累了,但我也对代码做了一些修改。您的类比让问题变得更清楚了,现在我知道如何解决这个问题。因此构造函数接受我的cipher对象,该对象包含字符串(?)等各种值,编写器只是将所有内容写入文件或流中。不知何故,我不明白我的密码和作者之间的区别。编写器不应该保存我的编码字符串吗?密码是做什么的?非常感谢。我尝试了你的答案,但我不完全理解AtbashCipher“cipher”和char[]encodedChars=encode(cbuf,off,len)背后的原因;我是否需要更改编码/解码方法,以便为其提供所需的参数?我们的编码方法(据我所知)只应该接收字符。谢谢你的帮助如果它可以编码一个字符,那么你可以用它来编码几个字符,不是吗?只需对每个要编码的字符调用encode()。编写器有一个方法
write(char[]cbuf,int off,int len)
,所以您需要实现它。谢谢您的帮助,我解决了这个问题,但没有时间回复。结果是在编辑,它的工作和问题得到解决:)
private Reader base;
private AtbashCipher cipher;

public AtbashReader(Reader base, AtbashCipher cipher) {
    this.base = base;
    this.cipher = cipher;
}

public int read (char[] cbuf, int off, int len) throws IOException{
    int result= base.read(cbuf, off, len);

    for(int i = off; i < (off+len); i++){
        cbuf[i] = cipher.decode(cbuf[i]);
    }
    return result;
}

public void close()throws IOException {
    base.close();
}
public void write(char[] cbuf, int off, int len) {
    // transform the dirty car into a washed car
    char[] encodedChars = encode(cbuf, off, len);

    // paint the washed car
    baseWriter.write(encodedChars, 0, encodedChars.length);
}