use of javax.crypto.NoSuchPaddingException in project MTweaks-KernelAdiutorMOD by morogoku.
the class SecurityActivity method loadFingerprint.
private void loadFingerprint() {
try {
KeyStore keyStore = KeyStore.getInstance("AndroidKeyStore");
KeyGenerator keyGenerator = KeyGenerator.getInstance(KeyProperties.KEY_ALGORITHM_AES, "AndroidKeyStore");
mCipher = Cipher.getInstance(KeyProperties.KEY_ALGORITHM_AES + "/" + KeyProperties.BLOCK_MODE_CBC + "/" + KeyProperties.ENCRYPTION_PADDING_PKCS7);
keyStore.load(null);
keyGenerator.init(new KeyGenParameterSpec.Builder(KEY_NAME, KeyProperties.PURPOSE_ENCRYPT | KeyProperties.PURPOSE_DECRYPT).setBlockModes(KeyProperties.BLOCK_MODE_CBC).setUserAuthenticationRequired(true).setEncryptionPaddings(KeyProperties.ENCRYPTION_PADDING_PKCS7).build());
keyGenerator.generateKey();
SecretKey key = (SecretKey) keyStore.getKey(KEY_NAME, null);
mCipher.init(Cipher.ENCRYPT_MODE, key);
} catch (KeyStoreException | NoSuchProviderException | NoSuchAlgorithmException | NoSuchPaddingException | UnrecoverableKeyException | InvalidKeyException | CertificateException | InvalidAlgorithmParameterException | IOException e) {
return;
}
mCryptoObject = new FingerprintManagerCompat.CryptoObject(mCipher);
FrameLayout fingerprintParent = (FrameLayout) findViewById(R.id.fingerprint_parent);
final SwirlView swirlView = new SwirlView(new ContextThemeWrapper(this, R.style.Swirl));
swirlView.setLayoutParams(new FrameLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
fingerprintParent.addView(swirlView);
fingerprintParent.setVisibility(View.VISIBLE);
mFingerprintUiHelper = new FingerprintUiHelper.FingerprintUiHelperBuilder(mFingerprintManagerCompat).build(swirlView, new FingerprintUiHelper.Callback() {
@Override
public void onAuthenticated() {
try {
mCipher.doFinal(SECRET_MESSAGE.getBytes());
mPasswordWrong.setVisibility(View.GONE);
setResult(1);
finish();
} catch (IllegalBlockSizeException | BadPaddingException e) {
e.printStackTrace();
swirlView.setState(SwirlView.State.ERROR);
}
}
@Override
public void onError() {
}
});
mFingerprintUiHelper.startListening(mCryptoObject);
}
use of javax.crypto.NoSuchPaddingException in project data-transfer-project by google.
the class DecrypterImpl method decrypt.
@Override
public String decrypt(String encrypted) {
try {
byte[] decoded = BaseEncoding.base64Url().decode(encrypted);
Cipher cipher = Cipher.getInstance(transformation);
cipher.init(Cipher.DECRYPT_MODE, key);
byte[] decrypted = cipher.doFinal(decoded);
if (decrypted == null || decrypted.length <= 8) {
throw new RuntimeException("incorrect decrypted text.");
}
byte[] data = new byte[decrypted.length - 8];
System.arraycopy(decrypted, 8, data, 0, data.length);
return new String(data, Charsets.UTF_8);
} catch (BadPaddingException | IllegalBlockSizeException | InvalidKeyException | NoSuchAlgorithmException | NoSuchPaddingException e) {
logger.error("Error decrypting data, length: {}", encrypted.length(), e);
throw new RuntimeException(e);
}
}
use of javax.crypto.NoSuchPaddingException in project data-transfer-project by google.
the class EncrypterImpl method encrypt.
@Override
public String encrypt(String data) {
try {
Cipher cipher = Cipher.getInstance(transformation);
cipher.init(Cipher.ENCRYPT_MODE, key);
byte[] salt = new byte[8];
SecureRandom random = SecureRandom.getInstance("SHA1PRNG");
random.nextBytes(salt);
cipher.update(salt);
byte[] encrypted = cipher.doFinal(data.getBytes(Charsets.UTF_8));
return BaseEncoding.base64Url().encode(encrypted);
} catch (BadPaddingException | IllegalBlockSizeException | InvalidKeyException | NoSuchAlgorithmException | NoSuchPaddingException e) {
logger.error("Exception encrypting data, length: {}", data.length(), e);
throw new RuntimeException(e);
}
}
use of javax.crypto.NoSuchPaddingException in project pdfbox by apache.
the class SecurityHandler method encryptDataAESother.
/**
* Encrypt or decrypt data with AES with key length other than 256 bits.
*
* @param finalKey The final key obtained with via {@link #calcFinalKey(long, long)}.
* @param data The data to encrypt.
* @param output The output to write the encrypted data to.
* @param decrypt true to decrypt the data, false to encrypt it.
*
* @throws IOException If there is an error reading the data.
*/
private void encryptDataAESother(byte[] finalKey, InputStream data, OutputStream output, boolean decrypt) throws IOException {
byte[] iv = new byte[16];
if (!prepareAESInitializationVector(decrypt, iv, data, output)) {
return;
}
try {
Cipher decryptCipher;
try {
decryptCipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
} catch (NoSuchAlgorithmException e) {
// should never happen
throw new RuntimeException(e);
}
SecretKey aesKey = new SecretKeySpec(finalKey, "AES");
IvParameterSpec ips = new IvParameterSpec(iv);
decryptCipher.init(decrypt ? Cipher.DECRYPT_MODE : Cipher.ENCRYPT_MODE, aesKey, ips);
byte[] buffer = new byte[256];
int n;
while ((n = data.read(buffer)) != -1) {
byte[] dst = decryptCipher.update(buffer, 0, n);
if (dst != null) {
output.write(dst);
}
}
output.write(decryptCipher.doFinal());
} catch (InvalidKeyException | InvalidAlgorithmParameterException | NoSuchPaddingException | IllegalBlockSizeException | BadPaddingException e) {
throw new IOException(e);
}
}
use of javax.crypto.NoSuchPaddingException in project Signal-Android by signalapp.
the class KeyStoreHelper method seal.
@RequiresApi(Build.VERSION_CODES.M)
public static SealedData seal(@NonNull byte[] input) {
SecretKey secretKey = getOrCreateKeyStoreEntry();
try {
Cipher cipher = Cipher.getInstance("AES/GCM/NoPadding");
cipher.init(Cipher.ENCRYPT_MODE, secretKey);
byte[] iv = cipher.getIV();
byte[] data = cipher.doFinal(input);
return new SealedData(iv, data);
} catch (NoSuchAlgorithmException | NoSuchPaddingException | InvalidKeyException | IllegalBlockSizeException | BadPaddingException e) {
throw new AssertionError(e);
}
}
Aggregations