public GenericKey generateUnwrappedKey( AlgorithmIdentifier encryptedKeyAlgorithm, byte[] encryptedKey) throws OperatorException { try { Cipher keyCipher = helper.createSymmetricWrapper(this.getAlgorithmIdentifier().getAlgorithm()); keyCipher.init(Cipher.UNWRAP_MODE, secretKey); return new JceGenericKey( encryptedKeyAlgorithm, keyCipher.unwrap( encryptedKey, helper.getKeyAlgorithmName(encryptedKeyAlgorithm.getAlgorithm()), Cipher.SECRET_KEY)); } catch (InvalidKeyException e) { throw new OperatorException("key invalid in message.", e); } catch (NoSuchAlgorithmException e) { throw new OperatorException("can't find algorithm.", e); } }
public byte[] generateWrappedKey(GenericKey encryptionKey) throws OperatorException { Key contentEncryptionKeySpec = OperatorUtils.getJceKey(encryptionKey); Cipher keyEncryptionCipher = helper.createSymmetricWrapper(this.getAlgorithmIdentifier().getAlgorithm()); try { keyEncryptionCipher.init(Cipher.WRAP_MODE, wrappingKey, random); return keyEncryptionCipher.wrap(contentEncryptionKeySpec); } catch (InvalidKeyException e) { throw new OperatorException("cannot wrap key: " + e.getMessage(), e); } catch (GeneralSecurityException e) { throw new OperatorException("cannot wrap key: " + e.getMessage(), e); } }