use of org.bouncycastle.crypto.params.KeyParameter in project Conversations by siacs.
the class AbstractConnectionManager method createOutputStream.
private static OutputStream createOutputStream(DownloadableFile file, boolean gcm, boolean append) {
FileOutputStream os;
try {
os = new FileOutputStream(file, append);
if (file.getKey() == null) {
return os;
}
} catch (FileNotFoundException e) {
return null;
}
try {
if (gcm) {
AEADBlockCipher cipher = new GCMBlockCipher(new AESEngine());
cipher.init(false, new AEADParameters(new KeyParameter(file.getKey()), 128, file.getIv()));
return new org.bouncycastle.crypto.io.CipherOutputStream(os, cipher);
} else {
IvParameterSpec ips = new IvParameterSpec(file.getIv());
Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
cipher.init(Cipher.DECRYPT_MODE, new SecretKeySpec(file.getKey(), "AES"), ips);
Log.d(Config.LOGTAG, "opening encrypted output stream");
return new CipherOutputStream(os, cipher);
}
} catch (InvalidKeyException e) {
return null;
} catch (NoSuchAlgorithmException e) {
return null;
} catch (NoSuchPaddingException e) {
return null;
} catch (InvalidAlgorithmParameterException e) {
return null;
}
}
use of org.bouncycastle.crypto.params.KeyParameter in project syncany by syncany.
the class AesGcmCipherSpec method newCipherInputStream.
@Override
public InputStream newCipherInputStream(InputStream underlyingInputStream, byte[] secretKey, byte[] iv) throws CipherException {
AEADBlockCipher cipher = new GCMBlockCipher(new AESEngine());
cipher.init(false, new AEADParameters(new KeyParameter(secretKey), MAC_SIZE, iv));
return new org.bouncycastle.crypto.io.CipherInputStream(underlyingInputStream, cipher);
}
use of org.bouncycastle.crypto.params.KeyParameter in project syncany by syncany.
the class TwofishGcmCipherSpec method newCipherInputStream.
@Override
public InputStream newCipherInputStream(InputStream underlyingInputStream, byte[] secretKey, byte[] iv) throws CipherException {
AEADBlockCipher cipher = new GCMBlockCipher(new TwofishEngine());
cipher.init(false, new AEADParameters(new KeyParameter(secretKey), MAC_SIZE, iv));
return new org.bouncycastle.crypto.io.CipherInputStream(underlyingInputStream, cipher);
}
use of org.bouncycastle.crypto.params.KeyParameter in project gocd by gocd.
the class GoCipher method decipher.
public String decipher(byte[] key, String cipherText) throws InvalidCipherTextException {
PaddedBufferedBlockCipher cipher = new PaddedBufferedBlockCipher(new CBCBlockCipher(new DESEngine()));
cipher.init(false, new KeyParameter(Hex.decode(key)));
byte[] cipherTextBytes = java.util.Base64.getDecoder().decode(cipherText);
byte[] plainTextBytes = new byte[cipher.getOutputSize(cipherTextBytes.length)];
int outputLength = cipher.processBytes(cipherTextBytes, 0, cipherTextBytes.length, plainTextBytes, 0);
cipher.doFinal(plainTextBytes, outputLength);
int paddingStarts = plainTextBytes.length - 1;
for (; paddingStarts >= 0; paddingStarts--) {
if (plainTextBytes[paddingStarts] != 0) {
break;
}
}
return new String(plainTextBytes, 0, paddingStarts + 1);
}
use of org.bouncycastle.crypto.params.KeyParameter in project google-authenticator by google.
the class AuthenticatorScreen method computePin.
/**
* Computes the one-time PIN given the secret key.
*
* @param secret
* the secret key
* @return the PIN
* @throws GeneralSecurityException
* @throws DecodingException
* If the key string is improperly encoded.
*/
public static String computePin(String secret, Long counter) {
try {
final byte[] keyBytes = Base32String.decode(secret);
Mac mac = new HMac(new SHA1Digest());
mac.init(new KeyParameter(keyBytes));
PasscodeGenerator pcg = new PasscodeGenerator(mac);
if (counter == null) {
// time-based totp
return pcg.generateTimeoutCode();
} else {
// counter-based hotp
return pcg.generateResponseCode(counter.longValue());
}
} catch (RuntimeException e) {
return "General security exception";
} catch (DecodingException e) {
return "Decoding exception";
}
}
Aggregations