0

我必须使用 vigenere 密码加密一些文件(jpg)。我写了一些代码,但加密和解密后我的文件已损坏。图像的前 1/4 显示正常,但其余部分已损坏。这是我的代码:

@Override
public byte[] encryptFile(byte[] file, String key) {
    char[] keyChars = key.toCharArray();
    byte[] bytes = file;
    for (int i = 0; i < file.length; i++) {
        int keyNR = keyChars[i % keyChars.length] - 32;
        int c = bytes[i] & 255;
        if ((c >= 32) && (c <= 127)) {
            int x = c - 32;
            x = (x + keyNR) % 96;
            bytes[i] = (byte) (x + 32);
        }
    }
    return bytes;
}


@Override
public byte[] decryptFile(byte[] file, String key) {
    char[] keyChars = key.toCharArray();
    byte[] bytes = file;
    for (int i = 0; i < file.length; i++) {
        int keyNR = keyChars[i % keyChars.length] - 32;
        int c = bytes[i] & 255;
        if ((c >= 32) && (c <= 127)) {
            int x = c - 32;
            x = (x - keyNR + 96) % 96;
            bytes[i] = (byte) (x + 32);
        }
    }
    return bytes;
}

我做错什么了?

编辑:

读取和写入文件:

public void sendFile(String selectedFile, ICipher cipher, String key) {
    try {
        DataOutputStream outStream = new DataOutputStream(client
                .getOutputStream());
        outStream.flush();
        File file = new File(selectedFile);
        FileInputStream fileStream = new FileInputStream(file);
        long fileSize = file.length();
        long completed = 0;
        long bytesLeft = fileSize - completed;
        String msg = "SENDING_FILE:" + file.getName() + ":" + fileSize;
        outStream.writeUTF(cipher.encryptMsg(msg, key));
        while (completed < fileSize) {
            int step = (int) (bytesLeft > 150000 ? 150000 : bytesLeft);
            byte[] buffer = new byte[step];
            fileStream.read(buffer);
            buffer = cipher.encryptFile(buffer, key);
            outStream.write(buffer);
            completed += step;
            bytesLeft = fileSize - completed;
        }
        outStream.writeUTF(cipher.encryptMsg("SEND_COMPLETE", key));
        fileStream.close();
    } catch (IOException e) {
        e.printStackTrace();
    }

}

    private void downloadFile(String fileName, int fileSize,DataInputStream input,ICipher cipher, String key) {
    try {
        FileOutputStream outStream = new FileOutputStream("C:\\" + fileName);
        int bytesRead = 0, counter = 0;

        while (counter < fileSize) {
            int step = (int) (fileSize > 150000 ? 150000 : fileSize);
            byte[] buffer = new byte[step];
            bytesRead = input.read(buffer);
            if (bytesRead >= 0) {
                buffer = cipher.decryptFile(buffer, key);
                outStream.write(buffer, 0, bytesRead);
                counter += bytesRead;
            }
            if (bytesRead < 1024) {
                outStream.flush();
                break;
            }
        }

        Display.getDefault().syncExec(new Runnable() {
            @Override
            public void run() {
                window.handleMessage("Download sucessfully");
            }
        });
        outStream.close();

    } catch (Exception e) {
        Display.getDefault().syncExec(new Runnable() {
            @Override
            public void run() {
                window.handleMessage("Error on downloading file!");
            }
        });
    }
}
4

1 回答 1

2

您可以在来自磁盘 I/O 的任何块中对文件进行编码:

        int step = (int) (bytesLeft > 150000 ? 150000 : bytesLeft);
        byte[] buffer = new byte[step];
        fileStream.read(buffer);
        buffer = cipher.encryptFile(buffer, key);

但是你解码来自网络 I/O 的任何块中的文件:

        bytesRead = input.read(buffer);
        if (bytesRead >= 0) {
            buffer = cipher.decryptFile(buffer, key);
            outStream.write(buffer, 0, bytesRead);
            counter += bytesRead;
        }

这些块很可能不同意。磁盘 I/O 可能总是给你完整的块(你很幸运),但网络 I/O 可能会给你包大小的块(1500 字节减去标题)。

密码应该在已经编码/解码的数据中获得一个偏移量(或一次编码/解码所有内容),并使用它来适当地移动密钥,否则可能会发生这种情况:

original: ...LOREM IPSUM...
key     : ...abCde abCde...
encoded : ...MQUIR JRVYR...
key     : ...abCde Cdeab... <<note the key got shifted
decoded : ...LOREM GNQXP... <<output wrong after the first chunk.

由于数据包数据大小(对于以太网大小的 TCP/IP 数据包)以四个字节对齐,因此长度为 4 的密钥可能总是对齐。


另一个问题是您在上传文件时忽略了从磁盘读取的字节数。虽然磁盘 I/O 可能总是为您提供完整大小的块(文件可能是内存映射的,或者底层的原生 API 确实提供了这种保证),但没有什么是理所当然的。始终使用实际读取的字节数:bytesRead = fileStream.read(buffer);

于 2012-11-14T21:47:47.777 回答