use of javax.crypto.IllegalBlockSizeException in project platform_frameworks_base by android.
the class LockSettingsService method setLockPasswordInternal.
private void setLockPasswordInternal(String password, String savedCredential, int userId) throws RemoteException {
byte[] currentHandle = getCurrentHandle(userId);
if (password == null) {
clearUserKeyProtection(userId);
getGateKeeperService().clearSecureUserId(userId);
mStorage.writePasswordHash(null, userId);
setKeystorePassword(null, userId);
fixateNewestUserKeyAuth(userId);
onUserLockChanged(userId);
return;
}
if (isManagedProfileWithUnifiedLock(userId)) {
// get credential from keystore when managed profile has unified lock
try {
savedCredential = getDecryptedPasswordForTiedProfile(userId);
} catch (FileNotFoundException e) {
Slog.i(TAG, "Child profile key not found");
} catch (UnrecoverableKeyException | InvalidKeyException | KeyStoreException | NoSuchAlgorithmException | NoSuchPaddingException | InvalidAlgorithmParameterException | IllegalBlockSizeException | BadPaddingException | CertificateException | IOException e) {
Slog.e(TAG, "Failed to decrypt child profile key", e);
}
} else {
if (currentHandle == null) {
if (savedCredential != null) {
Slog.w(TAG, "Saved credential provided, but none stored");
}
savedCredential = null;
}
}
byte[] enrolledHandle = enrollCredential(currentHandle, savedCredential, password, userId);
if (enrolledHandle != null) {
CredentialHash willStore = new CredentialHash(enrolledHandle, CredentialHash.VERSION_GATEKEEPER);
setUserKeyProtection(userId, password, doVerifyPassword(password, willStore, true, 0, userId, null));
mStorage.writePasswordHash(enrolledHandle, userId);
fixateNewestUserKeyAuth(userId);
onUserLockChanged(userId);
} else {
throw new RemoteException("Failed to enroll password");
}
}
use of javax.crypto.IllegalBlockSizeException in project platform_frameworks_base by android.
the class LockSettingsService method setLockPatternInternal.
private void setLockPatternInternal(String pattern, String savedCredential, int userId) throws RemoteException {
byte[] currentHandle = getCurrentHandle(userId);
if (pattern == null) {
clearUserKeyProtection(userId);
getGateKeeperService().clearSecureUserId(userId);
mStorage.writePatternHash(null, userId);
setKeystorePassword(null, userId);
fixateNewestUserKeyAuth(userId);
onUserLockChanged(userId);
return;
}
if (isManagedProfileWithUnifiedLock(userId)) {
// get credential from keystore when managed profile has unified lock
try {
savedCredential = getDecryptedPasswordForTiedProfile(userId);
} catch (UnrecoverableKeyException | InvalidKeyException | KeyStoreException | NoSuchAlgorithmException | NoSuchPaddingException | InvalidAlgorithmParameterException | IllegalBlockSizeException | BadPaddingException | CertificateException | IOException e) {
if (e instanceof FileNotFoundException) {
Slog.i(TAG, "Child profile key not found");
} else {
Slog.e(TAG, "Failed to decrypt child profile key", e);
}
}
} else {
if (currentHandle == null) {
if (savedCredential != null) {
Slog.w(TAG, "Saved credential provided, but none stored");
}
savedCredential = null;
}
}
byte[] enrolledHandle = enrollCredential(currentHandle, savedCredential, pattern, userId);
if (enrolledHandle != null) {
CredentialHash willStore = new CredentialHash(enrolledHandle, CredentialHash.VERSION_GATEKEEPER);
setUserKeyProtection(userId, pattern, doVerifyPattern(pattern, willStore, true, 0, userId, null));
mStorage.writePatternHash(enrolledHandle, userId);
fixateNewestUserKeyAuth(userId);
onUserLockChanged(userId);
} else {
throw new RemoteException("Failed to enroll pattern");
}
}
use of javax.crypto.IllegalBlockSizeException in project otter by alibaba.
the class AESUtils method encrypt.
/**
* 加密byte数据
*
* @param plainData
* @return
* @throws AESException
*/
public byte[] encrypt(byte[] plainData) throws AESException {
try {
SecretKeySpec skeySpec = new SecretKeySpec(secretKey, ENCRYPTION_ALGORITHM);
// Instantiate the cipher
Cipher cipher = Cipher.getInstance(ENCRYPTION_ALGORITHM);
cipher.init(Cipher.ENCRYPT_MODE, skeySpec);
return cipher.doFinal(plainData);
} catch (NoSuchAlgorithmException e) {
throw new AESException(e);
} catch (NoSuchPaddingException e) {
throw new AESException(e);
} catch (InvalidKeyException e) {
throw new AESException(e);
} catch (IllegalBlockSizeException e) {
throw new AESException(e);
} catch (BadPaddingException e) {
throw new AESException(e);
}
}
use of javax.crypto.IllegalBlockSizeException in project cassandra by apache.
the class EncryptionUtils method encryptAndWrite.
/**
* Encrypt the input data, and writes out to the same input buffer; if the buffer is not big enough,
* deallocate current, and allocate a large enough buffer.
* Writes the cipher text and headers out to the channel, as well.
*
* Note: channel is a parameter as we cannot write header info to the output buffer as we assume the input and output
* buffers can be the same buffer (and writing the headers to a shared buffer will corrupt any input data). Hence,
* we write out the headers directly to the channel, and then the cipher text (once encrypted).
*/
public static ByteBuffer encryptAndWrite(ByteBuffer inputBuffer, WritableByteChannel channel, boolean allowBufferResize, Cipher cipher) throws IOException {
final int plainTextLength = inputBuffer.remaining();
final int encryptLength = cipher.getOutputSize(plainTextLength);
ByteBuffer outputBuffer = inputBuffer.duplicate();
outputBuffer = ByteBufferUtil.ensureCapacity(outputBuffer, encryptLength, allowBufferResize);
// it's unfortunate that we need to allocate a small buffer here just for the headers, but if we reuse the input buffer
// for the output, then we would overwrite the first n bytes of the real data with the header data.
ByteBuffer intBuf = ByteBuffer.allocate(ENCRYPTED_BLOCK_HEADER_SIZE);
intBuf.putInt(0, encryptLength);
intBuf.putInt(4, plainTextLength);
channel.write(intBuf);
try {
cipher.doFinal(inputBuffer, outputBuffer);
} catch (ShortBufferException | IllegalBlockSizeException | BadPaddingException e) {
throw new IOException("failed to encrypt commit log block", e);
}
outputBuffer.position(0).limit(encryptLength);
channel.write(outputBuffer);
outputBuffer.position(0).limit(encryptLength);
return outputBuffer;
}
use of javax.crypto.IllegalBlockSizeException in project keywhiz by square.
the class ContentCryptographer method gcm.
private byte[] gcm(Mode mode, String info, byte[] nonce, byte[] data) {
try {
Cipher cipher = Cipher.getInstance(ENCRYPTION_ALGORITHM, encryptionProvider);
SecretKey derivedKey = deriveKey(cipher.getBlockSize(), info);
GCMParameterSpec gcmParameters = new GCMParameterSpec(TAG_BITS, nonce);
cipher.init(mode.cipherMode, derivedKey, gcmParameters);
return cipher.doFinal(data);
} catch (IllegalBlockSizeException | InvalidAlgorithmParameterException | NoSuchPaddingException | NoSuchAlgorithmException | InvalidKeyException | BadPaddingException e) {
throw Throwables.propagate(e);
}
}
Aggregations