Java RSA 加密的 AES 密钥

Java RSA encrypted AES key

我有一个 RSA 私钥,我正在尝试解密另一个包含 AES 密钥的文件内容。到目前为止,我似乎能从流程中得到 return 的只是行话。不太确定我在下面的代码中做错了什么。我看过 google 并看到至少有 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 在被放入正确目录后工作正常。