use of java.security.spec.KeySpec in project audit4j-core by audit4j.
the class EncryptionUtil method generateKeyIfNotAvailable.
/**
* Generate key.
*
* @param key
* the key
* @param salt
* the salt
* @return the key
* @throws NoSuchAlgorithmException
* the no such algorithm exception
* @throws UnsupportedEncodingException
* the unsupported encoding exception
* @throws InvalidKeySpecException
* the invalid key spec exception
*/
private static void generateKeyIfNotAvailable(String key, String salt) throws NoSuchAlgorithmException, UnsupportedEncodingException, InvalidKeySpecException {
if (null == cryptKey) {
LOG.debug("Generating Key...");
SecretKeyFactory factory = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA1");
char[] password = key.toCharArray();
byte[] saltBytes = salt.getBytes(CoreConstants.ENCODE_UTF8);
KeySpec spec = new PBEKeySpec(password, saltBytes, 65536, 128);
SecretKey tmp = factory.generateSecret(spec);
byte[] encoded = tmp.getEncoded();
cryptKey = new SecretKeySpec(encoded, "AES");
}
}
use of java.security.spec.KeySpec in project smoke by textbrowser.
the class Cryptography method generateMacKey.
public static SecretKey generateMacKey(byte[] salt, char[] password, int iterations) throws InvalidKeySpecException, NoSuchAlgorithmException {
// Bits.
final int length = 512;
KeySpec keySpec = new PBEKeySpec(password, salt, iterations, length);
SecretKeyFactory secretKeyFactory = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA1");
return secretKeyFactory.generateSecret(keySpec);
}
use of java.security.spec.KeySpec in project smoke by textbrowser.
the class Cryptography method pbkdf2.
public static byte[] pbkdf2(byte[] salt, char[] password, int iterations, int length) {
try {
KeySpec keySpec = new PBEKeySpec(password, salt, iterations, length);
SecretKeyFactory secretKeyFactory = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA1");
return secretKeyFactory.generateSecret(keySpec).getEncoded();
} catch (Exception exception) {
return null;
}
}
use of java.security.spec.KeySpec in project fdroidclient by f-droid.
the class ZipSigner method readPrivateKey.
/**
* Read a PKCS 8 format private key.
*/
public PrivateKey readPrivateKey(URL privateKeyUrl, String keyPassword) throws IOException, GeneralSecurityException {
DataInputStream input = new DataInputStream(privateKeyUrl.openStream());
try {
byte[] bytes = readContentAsBytes(input);
KeySpec spec = decryptPrivateKey(bytes, keyPassword);
if (spec == null) {
spec = new PKCS8EncodedKeySpec(bytes);
}
try {
return KeyFactory.getInstance("RSA").generatePrivate(spec);
} catch (InvalidKeySpecException ex) {
return KeyFactory.getInstance("DSA").generatePrivate(spec);
}
} finally {
input.close();
}
}
use of java.security.spec.KeySpec in project wicket by apache.
the class SunJceCrypt method generateSecretKey.
/**
* Generate the de-/encryption key.
* <p>
* Note: if you don't provide your own encryption key, the implementation will use a default. Be
* aware that this is potential security risk. Thus make sure you always provide your own one.
*
* @return secretKey the security key generated
* @throws NoSuchAlgorithmException
* unable to find encryption algorithm specified
* @throws InvalidKeySpecException
* invalid encryption key
*/
protected SecretKey generateSecretKey() throws NoSuchAlgorithmException, InvalidKeySpecException {
SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(cryptMethod);
KeySpec spec = createKeySpec();
return keyFactory.generateSecret(spec);
}
Aggregations