Search in sources :

Example 51 with CipherParameters

use of org.bouncycastle.crypto.CipherParameters in project OsmAnd-tools by osmandapp.

the class SigningUtils method decryptPassphrase.

/**
 * Steps (4) to (7): Decrypt passphrase with secret key
 * @param pass passphrase from withdrawal request
 * @param key secret key
 * @return decrypted passphrase for next steps
 * @throws BlockIOException
 */
static byte[] decryptPassphrase(byte[] pass, byte[] key) throws BlockIOException {
    PaddedBufferedBlockCipher aes = new PaddedBufferedBlockCipher(new AESEngine());
    CipherParameters aesKey = new KeyParameter(key);
    aes.init(false, aesKey);
    try {
        return cipherData(aes, pass);
    } catch (InvalidCipherTextException e) {
        throw new BlockIOException("Unexpected error while signing transaction. Please file an issue report.");
    }
}
Also used : CipherParameters(org.bouncycastle.crypto.CipherParameters) PaddedBufferedBlockCipher(org.bouncycastle.crypto.paddings.PaddedBufferedBlockCipher) AESEngine(org.bouncycastle.crypto.engines.AESEngine) InvalidCipherTextException(org.bouncycastle.crypto.InvalidCipherTextException) KeyParameter(org.bouncycastle.crypto.params.KeyParameter)

Example 52 with CipherParameters

use of org.bouncycastle.crypto.CipherParameters in project OsmAnd-tools by osmandapp.

the class SigningUtils method encryptPassphrase.

/**
 * Used only for testing: encrypt secret passphrase
 * @param plain plain passphrase
 * @param key secret key
 * @return encrypted passphrase
 * @throws BlockIOException
 */
static byte[] encryptPassphrase(String plain, byte[] key) throws BlockIOException {
    PaddedBufferedBlockCipher aes = new PaddedBufferedBlockCipher(new AESEngine());
    CipherParameters aesKey = new KeyParameter(key);
    aes.init(true, aesKey);
    try {
        return cipherData(aes, plain.getBytes("UTF-8"));
    } catch (InvalidCipherTextException e) {
        throw new BlockIOException("Unexpected error while signing transaction. Please file an issue report.");
    } catch (UnsupportedEncodingException e) {
        throw new BlockIOException("Your system does not seem to support UTF-8 encoding! Aborting signing process.");
    }
}
Also used : CipherParameters(org.bouncycastle.crypto.CipherParameters) PaddedBufferedBlockCipher(org.bouncycastle.crypto.paddings.PaddedBufferedBlockCipher) AESEngine(org.bouncycastle.crypto.engines.AESEngine) InvalidCipherTextException(org.bouncycastle.crypto.InvalidCipherTextException) KeyParameter(org.bouncycastle.crypto.params.KeyParameter) UnsupportedEncodingException(java.io.UnsupportedEncodingException)

Example 53 with CipherParameters

use of org.bouncycastle.crypto.CipherParameters in project samourai-wallet-android by Samourai-Wallet.

the class AESUtil method encryptWithSetMode.

public static String encryptWithSetMode(String cleartext, CharSequenceX password, int iterations, int mode, @Nullable BlockCipherPadding padding) throws DecryptionException, UnsupportedEncodingException {
    final int AESBlockSize = 4;
    if (password == null) {
        throw new DecryptionException("Password null");
    }
    // Use secure random to generate a 16 byte iv
    SecureRandom random = new SecureRandom();
    byte[] iv = new byte[AESBlockSize * 4];
    random.nextBytes(iv);
    byte[] clearbytes = cleartext.getBytes("UTF-8");
    PBEParametersGenerator generator = new PKCS5S2ParametersGenerator();
    generator.init(PBEParametersGenerator.PKCS5PasswordToUTF8Bytes(password.toString().toCharArray()), iv, iterations);
    KeyParameter keyParam = (KeyParameter) generator.generateDerivedParameters(256);
    CipherParameters params = new ParametersWithIV(keyParam, iv);
    BlockCipher cipherMode;
    if (mode == MODE_CBC) {
        cipherMode = new CBCBlockCipher(new AESEngine());
    } else {
        // mode == MODE_OFB
        cipherMode = new OFBBlockCipher(new AESEngine(), 128);
    }
    BufferedBlockCipher cipher;
    if (padding != null) {
        cipher = new PaddedBufferedBlockCipher(cipherMode, padding);
    } else {
        cipher = new BufferedBlockCipher(cipherMode);
    }
    cipher.reset();
    cipher.init(true, params);
    byte[] outBuf = cipherData(cipher, clearbytes);
    // Append to IV to the output
    int len1 = iv.length;
    int len2 = outBuf.length;
    byte[] ivAppended = new byte[len1 + len2];
    System.arraycopy(iv, 0, ivAppended, 0, len1);
    System.arraycopy(outBuf, 0, ivAppended, len1, len2);
    // String ret = Base64.encodeBase64String(ivAppended);
    byte[] raw = Base64.encodeBase64(ivAppended);
    return new String(raw);
}
Also used : AESEngine(org.bouncycastle.crypto.engines.AESEngine) OFBBlockCipher(org.bouncycastle.crypto.modes.OFBBlockCipher) PaddedBufferedBlockCipher(org.bouncycastle.crypto.paddings.PaddedBufferedBlockCipher) PKCS5S2ParametersGenerator(org.bouncycastle.crypto.generators.PKCS5S2ParametersGenerator) CBCBlockCipher(org.bouncycastle.crypto.modes.CBCBlockCipher) BufferedBlockCipher(org.bouncycastle.crypto.BufferedBlockCipher) BlockCipher(org.bouncycastle.crypto.BlockCipher) OFBBlockCipher(org.bouncycastle.crypto.modes.OFBBlockCipher) PaddedBufferedBlockCipher(org.bouncycastle.crypto.paddings.PaddedBufferedBlockCipher) KeyParameter(org.bouncycastle.crypto.params.KeyParameter) SecureRandom(java.security.SecureRandom) CipherParameters(org.bouncycastle.crypto.CipherParameters) ParametersWithIV(org.bouncycastle.crypto.params.ParametersWithIV) BufferedBlockCipher(org.bouncycastle.crypto.BufferedBlockCipher) PaddedBufferedBlockCipher(org.bouncycastle.crypto.paddings.PaddedBufferedBlockCipher) CBCBlockCipher(org.bouncycastle.crypto.modes.CBCBlockCipher) PBEParametersGenerator(org.bouncycastle.crypto.PBEParametersGenerator)

Example 54 with CipherParameters

use of org.bouncycastle.crypto.CipherParameters in project keepass2android by PhilippC.

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 JCEPBEKey) {
        JCEPBEKey k = (JCEPBEKey) 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)

Example 55 with CipherParameters

use of org.bouncycastle.crypto.CipherParameters in project nem2-sdk-java by nemtech.

the class AESGCM method createAESGCMCipher.

/**
 * Creates a new AES/GCM/NoPadding cipher.
 *
 * @param secretKey The AES key. Must not be {@code null}.
 * @param forEncryption If {@code true} creates an encryption cipher, else creates a decryption
 *     cipher.
 * @param iv The initialisation vector (IV). Must not be {@code null}.
 * @return The AES/GCM/NoPadding cipher.
 */
private static GCMBlockCipher createAESGCMCipher(final byte[] secretKey, final boolean forEncryption, final byte[] iv) {
    // Initialise AES cipher
    BlockCipher cipher = createCipher(secretKey, forEncryption);
    // Create GCM cipher with AES
    GCMBlockCipher gcm = new GCMBlockCipher(cipher);
    final KeyParameter keyParam = new KeyParameter(secretKey);
    final CipherParameters params = new ParametersWithIV(keyParam, iv);
    gcm.init(forEncryption, params);
    return gcm;
}
Also used : CipherParameters(org.bouncycastle.crypto.CipherParameters) ParametersWithIV(org.bouncycastle.crypto.params.ParametersWithIV) GCMBlockCipher(org.bouncycastle.crypto.modes.GCMBlockCipher) BlockCipher(org.bouncycastle.crypto.BlockCipher) KeyParameter(org.bouncycastle.crypto.params.KeyParameter) GCMBlockCipher(org.bouncycastle.crypto.modes.GCMBlockCipher)

Aggregations

CipherParameters (org.bouncycastle.crypto.CipherParameters)60 KeyParameter (org.bouncycastle.crypto.params.KeyParameter)35 ParametersWithIV (org.bouncycastle.crypto.params.ParametersWithIV)24 InvalidKeyException (java.security.InvalidKeyException)21 AESEngine (org.bouncycastle.crypto.engines.AESEngine)16 InvalidAlgorithmParameterException (java.security.InvalidAlgorithmParameterException)14 IvParameterSpec (javax.crypto.spec.IvParameterSpec)14 InvalidCipherTextException (org.bouncycastle.crypto.InvalidCipherTextException)14 PBEParameterSpec (javax.crypto.spec.PBEParameterSpec)12 ParametersWithRandom (org.bouncycastle.crypto.params.ParametersWithRandom)12 SecureRandom (java.security.SecureRandom)11 PaddedBufferedBlockCipher (org.bouncycastle.crypto.paddings.PaddedBufferedBlockCipher)11 CBCBlockCipher (org.bouncycastle.crypto.modes.CBCBlockCipher)9 BlockCipher (org.bouncycastle.crypto.BlockCipher)8 SecretKey (javax.crypto.SecretKey)7 BufferedBlockCipher (org.bouncycastle.crypto.BufferedBlockCipher)7 PBEParametersGenerator (org.bouncycastle.crypto.PBEParametersGenerator)7 GCMBlockCipher (org.bouncycastle.crypto.modes.GCMBlockCipher)7 IOException (java.io.IOException)5 UnsupportedEncodingException (java.io.UnsupportedEncodingException)5