com.nimbusds.jose.crypto.DirectEncrypter Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of nimbus-jose-jwt Show documentation
Show all versions of nimbus-jose-jwt Show documentation
Java library for Javascript Object Signing and Encryption (JOSE) and
JSON Web Tokens (JWT)
/*
* nimbus-jose-jwt
*
* Copyright 2012-2016, Connect2id Ltd.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use
* this file except in compliance with the License. You may obtain a copy of the
* License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed
* under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
* CONDITIONS OF ANY KIND, either express or implied. See the License for the
* specific language governing permissions and limitations under the License.
*/
package com.nimbusds.jose.crypto;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;
import net.jcip.annotations.ThreadSafe;
import com.nimbusds.jose.*;
import com.nimbusds.jose.jwk.OctetSequenceKey;
import com.nimbusds.jose.util.Base64URL;
import com.nimbusds.jose.util.ByteUtils;
/**
* Direct encrypter of {@link com.nimbusds.jose.JWEObject JWE objects} with a
* shared symmetric key.
*
* See RFC 7518
* section 4.5
* for more information.
*
* This class is thread-safe.
*
*
Supports the following key management algorithms:
*
*
* - {@link com.nimbusds.jose.JWEAlgorithm#DIR}
*
*
* Supports the following content encryption algorithms:
*
*
* - {@link com.nimbusds.jose.EncryptionMethod#A128CBC_HS256} (requires 256 bit key)
*
- {@link com.nimbusds.jose.EncryptionMethod#A192CBC_HS384} (requires 384 bit key)
*
- {@link com.nimbusds.jose.EncryptionMethod#A256CBC_HS512} (requires 512 bit key)
*
- {@link com.nimbusds.jose.EncryptionMethod#A128GCM} (requires 128 bit key)
*
- {@link com.nimbusds.jose.EncryptionMethod#A192GCM} (requires 192 bit key)
*
- {@link com.nimbusds.jose.EncryptionMethod#A256GCM} (requires 256 bit key)
*
- {@link com.nimbusds.jose.EncryptionMethod#A128CBC_HS256_DEPRECATED} (requires 256 bit key)
*
- {@link com.nimbusds.jose.EncryptionMethod#A256CBC_HS512_DEPRECATED} (requires 512 bit key)
*
*
* @author Vladimir Dzhuvinov
* @version 2017-06-01
*/
@ThreadSafe
public class DirectEncrypter extends DirectCryptoProvider implements JWEEncrypter {
/**
* Creates a new direct encrypter.
*
* @param key The symmetric key. Its algorithm must be "AES". Must be
* 128 bits (16 bytes), 192 bits (24 bytes), 256 bits (32
* bytes), 384 bits (48 bytes) or 512 bits (64 bytes) long.
* Must not be {@code null}.
*
* @throws KeyLengthException If the symmetric key length is not
* compatible.
*/
public DirectEncrypter(final SecretKey key)
throws KeyLengthException {
super(key);
}
/**
* Creates a new direct encrypter.
*
* @param keyBytes The symmetric key, as a byte array. Must be 128 bits
* (16 bytes), 192 bits (24 bytes), 256 bits (32
* bytes), 384 bits (48 bytes) or 512 bits (64 bytes)
* long. Must not be {@code null}.
*
* @throws KeyLengthException If the symmetric key length is not
* compatible.
*/
public DirectEncrypter(final byte[] keyBytes)
throws KeyLengthException {
this(new SecretKeySpec(keyBytes, "AES"));
}
/**
* Creates a new direct encrypter.
*
* @param octJWK The symmetric key, as a JWK. Must be 128 bits (16
* bytes), 192 bits (24 bytes), 256 bits (32 bytes), 384
* bits (48 bytes) or 512 bits (64 bytes) long. Must not
* be {@code null}.
*
* @throws KeyLengthException If the symmetric key length is not
* compatible.
*/
public DirectEncrypter(final OctetSequenceKey octJWK)
throws KeyLengthException {
this(octJWK.toSecretKey("AES"));
}
@Override
public JWECryptoParts encrypt(final JWEHeader header, final byte[] clearText)
throws JOSEException {
JWEAlgorithm alg = header.getAlgorithm();
if (! alg.equals(JWEAlgorithm.DIR)) {
throw new JOSEException(AlgorithmSupportMessage.unsupportedJWEAlgorithm(alg, SUPPORTED_ALGORITHMS));
}
// Check key length matches encryption method
EncryptionMethod enc = header.getEncryptionMethod();
if (enc.cekBitLength() != ByteUtils.safeBitLength(getKey().getEncoded())) {
throw new KeyLengthException(enc.cekBitLength(), enc);
}
final Base64URL encryptedKey = null; // The second JWE part
return ContentCryptoProvider.encrypt(header, clearText, getKey(), encryptedKey, getJCAContext());
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy