Search in sources :

Example 41 with InvalidCipherTextException

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

the class ECIESCoder method encrypt.

public static byte[] encrypt(ECPoint toPub, byte[] plaintext, byte[] macData) {
    ECKeyPairGenerator eGen = new ECKeyPairGenerator();
    SecureRandom random = new SecureRandom();
    KeyGenerationParameters gParam = new ECKeyGenerationParameters(CURVE, random);
    eGen.init(gParam);
    byte[] iv = new byte[KEY_SIZE / 8];
    new SecureRandom().nextBytes(iv);
    AsymmetricCipherKeyPair ephemPair = eGen.generateKeyPair();
    BigInteger prv = ((ECPrivateKeyParameters) ephemPair.getPrivate()).getD();
    ECPoint pub = ((ECPublicKeyParameters) ephemPair.getPublic()).getQ();
    EthereumIESEngine iesEngine = makeIESEngine(true, toPub, prv, iv);
    ECKeyGenerationParameters keygenParams = new ECKeyGenerationParameters(CURVE, random);
    ECKeyPairGenerator generator = new ECKeyPairGenerator();
    generator.init(keygenParams);
    ECKeyPairGenerator gen = new ECKeyPairGenerator();
    gen.init(new ECKeyGenerationParameters(ECKey.CURVE, random));
    byte[] cipher;
    try {
        cipher = iesEngine.processBlock(plaintext, 0, plaintext.length, macData);
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        bos.write(pub.getEncoded(false));
        bos.write(iv);
        bos.write(cipher);
        return bos.toByteArray();
    } catch (InvalidCipherTextException e) {
        throw Throwables.propagate(e);
    } catch (IOException e) {
        throw Throwables.propagate(e);
    }
}
Also used : ECKeyPairGenerator(org.bouncycastle.crypto.generators.ECKeyPairGenerator) InvalidCipherTextException(org.bouncycastle.crypto.InvalidCipherTextException) SecureRandom(java.security.SecureRandom) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) ECPoint(org.bouncycastle.math.ec.ECPoint) KeyGenerationParameters(org.bouncycastle.crypto.KeyGenerationParameters) AsymmetricCipherKeyPair(org.bouncycastle.crypto.AsymmetricCipherKeyPair) BigInteger(java.math.BigInteger)

Example 42 with InvalidCipherTextException

use of org.bouncycastle.crypto.InvalidCipherTextException in project zeppelin by apache.

the class Encryptor method decrypt.

public String decrypt(String base64Input) throws IOException {
    byte[] input = Base64.decode(base64Input);
    byte[] result = new byte[decryptCipher.getOutputSize(input.length)];
    int size = decryptCipher.processBytes(input, 0, input.length, result, 0);
    try {
        size += decryptCipher.doFinal(result, size);
        byte[] out = new byte[size];
        System.arraycopy(result, 0, out, 0, size);
        return new String(out);
    } catch (InvalidCipherTextException e) {
        throw new IOException("Cannot decrypt: " + e.getMessage(), e);
    }
}
Also used : InvalidCipherTextException(org.bouncycastle.crypto.InvalidCipherTextException) IOException(java.io.IOException)

Example 43 with InvalidCipherTextException

use of org.bouncycastle.crypto.InvalidCipherTextException in project zeppelin by apache.

the class Encryptor method encrypt.

public String encrypt(String inputString) throws IOException {
    byte[] input = inputString.getBytes();
    byte[] result = new byte[encryptCipher.getOutputSize(input.length)];
    int size = encryptCipher.processBytes(input, 0, input.length, result, 0);
    try {
        size += encryptCipher.doFinal(result, size);
        byte[] out = new byte[size];
        System.arraycopy(result, 0, out, 0, size);
        return new String(Base64.encode(out));
    } catch (InvalidCipherTextException e) {
        throw new IOException("Cannot encrypt: " + e.getMessage(), e);
    }
}
Also used : InvalidCipherTextException(org.bouncycastle.crypto.InvalidCipherTextException) IOException(java.io.IOException)

Example 44 with InvalidCipherTextException

use of org.bouncycastle.crypto.InvalidCipherTextException 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 45 with InvalidCipherTextException

use of org.bouncycastle.crypto.InvalidCipherTextException 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)

Aggregations

InvalidCipherTextException (org.bouncycastle.crypto.InvalidCipherTextException)46 KeyParameter (org.bouncycastle.crypto.params.KeyParameter)13 ParametersWithIV (org.bouncycastle.crypto.params.ParametersWithIV)13 AESEngine (org.bouncycastle.crypto.engines.AESEngine)12 CipherParameters (org.bouncycastle.crypto.CipherParameters)11 PaddedBufferedBlockCipher (org.bouncycastle.crypto.paddings.PaddedBufferedBlockCipher)9 IOException (java.io.IOException)7 DataLengthException (org.bouncycastle.crypto.DataLengthException)7 CBCBlockCipher (org.bouncycastle.crypto.modes.CBCBlockCipher)7 GCMBlockCipher (org.bouncycastle.crypto.modes.GCMBlockCipher)7 GoCipher (com.thoughtworks.go.security.GoCipher)6 Test (org.junit.Test)6 BadPaddingException (javax.crypto.BadPaddingException)5 BlockCipher (org.bouncycastle.crypto.BlockCipher)5 BufferedBlockCipher (org.bouncycastle.crypto.BufferedBlockCipher)5 UrlArgument (com.thoughtworks.go.util.command.UrlArgument)4 UnsupportedEncodingException (java.io.UnsupportedEncodingException)4 ExtendedInvalidCipherTextException (org.openremote.agent.protocol.bluetooth.mesh.utils.ExtendedInvalidCipherTextException)4 IllegalBlockSizeException (javax.crypto.IllegalBlockSizeException)3 SecretKeySpec (javax.crypto.spec.SecretKeySpec)3