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 com.nimbusds.jose.crypto.impl.*;
import net.jcip.annotations.ThreadSafe;
import com.nimbusds.jose.*;
import com.nimbusds.jose.jwk.OctetSequenceKey;
import com.nimbusds.jose.util.Base64URL;
/**
* 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)
*
- {@link com.nimbusds.jose.EncryptionMethod#XC20P} (requires 256 bit key)
*
*
* @author Vladimir Dzhuvinov
* @author Egor Puzanov
* @version 2023-09-10
*/
@ThreadSafe
public class DirectEncrypter extends DirectCryptoProvider implements JWEEncrypter {
/**
* Creates a new direct encrypter.
*
* @param key The symmetric key. Its algorithm should 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"));
}
/**
* Encrypts the specified clear text of a {@link JWEObject JWE object}.
*
* @param header The JSON Web Encryption (JWE) header. Must specify
* a supported JWE algorithm and method. Must not be
* {@code null}.
* @param clearText The clear text to encrypt. Must not be {@code null}.
*
* @return The resulting JWE crypto parts.
*
* @throws JOSEException If the JWE algorithm or method is not
* supported or if encryption failed for some
* other internal reason.
*/
@Deprecated
public JWECryptoParts encrypt(final JWEHeader header, final byte[] clearText)
throws JOSEException {
return encrypt(header, clearText, AAD.compute(header));
}
@Override
public JWECryptoParts encrypt(final JWEHeader header, final byte[] clearText, final byte[] aad)
throws JOSEException {
final JWEAlgorithm alg = JWEHeaderValidation.getAlgorithmAndEnsureNotNull(header);
if (! alg.equals(JWEAlgorithm.DIR)) {
throw new JOSEException(AlgorithmSupportMessage.unsupportedJWEAlgorithm(alg, SUPPORTED_ALGORITHMS));
}
final Base64URL encryptedKey = null; // The second JWE part
return ContentCryptoProvider.encrypt(header, clearText, aad, getKey(), encryptedKey, getJCAContext());
}
}
© 2015 - 2024 Weber Informatics LLC | Privacy Policy