Code example for Key

Methods: getAlgorithm, getEncoded

0
        // 
        // basic key check 
        // 
        if (!(key instanceof SecretKey))
        { 
            throw new InvalidKeyException("Key for algorithm " + key.getAlgorithm() + " not suitable for symmetric enryption.");
        } 
         
        // 
        // for RC5-64 we must have some default parameters 
        // 
        if (params == null && baseEngine.getAlgorithmName().startsWith("RC5-64"))
        { 
            throw new InvalidAlgorithmParameterException("RC5 requires an RC5ParametersSpec to be passed in.");
        } 
 
        // 
        // a note on iv's - if ivLength is zero the IV gets ignored (we don't use it). 
        // 
        if (key instanceof BCPBEKey)
        { 
            BCPBEKey k = (BCPBEKey)key;
             
            if (k.getOID() != null)
            { 
                pbeAlgorithm = k.getOID().getId();
            } 
            else 
            { 
                pbeAlgorithm = k.getAlgorithm();
            } 
             
            if (k.getParam() != null)
            { 
                param = k.getParam();
                pbeSpec = new PBEParameterSpec(k.getSalt(), k.getIterationCount());
            } 
            else if (params instanceof PBEParameterSpec)
            { 
                pbeSpec = (PBEParameterSpec)params;
                param = PBE.Util.makePBEParameters(k, params, cipher.getUnderlyingCipher().getAlgorithmName());
            } 
            else 
            { 
                throw new InvalidAlgorithmParameterException("PBE requires PBE parameters to be set.");
            } 
 
            if (param instanceof ParametersWithIV)
            { 
                ivParam = (ParametersWithIV)param;
            } 
        } 
        else if (params == null)
        { 
            param = new KeyParameter(key.getEncoded());
        } 
        else if (params instanceof IvParameterSpec)
        { 
            if (ivLength != 0)
            { 
                IvParameterSpec p = (IvParameterSpec)params;
 
                if (p.getIV().length != ivLength && !isAEADModeName(modeName))
                { 
                    throw new InvalidAlgorithmParameterException("IV must be " + ivLength + " bytes long.");
                } 
 
                if (key instanceof RepeatedSecretKeySpec)
                { 
                    param = new ParametersWithIV(null, p.getIV());
                    ivParam = (ParametersWithIV)param;
                } 
                else 
                { 
                    param = new ParametersWithIV(new KeyParameter(key.getEncoded()), p.getIV());
                    ivParam = (ParametersWithIV)param;
                } 
            } 
            else 
            { 
                if (modeName != null && modeName.equals("ECB"))
                { 
                    throw new InvalidAlgorithmParameterException("ECB mode does not use an IV");
                } 
                 
                param = new KeyParameter(key.getEncoded());
            } 
        } 
        else if (params instanceof GOST28147ParameterSpec)
        { 
            GOST28147ParameterSpec    gost28147Param = (GOST28147ParameterSpec)params;
 
            param = new ParametersWithSBox(
                       new KeyParameter(key.getEncoded()), ((GOST28147ParameterSpec)params).getSbox());
 
            if (gost28147Param.getIV() != null && ivLength != 0)
            { 
                param = new ParametersWithIV(param, gost28147Param.getIV());
                ivParam = (ParametersWithIV)param;
            } 
        } 
        else if (params instanceof RC2ParameterSpec)
        { 
            RC2ParameterSpec    rc2Param = (RC2ParameterSpec)params;
 
            param = new RC2Parameters(key.getEncoded(), ((RC2ParameterSpec)params).getEffectiveKeyBits());
 
            if (rc2Param.getIV() != null && ivLength != 0)
            { 
                param = new ParametersWithIV(param, rc2Param.getIV());
                ivParam = (ParametersWithIV)param;
            } 
        } 
        else if (params instanceof RC5ParameterSpec)
        { 
            RC5ParameterSpec    rc5Param = (RC5ParameterSpec)params;
 
            param = new RC5Parameters(key.getEncoded(), ((RC5ParameterSpec)params).getRounds());
            if (baseEngine.getAlgorithmName().startsWith("RC5"))
            { 
                if (baseEngine.getAlgorithmName().equals("RC5-32"))
                { 
                    if (rc5Param.getWordSize() != 32)