Search in sources :

Example 6 with BufferedBlockCipher

use of org.bouncycastle.crypto.BufferedBlockCipher in project rskj by rsksmart.

the class CryptoTest method test14.

// ECIES_AES128_SHA256 + No Ephemeral Key + IV(all zeroes)
@Test
public void test14() throws Throwable {
    AESEngine aesEngine = new AESEngine();
    IESEngine iesEngine = new IESEngine(new ECDHBasicAgreement(), new KDF2BytesGenerator(new SHA256Digest()), new HMac(new SHA256Digest()), new BufferedBlockCipher(new SICBlockCipher(aesEngine)));
    byte[] d = new byte[] { 1, 2, 3, 4, 5, 6, 7, 8 };
    byte[] e = new byte[] { 8, 7, 6, 5, 4, 3, 2, 1 };
    IESParameters p = new IESWithCipherParameters(d, e, 64, 128);
    ParametersWithIV parametersWithIV = new ParametersWithIV(p, new byte[16]);
    ECKeyPairGenerator eGen = new ECKeyPairGenerator();
    KeyGenerationParameters gParam = new ECKeyGenerationParameters(ECKey.CURVE, new SecureRandom());
    eGen.init(gParam);
    AsymmetricCipherKeyPair p1 = eGen.generateKeyPair();
    AsymmetricCipherKeyPair p2 = eGen.generateKeyPair();
    ECKeyGenerationParameters keygenParams = new ECKeyGenerationParameters(ECKey.CURVE, new SecureRandom());
    ECKeyPairGenerator generator = new ECKeyPairGenerator();
    generator.init(keygenParams);
    ECKeyPairGenerator gen = new ECKeyPairGenerator();
    gen.init(new ECKeyGenerationParameters(ECKey.CURVE, new SecureRandom()));
    iesEngine.init(true, p1.getPrivate(), p2.getPublic(), parametersWithIV);
    byte[] message = Hex.decode("010101");
    log.info("payload: {}", ByteUtil.toHexString(message));
    byte[] cipher = iesEngine.processBlock(message, 0, message.length);
    log.info("cipher: {}", ByteUtil.toHexString(cipher));
    IESEngine decryptorIES_Engine = new IESEngine(new ECDHBasicAgreement(), new KDF2BytesGenerator(new SHA256Digest()), new HMac(new SHA256Digest()), new BufferedBlockCipher(new SICBlockCipher(aesEngine)));
    decryptorIES_Engine.init(false, p2.getPrivate(), p1.getPublic(), parametersWithIV);
    byte[] orig = decryptorIES_Engine.processBlock(cipher, 0, cipher.length);
    log.info("orig: " + ByteUtil.toHexString(orig));
}
Also used : ECKeyPairGenerator(org.bouncycastle.crypto.generators.ECKeyPairGenerator) AESEngine(org.bouncycastle.crypto.engines.AESEngine) HMac(org.bouncycastle.crypto.macs.HMac) SecureRandom(java.security.SecureRandom) SICBlockCipher(org.bouncycastle.crypto.modes.SICBlockCipher) KeyGenerationParameters(org.bouncycastle.crypto.KeyGenerationParameters) IESEngine(org.bouncycastle.crypto.engines.IESEngine) AsymmetricCipherKeyPair(org.bouncycastle.crypto.AsymmetricCipherKeyPair) ECDHBasicAgreement(org.bouncycastle.crypto.agreement.ECDHBasicAgreement) KDF2BytesGenerator(org.bouncycastle.crypto.generators.KDF2BytesGenerator) SHA256Digest(org.bouncycastle.crypto.digests.SHA256Digest) BufferedBlockCipher(org.bouncycastle.crypto.BufferedBlockCipher) Test(org.junit.Test)

Example 7 with BufferedBlockCipher

use of org.bouncycastle.crypto.BufferedBlockCipher in project rskj by rsksmart.

the class ECIESCoder method decrypt.

public static byte[] decrypt(ECPoint ephem, BigInteger prv, byte[] iv, byte[] cipher, byte[] macData) throws InvalidCipherTextException {
    AESEngine aesEngine = new AESEngine();
    EthereumIESEngine iesEngine = new EthereumIESEngine(new ECDHBasicAgreement(), new ConcatKDFBytesGenerator(new SHA256Digest()), new HMac(new SHA256Digest()), new SHA256Digest(), new BufferedBlockCipher(new SICBlockCipher(aesEngine)));
    byte[] d = new byte[] {};
    byte[] e = new byte[] {};
    IESParameters p = new IESWithCipherParameters(d, e, KEY_SIZE, KEY_SIZE);
    ParametersWithIV parametersWithIV = new ParametersWithIV(p, iv);
    iesEngine.init(false, new ECPrivateKeyParameters(prv, CURVE), new ECPublicKeyParameters(ephem, CURVE), parametersWithIV);
    return iesEngine.processBlock(cipher, 0, cipher.length, macData);
}
Also used : AESEngine(org.bouncycastle.crypto.engines.AESEngine) HMac(org.bouncycastle.crypto.macs.HMac) SICBlockCipher(org.bouncycastle.crypto.modes.SICBlockCipher) ECDHBasicAgreement(org.bouncycastle.crypto.agreement.ECDHBasicAgreement) ConcatKDFBytesGenerator(org.ethereum.ConcatKDFBytesGenerator) SHA256Digest(org.bouncycastle.crypto.digests.SHA256Digest) BufferedBlockCipher(org.bouncycastle.crypto.BufferedBlockCipher)

Example 8 with BufferedBlockCipher

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

the class AESUtil method decryptWithSetMode.

public static String decryptWithSetMode(String ciphertext, CharSequenceX password, int iterations, int mode, @Nullable BlockCipherPadding padding) throws InvalidCipherTextException, UnsupportedEncodingException, DecryptionException {
    final int AESBlockSize = 4;
    byte[] cipherdata = Base64.decodeBase64(ciphertext.getBytes());
    // Separate the IV and cipher data
    byte[] iv = copyOfRange(cipherdata, 0, AESBlockSize * 4);
    byte[] input = copyOfRange(cipherdata, AESBlockSize * 4, cipherdata.length);
    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(false, params);
    // create a temporary buffer to decode into (includes padding)
    byte[] buf = new byte[cipher.getOutputSize(input.length)];
    int len = cipher.processBytes(input, 0, input.length, buf, 0);
    len += cipher.doFinal(buf, len);
    // remove padding
    byte[] out = new byte[len];
    System.arraycopy(buf, 0, out, 0, len);
    // return string representation of decoded bytes
    String result = new String(out, "UTF-8");
    if (result.isEmpty()) {
        throw new DecryptionException("Decrypted string is empty.");
    }
    return result;
}
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) 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 9 with BufferedBlockCipher

use of org.bouncycastle.crypto.BufferedBlockCipher in project photon-model by vmware.

the class EncryptorService method decrypt.

/**
 * Decrypts the provided byte array.
 * @param input
 *         Byte array (in base 64) to be decrypted
 * @return The decrypted version of the input byte array.
 */
public byte[] decrypt(final byte[] input) {
    if (input == null || input.length == 0) {
        return input;
    }
    try {
        BufferedBlockCipher cipher = getCipher(false);
        byte[] bytes = Base64.getDecoder().decode(input);
        byte[] output = new byte[cipher.getOutputSize(bytes.length)];
        int length = cipher.processBytes(bytes, 0, bytes.length, output, 0);
        length += cipher.doFinal(output, length);
        return Arrays.copyOfRange(output, 0, length);
    } catch (Exception e) {
        throw new LocalizableValidationException(e, "Decryption error!", "common.dercyption.error");
    }
}
Also used : LocalizableValidationException(com.vmware.xenon.common.LocalizableValidationException) BufferedBlockCipher(org.bouncycastle.crypto.BufferedBlockCipher) PaddedBufferedBlockCipher(org.bouncycastle.crypto.paddings.PaddedBufferedBlockCipher) IOException(java.io.IOException) LocalizableValidationException(com.vmware.xenon.common.LocalizableValidationException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException)

Example 10 with BufferedBlockCipher

use of org.bouncycastle.crypto.BufferedBlockCipher in project photon-model by vmware.

the class EncryptorService method encrypt.

/**
 * Encrypts the provided byte array.
 * @param input
 *         Byte array to be encrypted
 * @return The encrypted version of the input byte array (in base 64).
 */
public byte[] encrypt(final byte[] input) {
    if (input == null || input.length == 0) {
        return input;
    }
    try {
        BufferedBlockCipher cipher = getCipher(true);
        byte[] output = new byte[cipher.getOutputSize(input.length)];
        int length = cipher.processBytes(input, 0, input.length, output, 0);
        length += cipher.doFinal(output, length);
        return Base64.getEncoder().encode(Arrays.copyOfRange(output, 0, length));
    } catch (Exception e) {
        throw new LocalizableValidationException(e, "Encryption error!", "common.ecryption.error");
    }
}
Also used : LocalizableValidationException(com.vmware.xenon.common.LocalizableValidationException) BufferedBlockCipher(org.bouncycastle.crypto.BufferedBlockCipher) PaddedBufferedBlockCipher(org.bouncycastle.crypto.paddings.PaddedBufferedBlockCipher) IOException(java.io.IOException) LocalizableValidationException(com.vmware.xenon.common.LocalizableValidationException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException)

Aggregations

BufferedBlockCipher (org.bouncycastle.crypto.BufferedBlockCipher)24 PaddedBufferedBlockCipher (org.bouncycastle.crypto.paddings.PaddedBufferedBlockCipher)19 CBCBlockCipher (org.bouncycastle.crypto.modes.CBCBlockCipher)15 AESEngine (org.bouncycastle.crypto.engines.AESEngine)14 ParametersWithIV (org.bouncycastle.crypto.params.ParametersWithIV)14 KeyParameter (org.bouncycastle.crypto.params.KeyParameter)10 CipherParameters (org.bouncycastle.crypto.CipherParameters)7 SICBlockCipher (org.bouncycastle.crypto.modes.SICBlockCipher)6 InvalidCipherTextException (org.bouncycastle.crypto.InvalidCipherTextException)5 IOException (java.io.IOException)4 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)4 SecureRandom (java.security.SecureRandom)4 DataLengthException (org.bouncycastle.crypto.DataLengthException)4 PBEParametersGenerator (org.bouncycastle.crypto.PBEParametersGenerator)4 ECDHBasicAgreement (org.bouncycastle.crypto.agreement.ECDHBasicAgreement)4 SHA256Digest (org.bouncycastle.crypto.digests.SHA256Digest)4 PKCS5S2ParametersGenerator (org.bouncycastle.crypto.generators.PKCS5S2ParametersGenerator)4 HMac (org.bouncycastle.crypto.macs.HMac)4 OFBBlockCipher (org.bouncycastle.crypto.modes.OFBBlockCipher)4 AESFastEngine (org.bouncycastle.crypto.engines.AESFastEngine)3