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