Package com.nimbusds.jose

Examples of com.nimbusds.jose.EncryptionMethod


  @Override
  public JWECryptoParts encrypt(final ReadOnlyJWEHeader header, final byte[] bytes)
    throws JOSEException {

    final JWEAlgorithm alg = header.getAlgorithm();
    final EncryptionMethod enc = header.getEncryptionMethod();

    // Generate and encrypt the CEK according to the enc method
    final SecureRandom randomGen = getSecureRandom();
    final SecretKey cek = AES.generateKey(enc.cekBitLength(), randomGen);

    Base64URL encryptedKey; // The second JWE part

    if (alg.equals(JWEAlgorithm.RSA1_5)) {

      encryptedKey = Base64URL.encode(RSA1_5.encryptCEK(publicKey, cek, keyEncryptionProvider));

    } else if (alg.equals(JWEAlgorithm.RSA_OAEP)) {

      encryptedKey = Base64URL.encode(RSA_OAEP.encryptCEK(publicKey, cek, keyEncryptionProvider));

    } else if (alg.equals(JWEAlgorithm.RSA_OAEP_256)) {
     
      encryptedKey = Base64URL.encode(RSA_OAEP_256.encryptCEK(publicKey, cek, keyEncryptionProvider));
     
    } else {

      throw new JOSEException("Unsupported JWE algorithm, must be RSA1_5, RSA-OAEP, or RSA-OAEP-256");
    }


    // Apply compression if instructed
    byte[] plainText = DeflateHelper.applyCompression(header, bytes);

    // Compose the AAD
    byte[] aad = StringUtils.toByteArray(header.toBase64URL().toString());

    // Encrypt the plain text according to the JWE enc
    byte[] iv;
    AuthenticatedCipherText authCipherText;
   
    if (enc.equals(EncryptionMethod.A128CBC_HS256) ||
        enc.equals(EncryptionMethod.A192CBC_HS384) ||
        enc.equals(EncryptionMethod.A256CBC_HS512)    ) {

      iv = AESCBC.generateIV(randomGen);

      authCipherText = AESCBC.encryptAuthenticated(
        cek, iv, plainText, aad,
        contentEncryptionProvider, macProvider);

    } else if (enc.equals(EncryptionMethod.A128GCM) ||
         enc.equals(EncryptionMethod.A192GCM) ||
         enc.equals(EncryptionMethod.A256GCM)    ) {

      iv = AESGCM.generateIV(randomGen);

      authCipherText = AESGCM.encrypt(
        cek, iv, plainText, aad,
        contentEncryptionProvider);

    } else if (enc.equals(EncryptionMethod.A128CBC_HS256_DEPRECATED) ||
         enc.equals(EncryptionMethod.A256CBC_HS512_DEPRECATED)    ) {

      iv = AESCBC.generateIV(randomGen);

      authCipherText = AESCBC.encryptWithConcatKDF(
        header, cek, encryptedKey, iv, plainText,
View Full Code Here


      throw new JOSEException("Unsupported JWE algorithm, must be \"dir\"");
    }

    // Check key length matches matches encryption method
    EncryptionMethod enc = readOnlyJWEHeader.getEncryptionMethod();

    if (enc.cekBitLength() != getKey().getEncoded().length * 8) {

      throw new JOSEException("The Content Encryption Key (CEK) length must be " + enc.cekBitLength() + " bits for " + enc + " encryption");
    }

    final Base64URL encryptedKey = null; // The second JWE part


    // Apply compression if instructed
    byte[] plainText = DeflateHelper.applyCompression(readOnlyJWEHeader, bytes);


    // Compose the AAD
    byte[] aad = StringUtils.toByteArray(readOnlyJWEHeader.toBase64URL().toString());
   

    // Encrypt the plain text according to the JWE enc
    byte[] iv;
    AuthenticatedCipherText authCipherText;
    SecureRandom randomGen = getSecureRandom();

    if (enc.equals(EncryptionMethod.A128CBC_HS256) || enc.equals(EncryptionMethod.A192CBC_HS384) || enc.equals(EncryptionMethod.A256CBC_HS512)) {

      iv = AESCBC.generateIV(randomGen);

      authCipherText = AESCBC.encryptAuthenticated(getKey(), iv, plainText, aad, contentEncryptionProvider, macProvider);

    } else if (enc.equals(EncryptionMethod.A128GCM) || enc.equals(EncryptionMethod.A192GCM) || enc.equals(EncryptionMethod.A256GCM)) {

      iv = AESGCM.generateIV(randomGen);

      authCipherText = AESGCM.encrypt(getKey(), iv, plainText, aad, contentEncryptionProvider);

View Full Code Here

TOP

Related Classes of com.nimbusds.jose.EncryptionMethod

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.