【问题标题】:Encrypt byte array using vigenere cipher in java在java中使用vigenere密码加密字节数组
【发布时间】:2012-11-14 19:53:15
【问题描述】:

我必须使用 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!");
            }
        });
    }
}

【问题讨论】:

  • 我建议调查原始文件和解密文件中的字节以找出差异。
  • 您的代码仅适用于 ASCII 文本 (32..127)
  • 你如何从文件中读取/写入字节数组?如果您通过字符串读取/写入它们,那么您可能会遇到问题。
  • @stark 代码明确跳过不可打印的 ASCII 字节。
  • Jpeg 文件不是 ASCII

标签: java file encryption byte vigenere


【解决方案1】:

您将文件编码为来自磁盘 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);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多