use of javax.crypto.BadPaddingException in project XobotOS by xamarin.
the class JCEBlockCipher method engineDoFinal.
protected int engineDoFinal(byte[] input, int inputOffset, int inputLen, byte[] output, int outputOffset) throws IllegalBlockSizeException, BadPaddingException, ShortBufferException {
// BEGIN android-note
// added ShortBufferException to the throws statement
// END android-note
int len = 0;
// BEGIN android-added
int outputLen = cipher.getOutputSize(inputLen);
if (outputLen + outputOffset > output.length) {
throw new ShortBufferException("need at least " + outputLen + " bytes");
}
if (inputLen != 0) {
len = cipher.processBytes(input, inputOffset, inputLen, output, outputOffset);
}
try {
return (len + cipher.doFinal(output, outputOffset + len));
} catch (DataLengthException e) {
throw new IllegalBlockSizeException(e.getMessage());
} catch (InvalidCipherTextException e) {
throw new BadPaddingException(e.getMessage());
}
}
use of javax.crypto.BadPaddingException in project XobotOS by xamarin.
the class JCERSACipher method engineDoFinal.
protected int engineDoFinal(byte[] input, int inputOffset, int inputLen, byte[] output, int outputOffset) throws IllegalBlockSizeException, BadPaddingException {
if (input != null) {
bOut.write(input, inputOffset, inputLen);
}
if (cipher instanceof RSABlindedEngine) {
if (bOut.size() > cipher.getInputBlockSize() + 1) {
throw new ArrayIndexOutOfBoundsException("too much data for RSA block");
}
} else {
if (bOut.size() > cipher.getInputBlockSize()) {
throw new ArrayIndexOutOfBoundsException("too much data for RSA block");
}
}
byte[] out;
try {
byte[] bytes = bOut.toByteArray();
bOut.reset();
out = cipher.processBlock(bytes, 0, bytes.length);
} catch (InvalidCipherTextException e) {
throw new BadPaddingException(e.getMessage());
}
for (int i = 0; i != out.length; i++) {
output[outputOffset + i] = out[i];
}
return out.length;
}
use of javax.crypto.BadPaddingException in project jdk8u_jdk by JetBrains.
the class TestNonexpanding method runTest.
public void runTest(String algo, String mo, String pad) throws Exception {
Cipher ci = null;
SecretKey key = null;
try {
// Initialization
Random rdm = new Random();
byte[] plainText = new byte[128];
rdm.nextBytes(plainText);
ci = Cipher.getInstance(algo + "/" + mo + "/" + pad, PROVIDER);
KeyGenerator kg = KeyGenerator.getInstance(algo, PROVIDER);
kg.init(KEY_LENGTH);
key = kg.generateKey();
// encrypt
ci.init(Cipher.ENCRYPT_MODE, key);
byte[] cipherText = new byte[ci.getOutputSize(plainText.length)];
int offset = ci.update(plainText, 0, plainText.length, cipherText, 0);
ci.doFinal(cipherText, offset);
// Comparison
if (!(plainText.length == cipherText.length)) {
// authentication tag and cipher text.
if (mo.equalsIgnoreCase("GCM")) {
GCMParameterSpec spec = ci.getParameters().getParameterSpec(GCMParameterSpec.class);
int cipherTextLength = cipherText.length - spec.getTLen() / 8;
if (plainText.length == cipherTextLength) {
return;
}
}
System.out.println("Original length: " + plainText.length);
System.out.println("Cipher text length: " + cipherText.length);
throw new RuntimeException("Test failed!");
}
} catch (NoSuchAlgorithmException e) {
//CFB7 and OFB150 are for negative testing
if (!mo.equalsIgnoreCase("CFB7") && !mo.equalsIgnoreCase("OFB150")) {
System.out.println("Unexpected NoSuchAlgorithmException with mode: " + mo);
throw new RuntimeException("Test failed!");
}
} catch (NoSuchProviderException | NoSuchPaddingException | InvalidKeyException | InvalidParameterSpecException | ShortBufferException | IllegalBlockSizeException | BadPaddingException e) {
System.out.println("Test failed!");
throw e;
}
}
use of javax.crypto.BadPaddingException in project android_frameworks_base by DirtyUnicorns.
the class LockSettingsService method verifyTiedProfileChallenge.
@Override
public VerifyCredentialResponse verifyTiedProfileChallenge(String password, boolean isPattern, long challenge, int userId) throws RemoteException {
checkPasswordReadPermission(userId);
if (!isManagedProfileWithUnifiedLock(userId)) {
throw new RemoteException("User id must be managed profile with unified lock");
}
final int parentProfileId = mUserManager.getProfileParent(userId).id;
// Unlock parent by using parent's challenge
final VerifyCredentialResponse parentResponse = isPattern ? doVerifyPattern(password, true, challenge, parentProfileId, null) : doVerifyPassword(password, true, challenge, parentProfileId, null);
if (parentResponse.getResponseCode() != VerifyCredentialResponse.RESPONSE_OK) {
// Failed, just return parent's response
return parentResponse;
}
try {
// Unlock work profile, and work profile with unified lock must use password only
return doVerifyPassword(getDecryptedPasswordForTiedProfile(userId), true, challenge, userId, null);
} catch (UnrecoverableKeyException | InvalidKeyException | KeyStoreException | NoSuchAlgorithmException | NoSuchPaddingException | InvalidAlgorithmParameterException | IllegalBlockSizeException | BadPaddingException | CertificateException | IOException e) {
Slog.e(TAG, "Failed to decrypt child profile key", e);
throw new RemoteException("Unable to get tied profile token");
}
}
use of javax.crypto.BadPaddingException in project intellij-community by JetBrains.
the class CertificateManager method getDefaultKeyManagers.
/**
* Workaround for IDEA-124057. Manually find key store specified via VM options.
*
* @return key managers or {@code null} in case of any error
*/
@Nullable
public static KeyManager[] getDefaultKeyManagers() {
String keyStorePath = System.getProperty("javax.net.ssl.keyStore");
if (keyStorePath != null) {
LOG.info("Loading custom key store specified with VM options: " + keyStorePath);
try {
KeyManagerFactory factory = KeyManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm());
KeyStore keyStore;
String keyStoreType = System.getProperty("javax.net.ssl.keyStoreType", KeyStore.getDefaultType());
try {
keyStore = KeyStore.getInstance(keyStoreType);
} catch (KeyStoreException e) {
if (e.getCause() instanceof NoSuchAlgorithmException) {
LOG.error("Wrong key store type: " + keyStoreType, e);
return null;
}
throw e;
}
String password = System.getProperty("javax.net.ssl.keyStorePassword", "");
InputStream inputStream = null;
try {
inputStream = new FileInputStream(keyStorePath);
keyStore.load(inputStream, password.toCharArray());
factory.init(keyStore, password.toCharArray());
} catch (FileNotFoundException e) {
LOG.error("Key store file not found: " + keyStorePath);
return null;
} catch (Exception e) {
if (e.getCause() instanceof BadPaddingException) {
LOG.error("Wrong key store password: " + password, e);
return null;
}
throw e;
} finally {
StreamUtil.closeStream(inputStream);
}
return factory.getKeyManagers();
} catch (Exception e) {
LOG.error(e);
}
}
return null;
}
Aggregations