use of java.security.spec.InvalidKeySpecException in project hbase by apache.
the class Encryption method pbkdf128.
/**
* Return a 128 bit key derived from the concatenation of the supplied
* arguments using PBKDF2WithHmacSHA1 at 10,000 iterations.
*
*/
public static byte[] pbkdf128(String... args) {
byte[] salt = new byte[128];
Bytes.random(salt);
StringBuilder sb = new StringBuilder();
for (String s : args) {
sb.append(s);
}
PBEKeySpec spec = new PBEKeySpec(sb.toString().toCharArray(), salt, 10000, 128);
try {
return SecretKeyFactory.getInstance("PBKDF2WithHmacSHA1").generateSecret(spec).getEncoded();
} catch (NoSuchAlgorithmException e) {
throw new RuntimeException(e);
} catch (InvalidKeySpecException e) {
throw new RuntimeException(e);
}
}
use of java.security.spec.InvalidKeySpecException in project android_frameworks_base by ParanoidAndroid.
the class BackupManagerService method buildCharArrayKey.
private SecretKey buildCharArrayKey(char[] pwArray, byte[] salt, int rounds) {
try {
SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA1");
KeySpec ks = new PBEKeySpec(pwArray, salt, rounds, PBKDF2_KEY_SIZE);
return keyFactory.generateSecret(ks);
} catch (InvalidKeySpecException e) {
Slog.e(TAG, "Invalid key spec for PBKDF2!");
} catch (NoSuchAlgorithmException e) {
Slog.e(TAG, "PBKDF2 unavailable!");
}
return null;
}
use of java.security.spec.InvalidKeySpecException in project android_frameworks_base by ParanoidAndroid.
the class PackageParser method parseVerifier.
private static VerifierInfo parseVerifier(Resources res, XmlPullParser parser, AttributeSet attrs, int flags, String[] outError) throws XmlPullParserException, IOException {
final TypedArray sa = res.obtainAttributes(attrs, com.android.internal.R.styleable.AndroidManifestPackageVerifier);
final String packageName = sa.getNonResourceString(com.android.internal.R.styleable.AndroidManifestPackageVerifier_name);
final String encodedPublicKey = sa.getNonResourceString(com.android.internal.R.styleable.AndroidManifestPackageVerifier_publicKey);
sa.recycle();
if (packageName == null || packageName.length() == 0) {
Slog.i(TAG, "verifier package name was null; skipping");
return null;
} else if (encodedPublicKey == null) {
Slog.i(TAG, "verifier " + packageName + " public key was null; skipping");
}
EncodedKeySpec keySpec;
try {
final byte[] encoded = Base64.decode(encodedPublicKey, Base64.DEFAULT);
keySpec = new X509EncodedKeySpec(encoded);
} catch (IllegalArgumentException e) {
Slog.i(TAG, "Could not parse verifier " + packageName + " public key; invalid Base64");
return null;
}
/* First try the key as an RSA key. */
try {
final KeyFactory keyFactory = KeyFactory.getInstance("RSA");
final PublicKey publicKey = keyFactory.generatePublic(keySpec);
return new VerifierInfo(packageName, publicKey);
} catch (NoSuchAlgorithmException e) {
Log.wtf(TAG, "Could not parse public key because RSA isn't included in build");
return null;
} catch (InvalidKeySpecException e) {
// Not a RSA public key.
}
/* Now try it as a DSA key. */
try {
final KeyFactory keyFactory = KeyFactory.getInstance("DSA");
final PublicKey publicKey = keyFactory.generatePublic(keySpec);
return new VerifierInfo(packageName, publicKey);
} catch (NoSuchAlgorithmException e) {
Log.wtf(TAG, "Could not parse public key because DSA isn't included in build");
return null;
} catch (InvalidKeySpecException e) {
// Not a DSA public key.
}
return null;
}
use of java.security.spec.InvalidKeySpecException in project jersey by jersey.
the class RsaSha1Method method sign.
/**
* Generates the RSA-SHA1 signature of OAuth request elements.
*
* @param baseString the combined OAuth elements to sign.
* @param secrets the secrets object containing the private key for generating the signature.
* @return the OAuth signature, in base64-encoded form.
* @throws InvalidSecretException if the supplied secret is not valid.
*/
@Override
public String sign(final String baseString, final OAuth1Secrets secrets) throws InvalidSecretException {
final Signature signature;
try {
signature = Signature.getInstance(SIGNATURE_ALGORITHM);
} catch (final NoSuchAlgorithmException nsae) {
throw new IllegalStateException(nsae);
}
byte[] decodedPrivateKey;
try {
decodedPrivateKey = Base64.decode(secrets.getConsumerSecret());
} catch (final IOException ioe) {
throw new InvalidSecretException(LocalizationMessages.ERROR_INVALID_CONSUMER_SECRET(ioe));
}
final KeyFactory keyFactory;
try {
keyFactory = KeyFactory.getInstance(KEY_TYPE);
} catch (final NoSuchAlgorithmException nsae) {
throw new IllegalStateException(nsae);
}
final EncodedKeySpec keySpec = new PKCS8EncodedKeySpec(decodedPrivateKey);
final RSAPrivateKey rsaPrivateKey;
try {
rsaPrivateKey = (RSAPrivateKey) keyFactory.generatePrivate(keySpec);
} catch (final InvalidKeySpecException ikse) {
throw new IllegalStateException(ikse);
}
try {
signature.initSign(rsaPrivateKey);
} catch (final InvalidKeyException ike) {
throw new IllegalStateException(ike);
}
try {
signature.update(baseString.getBytes());
} catch (final SignatureException se) {
throw new IllegalStateException(se);
}
final byte[] rsasha1;
try {
rsasha1 = signature.sign();
} catch (final SignatureException se) {
throw new IllegalStateException(se);
}
return Base64.encode(rsasha1);
}
use of java.security.spec.InvalidKeySpecException in project che by eclipse.
the class PBKDF2PasswordEncryptor method computeHash.
private HashCode computeHash(char[] password, byte[] salt, int iterations) {
try {
final SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(SECRET_KEY_FACTORY_NAME);
final KeySpec keySpec = new PBEKeySpec(password, salt, iterations, 512);
return HashCode.fromBytes(keyFactory.generateSecret(keySpec).getEncoded());
} catch (NoSuchAlgorithmException | InvalidKeySpecException x) {
throw new RuntimeException(x.getMessage(), x);
}
}
Aggregations