Search in sources :

Example 6 with InvalidCipherTextException

use of org.spongycastle.crypto.InvalidCipherTextException in project bitcoin-wallet by bitcoin-wallet.

the class Crypto method decryptRaw.

/**
 * Decrypt bytes previously encrypted with this class.
 *
 * @param bytesToDecode
 *            The bytes to decrypt
 * @param passwordbThe
 *            password to use for decryption
 * @return The decrypted bytes
 * @throws IOException
 */
private static byte[] decryptRaw(final byte[] bytesToDecode, final char[] password) throws IOException {
    try {
        // separate the salt and bytes to decrypt
        final byte[] salt = new byte[SALT_LENGTH];
        System.arraycopy(bytesToDecode, 0, salt, 0, SALT_LENGTH);
        final byte[] cipherBytes = new byte[bytesToDecode.length - SALT_LENGTH];
        System.arraycopy(bytesToDecode, SALT_LENGTH, cipherBytes, 0, bytesToDecode.length - SALT_LENGTH);
        final ParametersWithIV key = (ParametersWithIV) getAESPasswordKey(password, salt);
        // decrypt the message
        final BufferedBlockCipher cipher = new PaddedBufferedBlockCipher(new CBCBlockCipher(new AESFastEngine()));
        cipher.init(false, key);
        final byte[] decryptedBytes = new byte[cipher.getOutputSize(cipherBytes.length)];
        final int processLen = cipher.processBytes(cipherBytes, 0, cipherBytes.length, decryptedBytes, 0);
        final int doFinalLen = cipher.doFinal(decryptedBytes, processLen);
        return Arrays.copyOf(decryptedBytes, processLen + doFinalLen);
    } catch (final InvalidCipherTextException | DataLengthException x) {
        throw new IOException("Could not decrypt bytes", x);
    }
}
Also used : ParametersWithIV(org.spongycastle.crypto.params.ParametersWithIV) PaddedBufferedBlockCipher(org.spongycastle.crypto.paddings.PaddedBufferedBlockCipher) InvalidCipherTextException(org.spongycastle.crypto.InvalidCipherTextException) BufferedBlockCipher(org.spongycastle.crypto.BufferedBlockCipher) PaddedBufferedBlockCipher(org.spongycastle.crypto.paddings.PaddedBufferedBlockCipher) DataLengthException(org.spongycastle.crypto.DataLengthException) CBCBlockCipher(org.spongycastle.crypto.modes.CBCBlockCipher) AESFastEngine(org.spongycastle.crypto.engines.AESFastEngine) IOException(java.io.IOException)

Example 7 with InvalidCipherTextException

use of org.spongycastle.crypto.InvalidCipherTextException in project libsignal-service-java by signalapp.

the class ProfileCipher method encryptName.

public byte[] encryptName(byte[] input, int paddedLength) {
    try {
        byte[] inputPadded = new byte[paddedLength];
        if (input.length > inputPadded.length) {
            throw new IllegalArgumentException("Input is too long: " + new String(input));
        }
        System.arraycopy(input, 0, inputPadded, 0, input.length);
        byte[] nonce = Util.getSecretBytes(12);
        GCMBlockCipher cipher = new GCMBlockCipher(new AESFastEngine());
        cipher.init(true, new AEADParameters(new KeyParameter(key), 128, nonce));
        byte[] ciphertext = new byte[cipher.getUpdateOutputSize(inputPadded.length)];
        cipher.processBytes(inputPadded, 0, inputPadded.length, ciphertext, 0);
        byte[] tag = new byte[cipher.getOutputSize(0)];
        cipher.doFinal(tag, 0);
        return ByteUtil.combine(nonce, ciphertext, tag);
    } catch (InvalidCipherTextException e) {
        throw new AssertionError(e);
    }
}
Also used : InvalidCipherTextException(org.spongycastle.crypto.InvalidCipherTextException) AEADParameters(org.spongycastle.crypto.params.AEADParameters) KeyParameter(org.spongycastle.crypto.params.KeyParameter) AESFastEngine(org.spongycastle.crypto.engines.AESFastEngine) GCMBlockCipher(org.spongycastle.crypto.modes.GCMBlockCipher)

Example 8 with InvalidCipherTextException

use of org.spongycastle.crypto.InvalidCipherTextException in project libsignal-service-java by signalapp.

the class ProfileCipherInputStream method read.

@Override
public int read(byte[] output, int outputOffset, int outputLength) throws IOException {
    if (finished)
        return -1;
    try {
        byte[] ciphertext = new byte[outputLength / 2];
        int read = in.read(ciphertext, 0, ciphertext.length);
        if (read == -1) {
            if (cipher.getOutputSize(0) > outputLength) {
                throw new AssertionError("Need: " + cipher.getOutputSize(0) + " but only have: " + outputLength);
            }
            finished = true;
            return cipher.doFinal(output, outputOffset);
        } else {
            if (cipher.getUpdateOutputSize(read) > outputLength) {
                throw new AssertionError("Need: " + cipher.getOutputSize(read) + " but only have: " + outputLength);
            }
            return cipher.processBytes(ciphertext, 0, read, output, outputOffset);
        }
    } catch (InvalidCipherTextException e) {
        throw new IOException(e);
    }
}
Also used : InvalidCipherTextException(org.spongycastle.crypto.InvalidCipherTextException) IOException(java.io.IOException)

Example 9 with InvalidCipherTextException

use of org.spongycastle.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.spongycastle.crypto.generators.ECKeyPairGenerator) InvalidCipherTextException(org.spongycastle.crypto.InvalidCipherTextException) SecureRandom(java.security.SecureRandom) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) ECPoint(org.spongycastle.math.ec.ECPoint) KeyGenerationParameters(org.spongycastle.crypto.KeyGenerationParameters) AsymmetricCipherKeyPair(org.spongycastle.crypto.AsymmetricCipherKeyPair) BigInteger(java.math.BigInteger)

Aggregations

InvalidCipherTextException (org.spongycastle.crypto.InvalidCipherTextException)9 IOException (java.io.IOException)6 DataLengthException (org.spongycastle.crypto.DataLengthException)4 AESFastEngine (org.spongycastle.crypto.engines.AESFastEngine)4 ByteArrayOutputStream (java.io.ByteArrayOutputStream)3 ByteArrayInputStream (java.io.ByteArrayInputStream)2 BufferedBlockCipher (org.spongycastle.crypto.BufferedBlockCipher)2 CBCBlockCipher (org.spongycastle.crypto.modes.CBCBlockCipher)2 GCMBlockCipher (org.spongycastle.crypto.modes.GCMBlockCipher)2 PaddedBufferedBlockCipher (org.spongycastle.crypto.paddings.PaddedBufferedBlockCipher)2 AEADParameters (org.spongycastle.crypto.params.AEADParameters)2 KeyParameter (org.spongycastle.crypto.params.KeyParameter)2 ParametersWithIV (org.spongycastle.crypto.params.ParametersWithIV)2 ECPoint (org.spongycastle.math.ec.ECPoint)2 ByteBuf (io.netty.buffer.ByteBuf)1 BigInteger (java.math.BigInteger)1 SecureRandom (java.security.SecureRandom)1 Message (org.ethereum.net.message.Message)1 DisconnectMessage (org.ethereum.net.p2p.DisconnectMessage)1 HelloMessage (org.ethereum.net.p2p.HelloMessage)1