java - RSA 逐 block 加密对大于 1kb 的文件产生空白输出

标签 java file encryption cryptography output

我不会为 AES 或其他加密打开此线程,因为这是我要用来加密 AES 和其他加密的 key 的内容。我从 StackOverflow 和其他一些网站收集了一些代码,并对其进行了编辑以适合我的程序,但是在尝试使用 RSA 进行逐 block 加密时,问题是我只能加密大小为 1 KB 的小文本文件。文本文件的加密和解密工作正常。但是,加密图片和任何大于 1 KB 的文件都会生成一个空白加密文件。

如果有人能帮我指出这段代码导致大于 1 KB 的文件导致空白输出文件/加密文件的位置,我想寻求帮助。这段代码适用于 AES,但我不确定为什么它不适用于 RSA 加密。问题始于 Encrypt_File 读取它的循环周围的某处。

代码:

public static final String ALGORITHM = "RSA";
private static final short KEY_LENGTH = 1024;
private static final short KEY_BYTES = 32;
private static final String CIPHER_EXTENSION = ".cgfile";

public void Encrypt_File(Path path_fileToEncrypt) {
    //get file name and the new file name
    String fileName = path_fileToEncrypt.getFileName().toString();
    String encryptedFileName = fileName+CIPHER_EXTENSION;
    String pathToEncryptedFile = path_fileToEncrypt.getParent()+File.separator+encryptedFileName;

    //attempt to open the public key
    try (ObjectInputStream publicKeyFile = new ObjectInputStream(new FileInputStream(PUBLIC_KEY_FILE))) {
        //load the key
        PublicKey publicKey = (PublicKey) publicKeyFile.readObject();

        // load file to encrypt and inputstream
        FileInputStream fileInputStream = new FileInputStream(new File(path_fileToEncrypt.toString()));

        // init the cipher
        final Cipher cipher = Cipher.getInstance(ALGORITHM);
        cipher.init(Cipher.ENCRYPT_MODE, publicKey);

        CipherOutputStream cos = new CipherOutputStream(new FileOutputStream(pathToEncryptedFile),cipher);
        byte[] buffer = new byte[KEY_BYTES];
        int count;
        while((count = fileInputStream.read(buffer))>0){
            cos.write(buffer, 0, count);
        }

        //close
        fileInputStream.close();
        cos.close();
        //delete fileToEncrypt since we have the encrypted file now
        DeleteFile(new File(path_fileToEncrypt.toString()));
        System.out.println("Finished encrypting "+ fileName +" It's new file name is "+ encryptedFileName);
    } catch (Exception e) {
        e.printStackTrace();
    }
}
public static void main(String[] args) {
    try {

        CGcipher rsaencrypt = new CGcipher();
        Path pathTosecret = Paths.get(System.getProperty("user.dir"), "pic.png");
        // Encrypt the string using the public key
        rsaencrypt.Encrypt_File(pathTosecret);
        //rsaencrypt.Decrypt_File(pathTosecret);

    } catch (Exception e) {
        System.out.println(e.toString());
    }
}

最佳答案

RSA 不适用于批量加密,因为它与 AES 等对称算法相比速度较慢(慢了 1000 倍以上)。一个 block 中可以使用 RSA 加密的数据量取决于 key 大小和填充可能使用的任何数据。

当您需要RSA 的两个 key ,同时需要加密超过一个RSA block 中的内容时,您通常会使用Hybrid encryption。 ,其中您使用随机对称 key 加密数据部分,然后使用 RSA 加密加密 key 。这样你就可以获得对称 key 的速度,以及 RSA 的两个 key 。但是,如果您真的不需要使用不同的 key 进行加密和解密,那么您根本不应该使用 RSA

由于 RSA 不是针对批量加密的,当您尝试加密的内容超过一个 block 中的内容时,标准实现无法处理它 - chaining of encryption blocks 没有实现任何逻辑。与 RSA。这将是开箱即用的对称密码的句柄。

关于java - RSA 逐 block 加密对大于 1kb 的文件产生空白输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44840831/

相关文章:

php - 如何在PHP中加密和解密图像文件?

c - 输出额外字符的简单字符加密

java - 在 Grails 中使用 Hibernate Annotated Java 类时的日期问题

linux - 重命名多个目录中的多个文件

file - 在 Adob​​e Dreamweaver 中使用 SASS

java - 系统找不到JAR中指定的路径

java - 将短字符串加密/保护为长字符串

java - 是否有更简洁的方法来检索未弃用的枚举值?

java - list 文件的用途是什么

java - 如何在 RecyclerView 中对选中的项目数据值求和