use of org.bouncycastle.crypto.params.KeyParameter in project xipki by xipki.
the class EmulatorP11Identity method aesGmac.
// TODO: check the correctness
private byte[] aesGmac(P11Params params, byte[] contentToSign) throws P11TokenException {
if (params == null) {
throw new P11TokenException("iv must not be null");
}
byte[] iv;
if (params instanceof P11IVParams) {
iv = ((P11IVParams) params).getIV();
} else {
throw new P11TokenException("params must be instanceof P11IVParams");
}
GMac gmac = new GMac(new GCMBlockCipher(new AESEngine()));
ParametersWithIV paramsWithIv = new ParametersWithIV(new KeyParameter(signingKey.getEncoded()), iv);
gmac.init(paramsWithIv);
gmac.update(contentToSign, 0, contentToSign.length);
byte[] signature = new byte[gmac.getMacSize()];
gmac.doFinal(signature, 0);
return signature;
}
use of org.bouncycastle.crypto.params.KeyParameter in project Zom-Android by zom.
the class Downloader method setupInputStream.
public static InputStream setupInputStream(InputStream is, byte[] keyAndIv) {
if (keyAndIv != null && keyAndIv.length == 48) {
byte[] key = new byte[32];
byte[] iv = new byte[16];
System.arraycopy(keyAndIv, 0, iv, 0, 16);
System.arraycopy(keyAndIv, 16, key, 0, 32);
AEADBlockCipher cipher = new GCMBlockCipher(new AESEngine());
cipher.init(true, new AEADParameters(new KeyParameter(key), 128, iv));
return new CipherInputStream(is, cipher);
} else {
return is;
}
}
use of org.bouncycastle.crypto.params.KeyParameter in project rxlib by RockyLOMO.
the class CryptBase method setIV.
protected void setIV(byte[] iv, boolean isEncrypt) {
if (_ivLength == 0) {
return;
}
if (isEncrypt) {
_encryptIV = new byte[_ivLength];
System.arraycopy(iv, 0, _encryptIV, 0, _ivLength);
try {
encCipher = getCipher(isEncrypt);
ParametersWithIV parameterIV = new ParametersWithIV(new KeyParameter(_key.getEncoded()), _encryptIV);
encCipher.init(isEncrypt, parameterIV);
} catch (InvalidAlgorithmParameterException e) {
logger.info(e.toString());
}
} else {
_decryptIV = new byte[_ivLength];
System.arraycopy(iv, 0, _decryptIV, 0, _ivLength);
try {
decCipher = getCipher(isEncrypt);
ParametersWithIV parameterIV = new ParametersWithIV(new KeyParameter(_key.getEncoded()), _decryptIV);
decCipher.init(isEncrypt, parameterIV);
} catch (InvalidAlgorithmParameterException e) {
logger.info(e.toString());
}
}
}
use of org.bouncycastle.crypto.params.KeyParameter in project photon-model by vmware.
the class EncryptorService method getCipher.
/*
* Cipher settings
*/
private BufferedBlockCipher getCipher(boolean forEncryption) {
BufferedBlockCipher cipher = new PaddedBufferedBlockCipher(new CBCBlockCipher(new AESEngine()), new PKCS7Padding());
cipher.init(forEncryption, new ParametersWithIV(new KeyParameter(this.keyBytes, IV_LENGTH, this.keyBytes.length - IV_LENGTH), this.keyBytes, 0, IV_LENGTH));
return cipher;
}
use of org.bouncycastle.crypto.params.KeyParameter 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);
}
Aggregations