Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/113.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_Rsa_X509certificate_Jce - Fatal编程技术网

基于Java的非对称文件加密

基于Java的非对称文件加密,java,encryption,rsa,x509certificate,jce,Java,Encryption,Rsa,X509certificate,Jce,我有一个包含公钥和私钥的pfx文件,我想使用这些密钥在我的机器上本地加密和解密文件。 这是我的代码: public static void encryptFile(File file, PublicKey key, String transformation) throws NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException, IOException, Inva

我有一个包含公钥和私钥的pfx文件,我想使用这些密钥在我的机器上本地加密和解密文件。 这是我的代码:

public static void encryptFile(File file, PublicKey key,
        String transformation) throws NoSuchAlgorithmException,
        NoSuchPaddingException, InvalidKeyException, IOException,
        InvalidAlgorithmParameterException, NoSuchProviderException {
    Cipher c = Cipher.getInstance(transformation, "SunJCE");
    byte[] iv = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
    IvParameterSpec ivspec = new IvParameterSpec(iv);

    SecretKeySpec secretKeySpec = new SecretKeySpec(keyb, "AES");

    c.init(Cipher.ENCRYPT_MODE, secretKeySpec, ivspec);

    FileInputStream is = new FileInputStream(file);
    CipherOutputStream os = new CipherOutputStream(new FileOutputStream(
            new File(file.getName() + "_enc")), c);

    copy(is, os);
}

public static void decryptFile(File encryptedFile, File decryptedFile,
        Key privateKey, String transformation) {
    try {
        Cipher c = Cipher.getInstance(transformation, "SunJCE");

        byte[] iv = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
        IvParameterSpec ivspec = new IvParameterSpec(iv);
        byte[] keyb = privateKey.getEncoded();

        SecretKeySpec secretKeySpec = new SecretKeySpec(keyb, "AES");

        c.init(Cipher.DECRYPT_MODE, secretKeySpec, ivspec);
        CipherInputStream is = new CipherInputStream(new FileInputStream(
                encryptedFile), c);
        FileOutputStream os = new FileOutputStream(decryptedFile);

        copy(is, os);
    } catch (Exception e) {
        e.printStackTrace();
    }
}

public static void copy(InputStream is, OutputStream os) {

    try {
        byte[] buf = new byte[1024];
        long total = 0;
        while (true) {
            int r = is.read(buf);
            if (r == -1) {
                break;
            }
            os.write(buf, 0, r);
            total += r;
        }
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        try {
            is.close();
            os.flush();
            os.close();
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }
}
我这样称呼它:

            CertificateHandler.encryptFile(new File("test.pdf"), pb, "AES/CBC/PKCS5Padding");
        CertificateHandler.decryptFile(new File("test.pdf_enc"), new File("test.enc.pdf"), pk, "AES/CBC/NoPadding");
但我得到了这个错误:

java.security.InvalidKeyException: Invalid AES key length: 294 bytes
我使用了无限JCE策略,但没有任何更改。当我尝试使用消化键时,我认为它不起作用,因为它剪切了键,不再有效


有什么建议吗?

要正确加密,您缺少一些细节

AES是一种对称密码,密钥大小为128、192或256位。您不能将RSA私钥与任何加密方案一起使用

例如,要使用RSA密钥,只需搜索网络即可

通常情况下,较长内容(文件)的加密方式为:

(请注意有多种选择或模式,我在这里写的只是一个简单的建议)

加密:

  • 生成一个随机AES密钥(128位即可)和nonce(IV)-对于IV,不要像在代码中那样使用固定向量
  • 使用RSA加密生成的密钥(例如RSA/ECB/PKCS1P)
  • 内容(sha-256)的计算摘要(哈希)
  • 向输出流写入加密的AES密钥、IV、摘要和加密内容(AES/CBC/PKCS5P) 解密

  • 从流中读取AES密钥、nonce和摘要
  • 使用RSA私钥解密密钥
  • 阅读并解密内容
  • 计算解密内容的摘要,并将其和已读哈希进行比较,若不匹配,则失败
  • 这看起来很复杂,但是跳过这些步骤中的任何一个都可能(并且经常会)导致加密被破坏。即使这些步骤也需要一些属性(固定的执行时间等,但对于start,您应该可以)。

    您使用的“transform”参数是什么?虽然你似乎遗漏了一些重要的概念,但我正在写一个答案。@gusto2我使用“AES/CBC/pkcs5p添加”