Search in sources :

Example 6 with KeyParameter

use of org.bouncycastle.crypto.params.KeyParameter in project Conversations by siacs.

the class AbstractConnectionManager method createOutputStream.

private static OutputStream createOutputStream(DownloadableFile file, boolean gcm, boolean append) {
    FileOutputStream os;
    try {
        os = new FileOutputStream(file, append);
        if (file.getKey() == null) {
            return os;
        }
    } catch (FileNotFoundException e) {
        return null;
    }
    try {
        if (gcm) {
            AEADBlockCipher cipher = new GCMBlockCipher(new AESEngine());
            cipher.init(false, new AEADParameters(new KeyParameter(file.getKey()), 128, file.getIv()));
            return new org.bouncycastle.crypto.io.CipherOutputStream(os, cipher);
        } else {
            IvParameterSpec ips = new IvParameterSpec(file.getIv());
            Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
            cipher.init(Cipher.DECRYPT_MODE, new SecretKeySpec(file.getKey(), "AES"), ips);
            Log.d(Config.LOGTAG, "opening encrypted output stream");
            return new CipherOutputStream(os, cipher);
        }
    } catch (InvalidKeyException e) {
        return null;
    } catch (NoSuchAlgorithmException e) {
        return null;
    } catch (NoSuchPaddingException e) {
        return null;
    } catch (InvalidAlgorithmParameterException e) {
        return null;
    }
}
Also used : AESEngine(org.bouncycastle.crypto.engines.AESEngine) CipherOutputStream(javax.crypto.CipherOutputStream) InvalidAlgorithmParameterException(java.security.InvalidAlgorithmParameterException) KeyParameter(org.bouncycastle.crypto.params.KeyParameter) FileNotFoundException(java.io.FileNotFoundException) NoSuchPaddingException(javax.crypto.NoSuchPaddingException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) InvalidKeyException(java.security.InvalidKeyException) AEADParameters(org.bouncycastle.crypto.params.AEADParameters) SecretKeySpec(javax.crypto.spec.SecretKeySpec) FileOutputStream(java.io.FileOutputStream) IvParameterSpec(javax.crypto.spec.IvParameterSpec) Cipher(javax.crypto.Cipher) AEADBlockCipher(org.bouncycastle.crypto.modes.AEADBlockCipher) GCMBlockCipher(org.bouncycastle.crypto.modes.GCMBlockCipher) GCMBlockCipher(org.bouncycastle.crypto.modes.GCMBlockCipher) AEADBlockCipher(org.bouncycastle.crypto.modes.AEADBlockCipher)

Example 7 with KeyParameter

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

the class AesGcmCipherSpec method newCipherInputStream.

@Override
public InputStream newCipherInputStream(InputStream underlyingInputStream, byte[] secretKey, byte[] iv) throws CipherException {
    AEADBlockCipher cipher = new GCMBlockCipher(new AESEngine());
    cipher.init(false, new AEADParameters(new KeyParameter(secretKey), MAC_SIZE, iv));
    return new org.bouncycastle.crypto.io.CipherInputStream(underlyingInputStream, cipher);
}
Also used : AESEngine(org.bouncycastle.crypto.engines.AESEngine) AEADParameters(org.bouncycastle.crypto.params.AEADParameters) KeyParameter(org.bouncycastle.crypto.params.KeyParameter) GCMBlockCipher(org.bouncycastle.crypto.modes.GCMBlockCipher) AEADBlockCipher(org.bouncycastle.crypto.modes.AEADBlockCipher)

Example 8 with KeyParameter

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

the class TwofishGcmCipherSpec method newCipherInputStream.

@Override
public InputStream newCipherInputStream(InputStream underlyingInputStream, byte[] secretKey, byte[] iv) throws CipherException {
    AEADBlockCipher cipher = new GCMBlockCipher(new TwofishEngine());
    cipher.init(false, new AEADParameters(new KeyParameter(secretKey), MAC_SIZE, iv));
    return new org.bouncycastle.crypto.io.CipherInputStream(underlyingInputStream, cipher);
}
Also used : AEADParameters(org.bouncycastle.crypto.params.AEADParameters) KeyParameter(org.bouncycastle.crypto.params.KeyParameter) TwofishEngine(org.bouncycastle.crypto.engines.TwofishEngine) GCMBlockCipher(org.bouncycastle.crypto.modes.GCMBlockCipher) AEADBlockCipher(org.bouncycastle.crypto.modes.AEADBlockCipher)

Example 9 with KeyParameter

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

the class GoCipher method decipher.

public String decipher(byte[] key, String cipherText) throws InvalidCipherTextException {
    PaddedBufferedBlockCipher cipher = new PaddedBufferedBlockCipher(new CBCBlockCipher(new DESEngine()));
    cipher.init(false, new KeyParameter(Hex.decode(key)));
    byte[] cipherTextBytes = java.util.Base64.getDecoder().decode(cipherText);
    byte[] plainTextBytes = new byte[cipher.getOutputSize(cipherTextBytes.length)];
    int outputLength = cipher.processBytes(cipherTextBytes, 0, cipherTextBytes.length, plainTextBytes, 0);
    cipher.doFinal(plainTextBytes, outputLength);
    int paddingStarts = plainTextBytes.length - 1;
    for (; paddingStarts >= 0; paddingStarts--) {
        if (plainTextBytes[paddingStarts] != 0) {
            break;
        }
    }
    return new String(plainTextBytes, 0, paddingStarts + 1);
}
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 10 with KeyParameter

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

the class AuthenticatorScreen method computePin.

/**
   * Computes the one-time PIN given the secret key.
   * 
   * @param secret
   *          the secret key
   * @return the PIN
   * @throws GeneralSecurityException
   * @throws DecodingException
   *           If the key string is improperly encoded.
   */
public static String computePin(String secret, Long counter) {
    try {
        final byte[] keyBytes = Base32String.decode(secret);
        Mac mac = new HMac(new SHA1Digest());
        mac.init(new KeyParameter(keyBytes));
        PasscodeGenerator pcg = new PasscodeGenerator(mac);
        if (counter == null) {
            // time-based totp
            return pcg.generateTimeoutCode();
        } else {
            // counter-based hotp
            return pcg.generateResponseCode(counter.longValue());
        }
    } catch (RuntimeException e) {
        return "General security exception";
    } catch (DecodingException e) {
        return "Decoding exception";
    }
}
Also used : HMac(org.bouncycastle.crypto.macs.HMac) SHA1Digest(org.bouncycastle.crypto.digests.SHA1Digest) KeyParameter(org.bouncycastle.crypto.params.KeyParameter) DecodingException(com.google.authenticator.blackberry.Base32String.DecodingException) HMac(org.bouncycastle.crypto.macs.HMac) Mac(org.bouncycastle.crypto.Mac)

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