use of org.bouncycastle.crypto.CipherParameters 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.CipherParameters 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.");
}
}
use of org.bouncycastle.crypto.CipherParameters in project samourai-wallet-android by Samourai-Wallet.
the class AESUtil method encryptWithSetMode.
public static String encryptWithSetMode(String cleartext, CharSequenceX password, int iterations, int mode, @Nullable BlockCipherPadding padding) throws DecryptionException, UnsupportedEncodingException {
final int AESBlockSize = 4;
if (password == null) {
throw new DecryptionException("Password null");
}
// Use secure random to generate a 16 byte iv
SecureRandom random = new SecureRandom();
byte[] iv = new byte[AESBlockSize * 4];
random.nextBytes(iv);
byte[] clearbytes = cleartext.getBytes("UTF-8");
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(true, params);
byte[] outBuf = cipherData(cipher, clearbytes);
// Append to IV to the output
int len1 = iv.length;
int len2 = outBuf.length;
byte[] ivAppended = new byte[len1 + len2];
System.arraycopy(iv, 0, ivAppended, 0, len1);
System.arraycopy(outBuf, 0, ivAppended, len1, len2);
// String ret = Base64.encodeBase64String(ivAppended);
byte[] raw = Base64.encodeBase64(ivAppended);
return new String(raw);
}
use of org.bouncycastle.crypto.CipherParameters in project keepass2android by PhilippC.
the class JCEStreamCipher method engineInit.
protected void engineInit(int opmode, Key key, AlgorithmParameterSpec params, SecureRandom random) throws InvalidKeyException, InvalidAlgorithmParameterException {
CipherParameters param;
this.pbeSpec = null;
this.pbeAlgorithm = null;
this.engineParams = null;
//
if (!(key instanceof SecretKey)) {
throw new InvalidKeyException("Key for algorithm " + key.getAlgorithm() + " not suitable for symmetric enryption.");
}
if (key instanceof JCEPBEKey) {
JCEPBEKey k = (JCEPBEKey) key;
if (k.getOID() != null) {
pbeAlgorithm = k.getOID().getId();
} else {
pbeAlgorithm = k.getAlgorithm();
}
if (k.getParam() != null) {
param = k.getParam();
pbeSpec = new PBEParameterSpec(k.getSalt(), k.getIterationCount());
} else if (params instanceof PBEParameterSpec) {
param = PBE.Util.makePBEParameters(k, params, cipher.getAlgorithmName());
pbeSpec = (PBEParameterSpec) params;
} else {
throw new InvalidAlgorithmParameterException("PBE requires PBE parameters to be set.");
}
if (k.getIvSize() != 0) {
ivParam = (ParametersWithIV) param;
}
} else if (params == null) {
param = new KeyParameter(key.getEncoded());
} else if (params instanceof IvParameterSpec) {
param = new ParametersWithIV(new KeyParameter(key.getEncoded()), ((IvParameterSpec) params).getIV());
ivParam = (ParametersWithIV) param;
} else {
throw new IllegalArgumentException("unknown parameter type.");
}
if ((ivLength != 0) && !(param instanceof ParametersWithIV)) {
SecureRandom ivRandom = random;
if (ivRandom == null) {
ivRandom = new SecureRandom();
}
if ((opmode == Cipher.ENCRYPT_MODE) || (opmode == Cipher.WRAP_MODE)) {
byte[] iv = new byte[ivLength];
ivRandom.nextBytes(iv);
param = new ParametersWithIV(param, iv);
ivParam = (ParametersWithIV) param;
} else {
throw new InvalidAlgorithmParameterException("no IV set when one expected");
}
}
switch(opmode) {
case Cipher.ENCRYPT_MODE:
case Cipher.WRAP_MODE:
cipher.init(true, param);
break;
case Cipher.DECRYPT_MODE:
case Cipher.UNWRAP_MODE:
cipher.init(false, param);
break;
default:
System.out.println("eeek!");
}
}
use of org.bouncycastle.crypto.CipherParameters in project nem2-sdk-java by nemtech.
the class AESGCM method createAESGCMCipher.
/**
* Creates a new AES/GCM/NoPadding cipher.
*
* @param secretKey The AES key. Must not be {@code null}.
* @param forEncryption If {@code true} creates an encryption cipher, else creates a decryption
* cipher.
* @param iv The initialisation vector (IV). Must not be {@code null}.
* @return The AES/GCM/NoPadding cipher.
*/
private static GCMBlockCipher createAESGCMCipher(final byte[] secretKey, final boolean forEncryption, final byte[] iv) {
// Initialise AES cipher
BlockCipher cipher = createCipher(secretKey, forEncryption);
// Create GCM cipher with AES
GCMBlockCipher gcm = new GCMBlockCipher(cipher);
final KeyParameter keyParam = new KeyParameter(secretKey);
final CipherParameters params = new ParametersWithIV(keyParam, iv);
gcm.init(forEncryption, params);
return gcm;
}
Aggregations