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);
}
}
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);
}
}
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);
}
}
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);
}
}
Aggregations