使用RSA私钥解密AES密钥

时间:2018-10-02 13:22:29

标签: java encryption

我正在尝试使用RSA私钥解密AES密钥。

这里是例外:

  

[10/2/18 15:36:07:841 EEST] 00000082 SystemErr R java.lang.ArrayIndexOutOfBoundsException:RSA块的数据过多       [10/2/18 15:36:07:841 EEST] 00000082 SystemErr R,位于org.bouncycastle.jce.provider.JCERSACipher.engineDoFinal(未知来源)       [10/2/18 15:36:07:841 EEST] 00000082 SystemErr R,位于javax.crypto.Cipher.doFinal(未知源)       [10/2/18 15:36:07:841 EEST] 00000082 SystemErr R,位于com.cms.g2g.service.EncryptionService.decryptKey(EncryptionService.java:134)

这是我的代码:

import org.bouncycastle.util.encoders.Base64;

public static SecretKeySpec decryptKey (String decAESAsString, PrivateKey  privateKey) 
         throws NoSuchPaddingException, NoSuchAlgorithmException, InvalidKeyException, BadPaddingException, IllegalBlockSizeException {
    Cipher cipher = null;

    Security.addProvider(new BouncyCastleProvider());
    try {
        cipher = Cipher.getInstance("RSA/None/OAEPWithSHA1AndMGF1Padding", "BC");
    } catch (NoSuchProviderException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    cipher.init(Cipher.DECRYPT_MODE, privateKey);

    try {
        byte [] data = Base64.decode(decAESAsString);
        SecretKeySpec aeskeySpec = new SecretKeySpec(cipher.doFinal(data), "AES");
        return aeskeySpec ;
    } catch (Exception e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    return null;  
}

0 个答案:

没有答案
相关问题