According to the JCDK User's Guide (for JC 2.2.2) the CREF (C-language RE) implementation supports the following alogirthms:
- 112-, 128-, 160-, 192-bit ECC
- 512-bit RSA
Therefore, the line
rsa_KeyPair = new KeyPair(KeyPair.ALG_RSA, KeyBuilder.LENGTH_RSA_2048);
should throw a CryptoException.NO_SUCH_ALGORITHM
when executed in CREF (as CREF does not support 2048-bit RSA).
Besides that error, that is likely the cause of the exception that you get, your code has at least one more issue:
byte[] ret = null;
rsa_PublicKey.getModulus(ret, (short) 0);
rsa_PublicKey.getExponent(ret, (short) (ret.length + 1));
Here you initialize the buffer ret
to null
but then you try to fill the modulus and exponent into the buffer. These two methods (getModulus()
and getExponent()
) don't create that buffer for you. Instead you need to create a buffer of appropriate size first:
byte[] buffer = new byte[expected_length]; // Note that you certainly do not want to allocate that buffer within your getPub method!
And then you can fill the modulus and exponent into that buffer:
byte modLen = rsa_PublicKey.getModulus(buffer, (short) 0);
byte expLen = rsa_PublicKey.getExponent(buffer, (short) modLen);