use of java.security.spec.InvalidParameterSpecException in project j2objc by google.
the class AlgorithmParameters method init.
/**
* Initializes this parameter object using the parameters
* specified in {@code paramSpec}.
*
* @param paramSpec the parameter specification.
*
* @exception InvalidParameterSpecException if the given parameter
* specification is inappropriate for the initialization of this parameter
* object, or if this parameter object has already been initialized.
*/
public final void init(AlgorithmParameterSpec paramSpec) throws InvalidParameterSpecException {
if (this.initialized)
throw new InvalidParameterSpecException("already initialized");
paramSpi.engineInit(paramSpec);
this.initialized = true;
}
use of java.security.spec.InvalidParameterSpecException in project Payara by payara.
the class HazelcastSymmetricEncryptor method encode.
public static byte[] encode(byte[] value) {
// Generate IV.
byte[] saltBytes = new byte[20];
random.nextBytes(saltBytes);
try {
// Encrypting the data
Cipher cipher = Cipher.getInstance(AES_ALGORITHM);
cipher.init(Cipher.ENCRYPT_MODE, secretKey);
AlgorithmParameters params = cipher.getParameters();
byte[] ivBytes = params.getParameterSpec(IvParameterSpec.class).getIV();
byte[] encryptedTextBytes = cipher.doFinal(value);
// Prepend salt and IV
byte[] buffer = new byte[saltBytes.length + ivBytes.length + encryptedTextBytes.length];
System.arraycopy(saltBytes, 0, buffer, 0, saltBytes.length);
System.arraycopy(ivBytes, 0, buffer, saltBytes.length, ivBytes.length);
System.arraycopy(encryptedTextBytes, 0, buffer, saltBytes.length + ivBytes.length, encryptedTextBytes.length);
return buffer;
} catch (NoSuchAlgorithmException | InvalidKeyException | NoSuchPaddingException | IllegalBlockSizeException | InvalidParameterSpecException | BadPaddingException exception) {
throw new HazelcastException(exception);
}
}
use of java.security.spec.InvalidParameterSpecException in project Payara by payara.
the class GenerateEncryptionKey method generateAndEncryptKey.
private byte[] generateAndEncryptKey(char[] masterpasswordChars) throws CommandException {
byte[] saltBytes = new byte[20];
random.nextBytes(saltBytes);
try {
// Derive the key
SecretKeyFactory factory = SecretKeyFactory.getInstance(PBKDF_ALGORITHM);
PBEKeySpec spec = new PBEKeySpec(masterpasswordChars, saltBytes, ITERATION_COUNT, KEYSIZE);
SecretKeySpec secret = new SecretKeySpec(factory.generateSecret(spec).getEncoded(), AES);
// Encrypting the data
Cipher cipher = Cipher.getInstance(AES_ALGORITHM);
cipher.init(Cipher.ENCRYPT_MODE, secret);
AlgorithmParameters params = cipher.getParameters();
byte[] ivBytes = params.getParameterSpec(IvParameterSpec.class).getIV();
// Key length is in bits !
byte[] keyBytes = new byte[KEYSIZE / 8];
random.nextBytes(keyBytes);
byte[] encryptedTextBytes = cipher.doFinal(keyBytes);
// Prepend salt and IV
byte[] buffer = new byte[saltBytes.length + ivBytes.length + encryptedTextBytes.length];
System.arraycopy(saltBytes, 0, buffer, 0, saltBytes.length);
System.arraycopy(ivBytes, 0, buffer, saltBytes.length, ivBytes.length);
System.arraycopy(encryptedTextBytes, 0, buffer, saltBytes.length + ivBytes.length, encryptedTextBytes.length);
return buffer;
} catch (InvalidKeySpecException | NoSuchAlgorithmException | BadPaddingException | InvalidKeyException | NoSuchPaddingException | InvalidParameterSpecException | IllegalBlockSizeException exception) {
throw new CommandException(exception.getMessage(), exception);
}
}
use of java.security.spec.InvalidParameterSpecException in project perun by CESNET.
the class urn_perun_user_attribute_def_def_sshPublicKey method getECParameterSpec.
/**
* Gets the curve parameters for the given key type identifier.
*
* @param identifier According to RFC 5656:
* "The string [identifier] is the identifier of the elliptic curve domain parameters."
* @return An ECParameterSpec suitable for creating a JCE ECPublicKeySpec.
*/
private ECParameterSpec getECParameterSpec(String identifier) {
try {
// http://www.bouncycastle.org/wiki/pages/viewpage.action?pageId=362269#SupportedCurves(ECDSAandECGOST)-NIST(aliasesforSECcurves)
String name = identifier.replace("nist", "sec") + "r1";
AlgorithmParameters parameters = AlgorithmParameters.getInstance("EC");
parameters.init(new ECGenParameterSpec(name));
return parameters.getParameterSpec(ECParameterSpec.class);
} catch (InvalidParameterSpecException | NoSuchAlgorithmException e) {
throw new IllegalArgumentException("Unable to parse curve parameters: ", e);
}
}
use of java.security.spec.InvalidParameterSpecException in project druid by druid-io.
the class CryptoService method encrypt.
public byte[] encrypt(byte[] plain) {
try {
byte[] salt = new byte[saltSize];
SECURE_RANDOM_INSTANCE.nextBytes(salt);
SecretKey tmp = getKeyFromPassword(passPhrase, salt);
SecretKey secret = new SecretKeySpec(tmp.getEncoded(), cipherAlgName);
// error-prone warns if the transformation is not a compile-time constant
// since it cannot check it for insecure combinations.
@SuppressWarnings("InsecureCryptoUsage") Cipher ecipher = Cipher.getInstance(transformation);
ecipher.init(Cipher.ENCRYPT_MODE, secret);
return new EncryptedData(salt, ecipher.getParameters().getParameterSpec(IvParameterSpec.class).getIV(), ecipher.doFinal(plain)).toByteAray();
} catch (InvalidKeySpecException | NoSuchAlgorithmException | NoSuchPaddingException | InvalidKeyException | InvalidParameterSpecException | IllegalBlockSizeException | BadPaddingException ex) {
throw new RuntimeException(ex);
}
}
Aggregations