use of java.security.spec.KeySpec 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.KeySpec 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);
}
}
use of java.security.spec.KeySpec in project XobotOS by xamarin.
the class JDKKeyStore method decodeKey.
private Key decodeKey(DataInputStream dIn) throws IOException {
int keyType = dIn.read();
String format = dIn.readUTF();
String algorithm = dIn.readUTF();
byte[] enc = new byte[dIn.readInt()];
KeySpec spec;
dIn.readFully(enc);
if (format.equals("PKCS#8") || format.equals("PKCS8")) {
spec = new PKCS8EncodedKeySpec(enc);
} else if (format.equals("X.509") || format.equals("X509")) {
spec = new X509EncodedKeySpec(enc);
} else if (format.equals("RAW")) {
return new SecretKeySpec(enc, algorithm);
} else {
throw new IOException("Key format " + format + " not recognised!");
}
try {
switch(keyType) {
case KEY_PRIVATE:
return KeyFactory.getInstance(algorithm, BouncyCastleProvider.PROVIDER_NAME).generatePrivate(spec);
case KEY_PUBLIC:
return KeyFactory.getInstance(algorithm, BouncyCastleProvider.PROVIDER_NAME).generatePublic(spec);
case KEY_SECRET:
return SecretKeyFactory.getInstance(algorithm, BouncyCastleProvider.PROVIDER_NAME).generateSecret(spec);
default:
throw new IOException("Key type " + keyType + " not recognised!");
}
} catch (Exception e) {
throw new IOException("Exception creating key: " + e.toString());
}
}
use of java.security.spec.KeySpec in project CloudStack-archive by CloudStack-extras.
the class RSAHelper method readKey.
private static RSAPublicKey readKey(String key) throws Exception {
byte[] encKey = Base64.decodeBase64(key.split(" ")[1]);
DataInputStream dis = new DataInputStream(new ByteArrayInputStream(encKey));
byte[] header = readElement(dis);
String pubKeyFormat = new String(header);
if (!pubKeyFormat.equals("ssh-rsa"))
throw new RuntimeException("Unsupported format");
byte[] publicExponent = readElement(dis);
byte[] modulus = readElement(dis);
KeySpec spec = new RSAPublicKeySpec(new BigInteger(modulus), new BigInteger(publicExponent));
KeyFactory keyFactory = KeyFactory.getInstance("RSA", BouncyCastleProvider.PROVIDER_NAME);
RSAPublicKey pubKey = (RSAPublicKey) keyFactory.generatePublic(spec);
return pubKey;
}
use of java.security.spec.KeySpec in project jdk8u_jdk by JetBrains.
the class GenerationTests method getPublicKey.
private static PublicKey getPublicKey(String algo, int keysize) throws Exception {
KeyFactory kf = KeyFactory.getInstance(algo);
KeySpec kspec;
if (algo.equalsIgnoreCase("DSA")) {
if (keysize == 1024) {
kspec = new DSAPublicKeySpec(new BigInteger(DSA_Y), new BigInteger(DSA_P), new BigInteger(DSA_Q), new BigInteger(DSA_G));
} else if (keysize == 2048) {
kspec = new DSAPublicKeySpec(new BigInteger(DSA_2048_Y), new BigInteger(DSA_2048_P), new BigInteger(DSA_2048_Q), new BigInteger(DSA_2048_G));
} else
throw new RuntimeException("Unsupported keysize:" + keysize);
} else if (algo.equalsIgnoreCase("RSA")) {
if (keysize == 512) {
kspec = new RSAPublicKeySpec(new BigInteger(RSA_MOD), new BigInteger(RSA_PUB));
} else if (keysize == 1024) {
kspec = new RSAPublicKeySpec(new BigInteger(RSA_1024_MOD), new BigInteger(RSA_PUB));
} else
throw new RuntimeException("Unsupported keysize:" + keysize);
} else
throw new RuntimeException("Unsupported key algorithm " + algo);
return kf.generatePublic(kspec);
}
Aggregations