I'm trying to convert a BigInteger privateKey to KEY, I found this piece of code and made some modifications like this:
KeyFactory factory = KeyFactory.getInstance("RSA");
RSAPrivateKeySpec spec;
spec = new RSAPrivateKeySpec(modulus,privateKey);
RSAPrivateKey priPEM = (RSAPrivateKey) factory.generatePrivate(spec);
Cipher enc = Cipher.getInstance("RSA");
enc.init(Cipher.WRAP_MODE, priPEM);
byte[] encryptedKey = enc.wrap(priPEM);
However, the result is: Key is too long for wrapping
Exception in thread "main" java.security.InvalidKeyException: Key is too long for wrapping
I am using keysize (2048), I've tried 1024 and 512 but still the same output error.
Any idea on how can I get rid of this error?