use of javax.crypto.NoSuchPaddingException in project core-ng-project by neowu.
the class RSA method decrypt.
public byte[] decrypt(byte[] encryptedMessage) {
try {
Cipher cipher = Cipher.getInstance(ALGORITHM_RSA);
cipher.init(Cipher.DECRYPT_MODE, privateKey);
return cipher.doFinal(encryptedMessage);
} catch (NoSuchAlgorithmException | NoSuchPaddingException | IllegalBlockSizeException | BadPaddingException e) {
throw new Error("failed to decrypt message, please check private key and message", e);
} catch (InvalidKeyException e) {
throw new Error(e);
}
}
use of javax.crypto.NoSuchPaddingException in project android-fingerprint-authentication by multidots.
the class FingerPrintAuthHelper method cipherInit.
/**
* Initialize the cipher.
*
* @return true if the initialization is successful.
*/
@TargetApi(23)
private boolean cipherInit() {
boolean isKeyGenerated = generateKey();
if (!isKeyGenerated) {
mCallback.onAuthFailed(AuthErrorCodes.NON_RECOVERABLE_ERROR, ERROR_FAILED_TO_GENERATE_KEY);
return false;
}
try {
mCipher = Cipher.getInstance(KeyProperties.KEY_ALGORITHM_AES + "/" + KeyProperties.BLOCK_MODE_CBC + "/" + KeyProperties.ENCRYPTION_PADDING_PKCS7);
} catch (NoSuchAlgorithmException | NoSuchPaddingException e) {
mCallback.onAuthFailed(AuthErrorCodes.NON_RECOVERABLE_ERROR, ERROR_FAILED_TO_GENERATE_KEY);
return false;
}
try {
mKeyStore.load(null);
SecretKey key = (SecretKey) mKeyStore.getKey(KEY_NAME, null);
mCipher.init(Cipher.ENCRYPT_MODE, key);
return true;
} catch (KeyPermanentlyInvalidatedException e) {
mCallback.onAuthFailed(AuthErrorCodes.NON_RECOVERABLE_ERROR, ERROR_FAILED_TO_INIT_CHIPPER);
return false;
} catch (KeyStoreException | CertificateException | UnrecoverableKeyException | IOException | NoSuchAlgorithmException | InvalidKeyException e) {
mCallback.onAuthFailed(AuthErrorCodes.NON_RECOVERABLE_ERROR, ERROR_FAILED_TO_INIT_CHIPPER);
return false;
}
}
use of javax.crypto.NoSuchPaddingException in project ctSESAM-android by pinae.
the class Crypter method encrypt.
public byte[] encrypt(byte[] data, String padding) {
SecretKeySpec skeySpec = new SecretKeySpec(key, "AES");
try {
Cipher cipher = Cipher.getInstance("AES/CBC/" + padding);
cipher.init(Cipher.ENCRYPT_MODE, skeySpec, new IvParameterSpec(this.iv));
return cipher.doFinal(data);
} catch (NoSuchAlgorithmException e) {
Log.d("Encryption error", "AES/CBC is not implemented.");
e.printStackTrace();
} catch (NoSuchPaddingException e) {
Log.d("Encryption error", padding + " is not implemented.");
e.printStackTrace();
} catch (InvalidAlgorithmParameterException e) {
Log.d("Encryption error", "Invalid IV.");
e.printStackTrace();
} catch (InvalidKeyException e) {
Log.d("Encryption error", "Invalid key.");
e.printStackTrace();
} catch (IllegalBlockSizeException e) {
Log.d("Encryption error", "Illegal block size of the data.");
e.printStackTrace();
} catch (BadPaddingException e) {
Log.d("Encryption error", "Bad padding of the data.");
e.printStackTrace();
}
return new byte[] {};
}
use of javax.crypto.NoSuchPaddingException in project ballerina by ballerina-lang.
the class AESCipherTool method decrypt.
/**
* This method is used to decrypt a given encrypted and base64 encoded value.
*
* @param value Encrypted value to be decrypted.
* @return Decrypted value.
*/
public String decrypt(String value) throws AESCipherToolException {
try {
byte[] decodedByteArray = decodeBase64(value);
IvParameterSpec ivParameterSpec = new IvParameterSpec(Arrays.copyOfRange(decodedByteArray, 0, IV_SIZE));
Cipher decryptionCipher = Cipher.getInstance(ALGORITHM_AES_CBC_PKCS5);
decryptionCipher.init(Cipher.DECRYPT_MODE, secretKey, ivParameterSpec);
byte[] encryptedByteArray = Arrays.copyOfRange(decodedByteArray, IV_SIZE, decodedByteArray.length);
return new String(decryptionCipher.doFinal(encryptedByteArray), StandardCharsets.UTF_8);
} catch (BadPaddingException | IllegalBlockSizeException | NoSuchPaddingException | NoSuchAlgorithmException | InvalidAlgorithmParameterException | InvalidKeyException err) {
log.error("Failed to decrypt value: " + value, err);
throw new AESCipherToolException(err.getMessage(), err);
}
}
use of javax.crypto.NoSuchPaddingException in project ballerina by ballerina-lang.
the class AESCipherTool method encrypt.
/**
* This method is used to encrypt and base64 encode a plain value.
*
* @param value Value to be encrypted.
* @return Encrypted value.
*/
public String encrypt(String value) throws AESCipherToolException {
try {
byte[] ivByteArray = getSecureRandomBytes();
IvParameterSpec ivParameterSpec = new IvParameterSpec(ivByteArray);
Cipher encryptionCipher = Cipher.getInstance(ALGORITHM_AES_CBC_PKCS5);
encryptionCipher.init(Cipher.ENCRYPT_MODE, secretKey, ivParameterSpec);
byte[] encryptedBytes = encryptionCipher.doFinal(getBytes(value));
return encodeBase64(appendByteArrays(ivByteArray, encryptedBytes));
} catch (BadPaddingException | IllegalBlockSizeException | NoSuchPaddingException | NoSuchAlgorithmException | InvalidAlgorithmParameterException | InvalidKeyException err) {
log.error("Failed to encrypt value: " + value, err);
throw new AESCipherToolException(err.getMessage(), err);
}
}
Aggregations