// 
// Decompiled by Procyon v0.6.0
// 

package com.nimbusds.jose.crypto.impl;

import javax.crypto.spec.SecretKeySpec;
import java.security.PrivateKey;
import javax.crypto.Cipher;
import java.security.InvalidKeyException;
import com.nimbusds.jose.JOSEException;
import java.security.Key;
import java.security.SecureRandom;
import java.security.Provider;
import com.nimbusds.jose.crypto.opts.CipherMode;
import javax.crypto.SecretKey;
import java.security.interfaces.RSAPublicKey;
import com.nimbusds.jose.shaded.jcip.ThreadSafe;

@ThreadSafe
public class RSA_OAEP
{
    private static final String RSA_OEAP_JCA_ALG = "RSA/ECB/OAEPWithSHA-1AndMGF1Padding";
    
    public static byte[] encryptCEK(final RSAPublicKey pub, final SecretKey cek, final CipherMode mode, final Provider provider) throws JOSEException {
        assert mode == CipherMode.ENCRYPT_DECRYPT;
        try {
            final Cipher cipher = CipherHelper.getInstance("RSA/ECB/OAEPWithSHA-1AndMGF1Padding", provider);
            cipher.init(mode.getForJWEEncrypter(), pub, new SecureRandom());
            if (mode == CipherMode.WRAP_UNWRAP) {
                return cipher.wrap(cek);
            }
            return cipher.doFinal(cek.getEncoded());
        }
        catch (final InvalidKeyException e) {
            throw new JOSEException("RSA block size exception: The RSA key is too short, try a longer one", e);
        }
        catch (final Exception e2) {
            throw new JOSEException(e2.getMessage(), e2);
        }
    }
    
    public static SecretKey decryptCEK(final PrivateKey priv, final byte[] encryptedCEK, final CipherMode mode, final Provider provider) throws JOSEException {
        assert mode == CipherMode.ENCRYPT_DECRYPT;
        try {
            final Cipher cipher = CipherHelper.getInstance("RSA/ECB/OAEPWithSHA-1AndMGF1Padding", provider);
            cipher.init(mode.getForJWEDecrypter(), priv);
            if (mode == CipherMode.WRAP_UNWRAP) {
                return (SecretKey)cipher.unwrap(encryptedCEK, "AES", 3);
            }
            return new SecretKeySpec(cipher.doFinal(encryptedCEK), "AES");
        }
        catch (final Exception e) {
            throw new JOSEException(e.getMessage(), e);
        }
    }
    
    private RSA_OAEP() {
    }
}
