Java RSA加密AES密钥

Java RSA加密AES密钥,java,encryption,cryptography,aes,rsa,Java,Encryption,Cryptography,Aes,Rsa,我有一个RSA私钥,我正试图解密另一个包含AES密钥的文件内容。到目前为止,我能从这些过程中得到的似乎只有行话。不确定我在下面的代码中做错了什么。我在谷歌上看到了至少100种不同的方式 import java.io.*; import java.io.IOException; import java.security.KeyFactory; import java.security.interfaces.RSAPrivateKey; import java.security.GeneralSe

我有一个RSA私钥,我正试图解密另一个包含AES密钥的文件内容。到目前为止,我能从这些过程中得到的似乎只有行话。不确定我在下面的代码中做错了什么。我在谷歌上看到了至少100种不同的方式

import java.io.*;
import java.io.IOException;

import java.security.KeyFactory;
import java.security.interfaces.RSAPrivateKey;
import java.security.GeneralSecurityException;
import java.security.spec.PKCS8EncodedKeySpec;

import javax.crypto.Cipher;
import javax.crypto.CipherInputStream;

import org.apache.commons.codec.binary.Base64;

import org.apache.commons.io.FileUtils;

public class RsaEncryption {
    private Cipher _pkCipher;

    public RsaEncryption() throws GeneralSecurityException {
        // create RSA public key cipher
        _pkCipher = Cipher.getInstance("RSA");
    }

    public String loadKey(File in, String privateKey) throws GeneralSecurityException, IOException, Exception {
        privateKey = privateKey.replaceAll("-+.*?-+", "");
        byte[] encodedKey = Base64.decodeBase64(privateKey);

        // create private key
        PKCS8EncodedKeySpec privateKeySpec = new PKCS8EncodedKeySpec(encodedKey);
        KeyFactory kf = KeyFactory.getInstance("RSA");
        RSAPrivateKey pk = (RSAPrivateKey) kf.generatePrivate(privateKeySpec);

        // read AES key
        _pkCipher.init(Cipher.DECRYPT_MODE, pk);
        byte[] encryptedBytes       = FileUtils.readFileToByteArray(in);
        ByteArrayInputStream fileIn = new ByteArrayInputStream(encryptedBytes);
        CipherInputStream cis       = new CipherInputStream(fileIn, _pkCipher);
        DataInputStream dis         = new DataInputStream(cis);
        byte[] decryptedData        = new byte[32];
        dis.read(decryptedData);
        String key = new String(decryptedData);
        return key;
    }
}
更新

带弹性城堡的新方法pem转换器仍不工作

import java.io.StringReader;
import java.io.File;
import java.io.IOException;

import java.security.KeyPair;
import java.security.interfaces.RSAPrivateKey;
import java.security.GeneralSecurityException;
import java.security.interfaces.RSAPublicKey;

import javax.crypto.Cipher;

import org.apache.commons.io.FileUtils;
import org.bouncycastle.openssl.PEMKeyPair;
import org.bouncycastle.openssl.PEMParser;
import org.bouncycastle.openssl.jcajce.JcaPEMKeyConverter;

public class RsaEncryption {
    private Cipher _pkCipher;
    private RSAPrivateKey _PrivateKey;
    private RSAPublicKey  _PublicKey;

    public RsaEncryption(String privateKey) throws GeneralSecurityException, IOException {
        loadKey(privateKey);
        // create RSA public key cipher
        _pkCipher = Cipher.getInstance("RSA/None/PKCS1Padding", "BC");
    }

    private void loadKey(String privateKey) throws IOException {
        PEMParser pemParser          = new PEMParser(new StringReader(privateKey));
        PEMKeyPair pemKeyPair        = (PEMKeyPair) pemParser.readObject();
        JcaPEMKeyConverter converter = new JcaPEMKeyConverter().setProvider("BC");
        KeyPair keyPair              = converter.getKeyPair(pemKeyPair);
        _PrivateKey                  = (RSAPrivateKey) keyPair.getPrivate();
        _PublicKey                   = (RSAPublicKey) keyPair.getPublic();
        pemParser.close();
    }

    public String decrypt(File in) throws GeneralSecurityException , IOException{
        _pkCipher.init(Cipher.DECRYPT_MODE, _PrivateKey);
        byte[] encryptedBytes = FileUtils.readFileToByteArray(in);
        String key = new String(_pkCipher.doFinal(encryptedBytes));
        System.out.println(key);
        return key;
    }


    public RSAPrivateKey getPrivateKey() { return _PrivateKey; }
    public RSAPublicKey getPublicKey()   { return _PublicKey;  }
}

RSA只能加密少量必须作为块处理的数据。你不需要一条小溪。就叫

byte[] aesKey = _pkCipher.doFinal(FileUtils.readFileToByteArray(in));

获取AES密钥。

JCE JAR在错误目录中工作正常,一旦放入正确目录。

什么是“行话”?如果没有任何例外,那么你就要拿回你的钥匙。如果我试着打印钥匙,它是空白的,但长度是32。然后,当我把它传递给AES类来尝试解密某些东西时,我得到的是我所说的术语,即胡说八道。请定义数据是如何加密的,以及密文是如何布局的。使用AES加密的实际数据密文在哪里?包含AES密钥的RSA密文在哪里?这两个文件是如何加密的?我们有一个密钥文件和一个IV文件。它们都是用RSA公钥加密的。RSA私钥存储在yaml文件中。将RSA私钥加载到字符串中。然后尝试用RSA私钥解密密钥或IV文件。AES部分并不重要,它获取AES所需的密钥这是RSA加密文件的问题。您忽略读入
dis.read(decryptedData)返回的字节数返回了多少字节?这就是我最初的路线。我得到了这个错误。javax.crypto.BadPaddingException:sun.security.rsa.RSAPadding.unpadding15(RSAPadding.java:380)~[na:1.8.0_11]在sun.security.rsa.RSAPadding.unpad(RSAPadding.java:291)~[na:1.8.0_11]在com.sun.crypto.provider.rsaciper.doFinal(rsaciper.java:354)~[sunjce_provider.jar:1.8.0]我忘了将加密密钥传递给解密方法。如果这不起作用,AES密钥是如何加密的?是真的使用了PKCS1Padding还是使用了OAEP?我在传入字节数组时遇到了错误。这就是我最初的想法。私钥最初是用ruby生成的,与其他任何东西一样。私钥:
OpenSSL::PKey::RSA.generate(2048).to_.each_line.collect{line}“{line}.join(“”)
请显示AES密钥是如何加密的,而不是RSA密钥是如何生成的-将其编辑到问题的底部。不,据我所知不需要。@原始海报或回答者不需要。该死,现在我不确定了。