Code example for OAEPParameterSpec

Methods: getMGFParameters, getPSource

0
      
    private void initFromSpec( 
        OAEPParameterSpec pSpec)
        throws NoSuchPaddingException 
    { 
        MGF1ParameterSpec mgfParams = (MGF1ParameterSpec)pSpec.getMGFParameters();
        Digest digest = DigestFactory.getDigest(mgfParams.getDigestAlgorithm());
         
        if (digest == null)
        { 
            throw new NoSuchPaddingException("no match on OAEP constructor for digest algorithm: "+ mgfParams.getDigestAlgorithm());
        } 
 
        cipher = new OAEPEncoding(new RSABlindedEngine(), digest, ((PSource.PSpecified)pSpec.getPSource()).getValue());
        paramSpec = pSpec;
    } 
     
    protected int engineGetBlockSize()  
    { 
        try 
        { 
            return cipher.getInputBlockSize();
        } 
        catch (NullPointerException e)
        { 
            throw new IllegalStateException("RSA Cipher not initialised");
        } 
    } 
 
    protected int engineGetKeySize( 
        Key key)
    { 
        if (key instanceof RSAPrivateKey)
        { 
            RSAPrivateKey k = (RSAPrivateKey)key;
 
            return k.getModulus().bitLength();
        } 
        else if (key instanceof RSAPublicKey)
        { 
            RSAPublicKey k = (RSAPublicKey)key;
 
            return k.getModulus().bitLength();
        } 
 
        throw new IllegalArgumentException("not an RSA key!");
    } 
 
    protected int engineGetOutputSize( 
        int     inputLen) 
    { 
        try 
        { 
            return cipher.getOutputBlockSize();
        } 
        catch (NullPointerException e)
        { 
            throw new IllegalStateException("RSA Cipher not initialised");
        } 
    } 
 
    protected AlgorithmParameters engineGetParameters()
    { 
        if (engineParams == null)
        { 
            if (paramSpec != null)
            { 
                try 
                { 
                    engineParams = AlgorithmParameters.getInstance("OAEP", BouncyCastleProvider.PROVIDER_NAME);
                    engineParams.init(paramSpec);
                } 
                catch (Exception e)
                { 
                    throw new RuntimeException(e.toString());
                } 
            } 
        } 
 
        return engineParams;
    } 
 
    protected void engineSetMode( 
        String mode)
        throws NoSuchAlgorithmException 
    { 
        String md = Strings.toUpperCase(mode);
         
        if (md.equals("NONE") || md.equals("ECB"))
        { 
            return; 
        } 
         
        if (md.equals("1"))
        { 
            privateKeyOnly = true;
            publicKeyOnly = false;
            return; 
        } 
        else if (md.equals("2"))
        { 
            privateKeyOnly = false;
            publicKeyOnly = true;
            return; 
        } 
         
        throw new NoSuchAlgorithmException("can't support mode " + mode);
    } 
 
    protected void engineSetPadding( 
        String padding)
        throws NoSuchPaddingException 
    { 
        String pad = Strings.toUpperCase(padding);
 
        if (pad.equals("NOPADDING"))
        { 
            cipher = new RSABlindedEngine();
        } 
        else if (pad.equals("PKCS1PADDING"))
        { 
            cipher = new PKCS1Encoding(new RSABlindedEngine());
        } 
        // BEGIN android-removed 
        // else if (pad.equals("ISO9796-1PADDING")) 
        // { 
        //     cipher = new ISO9796d1Encoding(new RSABlindedEngine()); 
        // } 
        // END android-removed 
        else if (pad.equals("OAEPWITHMD5ANDMGF1PADDING"))
        { 
            initFromSpec(new OAEPParameterSpec("MD5", "MGF1", new MGF1ParameterSpec("MD5"), PSource.PSpecified.DEFAULT));
        } 
        else if (pad.equals("OAEPPADDING"))
        { 
            initFromSpec(OAEPParameterSpec.DEFAULT);
        }