use of javax.crypto.NoSuchPaddingException in project triplea by triplea-game.
the class RsaAuthenticator method decryptPasswordForAction.
/**
* Decrypts the password contained in the specified response and provides it to the specified action for further
* processing.
*
* @param response The response map containing the encrypted password.
* @param action A {@link Function} which is executed if the password is successfully decrypted.
*
* @return The result of {@code action} if the password is decrypted successfully; otherwise a message describing the
* error that occurred during decryption.
*
* @throws IllegalStateException If the encryption cipher is not available.
*/
String decryptPasswordForAction(final Map<String, String> response, final Function<String, String> action) {
final String encryptedPassword = response.get(ENCRYPTED_PASSWORD_KEY);
try {
final Cipher cipher = Cipher.getInstance(RSA_ECB_OAEPP);
cipher.init(Cipher.DECRYPT_MODE, keyPair.getPrivate());
return action.apply(new String(cipher.doFinal(Base64.getDecoder().decode(encryptedPassword)), StandardCharsets.UTF_8));
} catch (final NoSuchAlgorithmException | NoSuchPaddingException e) {
throw new IllegalStateException(e);
} catch (final InvalidKeyException | IllegalBlockSizeException | BadPaddingException e) {
return e.getMessage();
}
}
use of javax.crypto.NoSuchPaddingException in project incubator-pulsar by apache.
the class MessageCrypto method addPublicKeyCipher.
private void addPublicKeyCipher(String keyName, CryptoKeyReader keyReader) throws CryptoException {
if (keyName == null || keyReader == null) {
throw new PulsarClientException.CryptoException("Keyname or KeyReader is null");
}
// Read the public key and its info using callback
EncryptionKeyInfo keyInfo = keyReader.getPublicKey(keyName, null);
PublicKey pubKey;
try {
pubKey = loadPublicKey(keyInfo.getKey());
} catch (Exception e) {
String msg = logCtx + "Failed to load public key " + keyName + ". " + e.getMessage();
log.error(msg);
throw new PulsarClientException.CryptoException(msg);
}
Cipher dataKeyCipher = null;
byte[] encryptedKey;
try {
// Encrypt data key using public key
if (RSA.equals(pubKey.getAlgorithm())) {
dataKeyCipher = Cipher.getInstance(RSA_TRANS, BouncyCastleProvider.PROVIDER_NAME);
} else if (ECDSA.equals(pubKey.getAlgorithm())) {
dataKeyCipher = Cipher.getInstance(ECIES, BouncyCastleProvider.PROVIDER_NAME);
} else {
String msg = logCtx + "Unsupported key type " + pubKey.getAlgorithm() + " for key " + keyName;
log.error(msg);
throw new PulsarClientException.CryptoException(msg);
}
dataKeyCipher.init(Cipher.ENCRYPT_MODE, pubKey);
encryptedKey = dataKeyCipher.doFinal(dataKey.getEncoded());
} catch (IllegalBlockSizeException | BadPaddingException | NoSuchAlgorithmException | NoSuchProviderException | NoSuchPaddingException | InvalidKeyException e) {
log.error("{} Failed to encrypt data key {}. {}", logCtx, keyName, e.getMessage());
throw new PulsarClientException.CryptoException(e.getMessage());
}
EncryptionKeyInfo eki = new EncryptionKeyInfo(encryptedKey, keyInfo.getMetadata());
encryptedDataKeyMap.put(keyName, eki);
}
use of javax.crypto.NoSuchPaddingException in project api-framework by vinscom.
the class SecurityTools method encrypt.
public String encrypt(String value) {
try {
byte[] riv = new byte[16];
mRandom.nextBytes(riv);
IvParameterSpec iv = new IvParameterSpec(riv);
Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5PADDING");
cipher.init(Cipher.ENCRYPT_MODE, getKeySpec(), iv);
byte[] encrypted = cipher.doFinal(value.getBytes());
return Base64.getEncoder().encodeToString(concatenateByteArrays(encrypted, riv));
} catch (InvalidAlgorithmParameterException | InvalidKeyException | NoSuchAlgorithmException | NoSuchPaddingException | IllegalBlockSizeException | BadPaddingException | InterruptedException | ExecutionException ex) {
getLog().error(ex);
}
return null;
}
use of javax.crypto.NoSuchPaddingException in project api-framework by vinscom.
the class SecurityTools method decrypt.
public String decrypt(String encrypted) {
try {
byte[] data = Base64.getDecoder().decode(encrypted);
IvParameterSpec iv = new IvParameterSpec(data, data.length - 16, 16);
Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5PADDING");
cipher.init(Cipher.DECRYPT_MODE, getKeySpec(), iv);
byte[] original = cipher.doFinal(data, 0, data.length - 16);
return new String(original);
} catch (NoSuchAlgorithmException | NoSuchPaddingException | InvalidKeyException | InvalidAlgorithmParameterException | IllegalBlockSizeException | BadPaddingException | InterruptedException | ExecutionException ex) {
getLog().error(ex);
}
return null;
}
use of javax.crypto.NoSuchPaddingException in project cordova-plugin-android-fingerprint-auth by mjwheatley.
the class FingerprintAuth method initialize.
/**
* Sets the context of the Command. This can then be used to do things like
* get file paths associated with the Activity.
*
* @param cordova The context of the main Activity.
* @param webView The CordovaWebView Cordova is running in.
*/
public void initialize(CordovaInterface cordova, CordovaWebView webView) {
super.initialize(cordova, webView);
Log.v(TAG, "Init FingerprintAuth");
packageName = cordova.getActivity().getApplicationContext().getPackageName();
mPluginResult = new PluginResult(PluginResult.Status.NO_RESULT);
mActivity = cordova.getActivity();
mContext = cordova.getActivity().getApplicationContext();
if (android.os.Build.VERSION.SDK_INT < 23) {
return;
}
mKeyguardManager = cordova.getActivity().getSystemService(KeyguardManager.class);
mFingerPrintManager = cordova.getActivity().getApplicationContext().getSystemService(FingerprintManager.class);
try {
mKeyGenerator = KeyGenerator.getInstance(KeyProperties.KEY_ALGORITHM_AES, ANDROID_KEY_STORE);
mKeyStore = KeyStore.getInstance(ANDROID_KEY_STORE);
} catch (NoSuchAlgorithmException e) {
throw new RuntimeException("Failed to get an instance of KeyGenerator", e);
} catch (NoSuchProviderException e) {
throw new RuntimeException("Failed to get an instance of KeyGenerator", e);
} catch (KeyStoreException e) {
throw new RuntimeException("Failed to get an instance of KeyStore", e);
}
try {
mCipher = Cipher.getInstance(KeyProperties.KEY_ALGORITHM_AES + "/" + KeyProperties.BLOCK_MODE_CBC + "/" + KeyProperties.ENCRYPTION_PADDING_PKCS7);
} catch (NoSuchAlgorithmException e) {
throw new RuntimeException("Failed to get an instance of Cipher", e);
} catch (NoSuchPaddingException e) {
throw new RuntimeException("Failed to get an instance of Cipher", e);
}
}
Aggregations