What's wrong with IBM's JCE provider?

前端 未结 6 1536
慢半拍i
慢半拍i 2020-12-28 23:29

I have a JCE test that works fine with all Sun JDKs I have tried, but fails with various IBM J9 JDKs (e.g. 1.6.0 build pwi3260sr8-20100409_01(SR8)). The exception below happ

6条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-12-29 00:24

    IBM insists private keys cannot be used for encryption and public keys cannot be used for decryption, so they either see this artificial restriction as a feature, or someone is seriously confused here.

    Here is how I worked around this problem:

    RSAPrivateCrtKey privateKey = (RSAPrivateCrtKey) ks.getKey(keyAlias, ksPassword.trim().toCharArray());
    RSAPublicKeySpec spec = new RSAPublicKeySpec(privateKey.getModulus(),privateKey.getPrivateExponent());
    Key fakePublicKey = KeyFactory.getInstance("RSA").generatePublic(spec);
    encryptCipher.init(Cipher.ENCRYPT_MODE, fakePublicKey);
    

    Essentially, I created a public key object with private key's crypto material. You will need to do the reverse, create a private key object with public key's crypto material, to decrypt with public key if you want to avoid the "Public key cannot be used to decrypt" exception.

提交回复
热议问题