· 6 years ago · Jul 01, 2019, 01:54 AM
1SecretKey swkKeySpec = new SecretKeySpec(KEK, 0, swkKey.length, "AES");///KEK is a 256 bit AES key used for wrapping.
2 cipher = Cipher.getInstance("AESWrap", "SunJCE");
3 cipher.init(Cipher.WRAP_MODE, swkKeySpec);
4
5 byte[] key = keyAttributes.getPrivateKey() ///This returns private key in encoded format which is to be wrapped.
6 KeyFactory factory = KeyFactory.getInstance("RSA"); // throws NoSuchAlgorithmException
7 PrivateKey privateKey = factory.generatePrivate(new PKCS8EncodedKeySpec(privateKeyBytes));
8 log.debug("key.length: {}", key.length); ///this gives 634.
9 wrappedAppKey = cipher.wrap(privateKey); ///exception thrown at this point`