Search in sources :

Example 21 with KeyParameter

use of org.bouncycastle.crypto.params.KeyParameter in project gocd by gocd.

the class GoCipher method cipher.

public String cipher(byte[] key, String plainText) throws InvalidCipherTextException {
    PaddedBufferedBlockCipher cipher = new PaddedBufferedBlockCipher(new CBCBlockCipher(new DESEngine()));
    KeyParameter keyParameter = new KeyParameter(Hex.decode(key));
    cipher.init(true, keyParameter);
    byte[] plainTextBytes = plainText.getBytes();
    byte[] cipherTextBytes = new byte[cipher.getOutputSize(plainTextBytes.length)];
    int outputLength = cipher.processBytes(plainTextBytes, 0, plainTextBytes.length, cipherTextBytes, 0);
    cipher.doFinal(cipherTextBytes, outputLength);
    return java.util.Base64.getEncoder().encodeToString(cipherTextBytes).trim();
}
Also used : PaddedBufferedBlockCipher(org.bouncycastle.crypto.paddings.PaddedBufferedBlockCipher) DESEngine(org.bouncycastle.crypto.engines.DESEngine) KeyParameter(org.bouncycastle.crypto.params.KeyParameter) CBCBlockCipher(org.bouncycastle.crypto.modes.CBCBlockCipher)

Example 22 with KeyParameter

use of org.bouncycastle.crypto.params.KeyParameter in project google-authenticator by google.

the class CheckCodeScreen method getCheckCode.

static String getCheckCode(String secret) throws Base32String.DecodingException {
    final byte[] keyBytes = Base32String.decode(secret);
    Mac mac = new HMac(new SHA1Digest());
    mac.init(new KeyParameter(keyBytes));
    PasscodeGenerator pcg = new PasscodeGenerator(mac);
    return pcg.generateResponseCode(0L);
}
Also used : HMac(org.bouncycastle.crypto.macs.HMac) SHA1Digest(org.bouncycastle.crypto.digests.SHA1Digest) KeyParameter(org.bouncycastle.crypto.params.KeyParameter) HMac(org.bouncycastle.crypto.macs.HMac) Mac(org.bouncycastle.crypto.Mac)

Example 23 with KeyParameter

use of org.bouncycastle.crypto.params.KeyParameter in project robovm by robovm.

the class JCEStreamCipher method engineInit.

protected void engineInit(int opmode, Key key, AlgorithmParameterSpec params, SecureRandom random) throws InvalidKeyException, InvalidAlgorithmParameterException {
    CipherParameters param;
    this.pbeSpec = null;
    this.pbeAlgorithm = null;
    this.engineParams = null;
    //
    if (!(key instanceof SecretKey)) {
        throw new InvalidKeyException("Key for algorithm " + key.getAlgorithm() + " not suitable for symmetric enryption.");
    }
    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) {
            param = PBE.Util.makePBEParameters(k, params, cipher.getAlgorithmName());
            pbeSpec = (PBEParameterSpec) params;
        } else {
            throw new InvalidAlgorithmParameterException("PBE requires PBE parameters to be set.");
        }
        if (k.getIvSize() != 0) {
            ivParam = (ParametersWithIV) param;
        }
    } else if (params == null) {
        param = new KeyParameter(key.getEncoded());
    } else if (params instanceof IvParameterSpec) {
        param = new ParametersWithIV(new KeyParameter(key.getEncoded()), ((IvParameterSpec) params).getIV());
        ivParam = (ParametersWithIV) param;
    } else {
        throw new IllegalArgumentException("unknown parameter type.");
    }
    if ((ivLength != 0) && !(param instanceof ParametersWithIV)) {
        SecureRandom ivRandom = random;
        if (ivRandom == null) {
            ivRandom = new SecureRandom();
        }
        if ((opmode == Cipher.ENCRYPT_MODE) || (opmode == Cipher.WRAP_MODE)) {
            byte[] iv = new byte[ivLength];
            ivRandom.nextBytes(iv);
            param = new ParametersWithIV(param, iv);
            ivParam = (ParametersWithIV) param;
        } else {
            throw new InvalidAlgorithmParameterException("no IV set when one expected");
        }
    }
    switch(opmode) {
        case Cipher.ENCRYPT_MODE:
        case Cipher.WRAP_MODE:
            cipher.init(true, param);
            break;
        case Cipher.DECRYPT_MODE:
        case Cipher.UNWRAP_MODE:
            cipher.init(false, param);
            break;
        default:
            System.out.println("eeek!");
    }
}
Also used : CipherParameters(org.bouncycastle.crypto.CipherParameters) ParametersWithIV(org.bouncycastle.crypto.params.ParametersWithIV) SecretKey(javax.crypto.SecretKey) InvalidAlgorithmParameterException(java.security.InvalidAlgorithmParameterException) KeyParameter(org.bouncycastle.crypto.params.KeyParameter) BCPBEKey(org.bouncycastle.jcajce.provider.symmetric.util.BCPBEKey) SecureRandom(java.security.SecureRandom) IvParameterSpec(javax.crypto.spec.IvParameterSpec) InvalidKeyException(java.security.InvalidKeyException) PBEParameterSpec(javax.crypto.spec.PBEParameterSpec)

Example 24 with KeyParameter

use of org.bouncycastle.crypto.params.KeyParameter in project robovm by robovm.

the class BaseBlockCipher method engineInit.

protected void engineInit(int opmode, Key key, AlgorithmParameterSpec params, SecureRandom random) throws InvalidKeyException, InvalidAlgorithmParameterException {
    CipherParameters param;
    this.pbeSpec = null;
    this.pbeAlgorithm = null;
    this.engineParams = null;
    //
    if (!(key instanceof SecretKey)) {
        throw new InvalidKeyException("Key for algorithm " + key.getAlgorithm() + " not suitable for symmetric enryption.");
    }
    //
    if (params == null && baseEngine.getAlgorithmName().startsWith("RC5-64")) {
        throw new InvalidAlgorithmParameterException("RC5 requires an RC5ParametersSpec to be passed in.");
    }
    //
    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();
            if (params instanceof IvParameterSpec) {
                IvParameterSpec iv = (IvParameterSpec) params;
                param = new ParametersWithIV(param, iv.getIV());
            }
        } 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 // BEGIN android-removed
    // 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)
    //             {
    //                 throw new InvalidAlgorithmParameterException("RC5 already set up for a word size of 32 not " + rc5Param.getWordSize() + ".");
    //             }
    //         }
    //         else if (baseEngine.getAlgorithmName().equals("RC5-64"))
    //         {
    //             if (rc5Param.getWordSize() != 64)
    //             {
    //                 throw new InvalidAlgorithmParameterException("RC5 already set up for a word size of 64 not " + rc5Param.getWordSize() + ".");
    //             }
    //         }
    //     }
    //     else
    //     {
    //         throw new InvalidAlgorithmParameterException("RC5 parameters passed to a cipher that is not RC5.");
    //     }
    //     if ((rc5Param.getIV() != null) && (ivLength != 0))
    //     {
    //         param = new ParametersWithIV(param, rc5Param.getIV());
    //         ivParam = (ParametersWithIV)param;
    //     }
    // }
    // END android-removed
    {
        throw new InvalidAlgorithmParameterException("unknown parameter type.");
    }
    if ((ivLength != 0) && !(param instanceof ParametersWithIV)) {
        SecureRandom ivRandom = random;
        if (ivRandom == null) {
            ivRandom = new SecureRandom();
        }
        if ((opmode == Cipher.ENCRYPT_MODE) || (opmode == Cipher.WRAP_MODE)) {
            byte[] iv = new byte[ivLength];
            ivRandom.nextBytes(iv);
            param = new ParametersWithIV(param, iv);
            ivParam = (ParametersWithIV) param;
        } else if (cipher.getUnderlyingCipher().getAlgorithmName().indexOf("PGPCFB") < 0) {
            throw new InvalidAlgorithmParameterException("no IV set when one expected");
        }
    }
    if (random != null && padded) {
        param = new ParametersWithRandom(param, random);
    }
    try {
        switch(opmode) {
            case Cipher.ENCRYPT_MODE:
            case Cipher.WRAP_MODE:
                cipher.init(true, param);
                break;
            case Cipher.DECRYPT_MODE:
            case Cipher.UNWRAP_MODE:
                cipher.init(false, param);
                break;
            default:
                throw new InvalidParameterException("unknown opmode " + opmode + " passed");
        }
    } catch (Exception e) {
        throw new InvalidKeyException(e.getMessage());
    }
}
Also used : InvalidAlgorithmParameterException(java.security.InvalidAlgorithmParameterException) RepeatedSecretKeySpec(org.bouncycastle.jce.spec.RepeatedSecretKeySpec) KeyParameter(org.bouncycastle.crypto.params.KeyParameter) SecureRandom(java.security.SecureRandom) ParametersWithRandom(org.bouncycastle.crypto.params.ParametersWithRandom) InvalidKeyException(java.security.InvalidKeyException) InvalidAlgorithmParameterException(java.security.InvalidAlgorithmParameterException) InvalidCipherTextException(org.bouncycastle.crypto.InvalidCipherTextException) DataLengthException(org.bouncycastle.crypto.DataLengthException) InvalidParameterException(java.security.InvalidParameterException) NoSuchPaddingException(javax.crypto.NoSuchPaddingException) ShortBufferException(javax.crypto.ShortBufferException) OutputLengthException(org.bouncycastle.crypto.OutputLengthException) IllegalBlockSizeException(javax.crypto.IllegalBlockSizeException) BadPaddingException(javax.crypto.BadPaddingException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) InvalidKeyException(java.security.InvalidKeyException) CipherParameters(org.bouncycastle.crypto.CipherParameters) ParametersWithIV(org.bouncycastle.crypto.params.ParametersWithIV) InvalidParameterException(java.security.InvalidParameterException) SecretKey(javax.crypto.SecretKey) IvParameterSpec(javax.crypto.spec.IvParameterSpec) PBEParameterSpec(javax.crypto.spec.PBEParameterSpec)

Example 25 with KeyParameter

use of org.bouncycastle.crypto.params.KeyParameter in project robovm by robovm.

the class BaseStreamCipher method engineInit.

protected void engineInit(int opmode, Key key, AlgorithmParameterSpec params, SecureRandom random) throws InvalidKeyException, InvalidAlgorithmParameterException {
    CipherParameters param;
    this.pbeSpec = null;
    this.pbeAlgorithm = null;
    this.engineParams = null;
    //
    if (!(key instanceof SecretKey)) {
        throw new InvalidKeyException("Key for algorithm " + key.getAlgorithm() + " not suitable for symmetric enryption.");
    }
    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) {
            param = PBE.Util.makePBEParameters(k, params, cipher.getAlgorithmName());
            pbeSpec = (PBEParameterSpec) params;
        } else {
            throw new InvalidAlgorithmParameterException("PBE requires PBE parameters to be set.");
        }
        if (k.getIvSize() != 0) {
            ivParam = (ParametersWithIV) param;
        }
    } else if (params == null) {
        param = new KeyParameter(key.getEncoded());
    } else if (params instanceof IvParameterSpec) {
        param = new ParametersWithIV(new KeyParameter(key.getEncoded()), ((IvParameterSpec) params).getIV());
        ivParam = (ParametersWithIV) param;
    } else {
        throw new IllegalArgumentException("unknown parameter type.");
    }
    if ((ivLength != 0) && !(param instanceof ParametersWithIV)) {
        SecureRandom ivRandom = random;
        if (ivRandom == null) {
            ivRandom = new SecureRandom();
        }
        if ((opmode == Cipher.ENCRYPT_MODE) || (opmode == Cipher.WRAP_MODE)) {
            byte[] iv = new byte[ivLength];
            ivRandom.nextBytes(iv);
            param = new ParametersWithIV(param, iv);
            ivParam = (ParametersWithIV) param;
        } else {
            throw new InvalidAlgorithmParameterException("no IV set when one expected");
        }
    }
    switch(opmode) {
        case Cipher.ENCRYPT_MODE:
        case Cipher.WRAP_MODE:
            cipher.init(true, param);
            break;
        case Cipher.DECRYPT_MODE:
        case Cipher.UNWRAP_MODE:
            cipher.init(false, param);
            break;
        default:
            System.out.println("eeek!");
    }
}
Also used : CipherParameters(org.bouncycastle.crypto.CipherParameters) ParametersWithIV(org.bouncycastle.crypto.params.ParametersWithIV) SecretKey(javax.crypto.SecretKey) InvalidAlgorithmParameterException(java.security.InvalidAlgorithmParameterException) KeyParameter(org.bouncycastle.crypto.params.KeyParameter) SecureRandom(java.security.SecureRandom) IvParameterSpec(javax.crypto.spec.IvParameterSpec) InvalidKeyException(java.security.InvalidKeyException) PBEParameterSpec(javax.crypto.spec.PBEParameterSpec)

Aggregations

KeyParameter (org.bouncycastle.crypto.params.KeyParameter)47 ParametersWithIV (org.bouncycastle.crypto.params.ParametersWithIV)21 CipherParameters (org.bouncycastle.crypto.CipherParameters)15 IvParameterSpec (javax.crypto.spec.IvParameterSpec)13 GCMBlockCipher (org.bouncycastle.crypto.modes.GCMBlockCipher)12 AEADParameters (org.bouncycastle.crypto.params.AEADParameters)12 InvalidAlgorithmParameterException (java.security.InvalidAlgorithmParameterException)11 InvalidKeyException (java.security.InvalidKeyException)9 PBEParameterSpec (javax.crypto.spec.PBEParameterSpec)9 AESEngine (org.bouncycastle.crypto.engines.AESEngine)8 AEADBlockCipher (org.bouncycastle.crypto.modes.AEADBlockCipher)8 SecureRandom (java.security.SecureRandom)7 SecretKeySpec (javax.crypto.spec.SecretKeySpec)7 SecretKey (javax.crypto.SecretKey)5 InvalidCipherTextException (org.bouncycastle.crypto.InvalidCipherTextException)5 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)4 NoSuchPaddingException (javax.crypto.NoSuchPaddingException)4 BlockCipher (org.bouncycastle.crypto.BlockCipher)4 CBCBlockCipher (org.bouncycastle.crypto.modes.CBCBlockCipher)4 ParametersWithRandom (org.bouncycastle.crypto.params.ParametersWithRandom)4