public void init()

in src/main/java/org/apache/commons/crypto/jna/OpenSslJnaCipher.java [211:261]


    public void init(final int mode, final Key key, final AlgorithmParameterSpec params)
            throws InvalidKeyException, InvalidAlgorithmParameterException {
        Objects.requireNonNull(key, "key");
        Objects.requireNonNull(params, "params");
        final int cipherMode = mode == Cipher.ENCRYPT_MODE ? OpenSslNativeJna.OOSL_JNA_ENCRYPT_MODE : OpenSslNativeJna.OOSL_JNA_DECRYPT_MODE;
        if (!(params instanceof IvParameterSpec)) {
            // other AlgorithmParameterSpec such as GCMParameterSpec is not
            // supported now.
            throw new InvalidAlgorithmParameterException("Illegal parameters");
        }
        final byte[] iv = ((IvParameterSpec) params).getIV();

        if ((algorithmMode == AlgorithmMode.AES_CBC || algorithmMode == AlgorithmMode.AES_CTR) && iv.length != IV_LENGTH) {
            throw new InvalidAlgorithmParameterException("Wrong IV length: must be 16 bytes long");
        }
        final int keyEncodedLength = key.getEncoded().length;

        if (algorithmMode == AlgorithmMode.AES_CBC) {
            switch (keyEncodedLength) {
            case AES_128_ENCODED_KEYLEN:
                algo = OpenSslNativeJna.EVP_aes_128_cbc();
                break;
            case AES_192_ENCODED_KEYLEN:
                algo = OpenSslNativeJna.EVP_aes_192_cbc();
                break;
            case AES_256_ENCODED_KEYLEN:
                algo = OpenSslNativeJna.EVP_aes_256_cbc();
                break;
            default:
                throw new InvalidKeyException("keysize unsupported (" + keyEncodedLength + ")");
            }

        } else {
            switch (keyEncodedLength) {
            case AES_128_ENCODED_KEYLEN:
                algo = OpenSslNativeJna.EVP_aes_128_ctr();
                break;
            case AES_192_ENCODED_KEYLEN:
                algo = OpenSslNativeJna.EVP_aes_192_ctr();
                break;
            case AES_256_ENCODED_KEYLEN:
                algo = OpenSslNativeJna.EVP_aes_256_ctr();
                break;
            default:
                throw new InvalidKeyException("keysize unsupported (" + keyEncodedLength + ")");
            }
        }

        throwOnError(OpenSslNativeJna.EVP_CipherInit_ex(context, algo, null, key.getEncoded(), iv, cipherMode));
        throwOnError(OpenSslNativeJna.EVP_CIPHER_CTX_set_padding(context, padding));
    }