use of java.security.spec.KeySpec in project felix by apache.
the class CryptoServiceSingleton method generateAESKey.
/**
* Generate the AES key from the salt and the private key.
*
* @param salt the salt (hexadecimal)
* @param privateKey the private key
* @return the generated key.
*/
private SecretKey generateAESKey(String privateKey, String salt) {
try {
byte[] raw = Hex.decodeHex(salt.toCharArray());
KeySpec spec = new PBEKeySpec(privateKey.toCharArray(), raw, iterationCount, keySize);
SecretKeyFactory factory = SecretKeyFactory.getInstance(PBKDF_2_WITH_HMAC_SHA_1);
return new SecretKeySpec(factory.generateSecret(spec).getEncoded(), AES_ECB_ALGORITHM);
} catch (DecoderException e) {
throw new IllegalStateException(e);
} catch (NoSuchAlgorithmException e) {
throw new IllegalStateException(e);
} catch (InvalidKeySpecException e) {
throw new IllegalStateException(e);
}
}
use of java.security.spec.KeySpec in project android by nextcloud.
the class EncryptionUtils method decryptPrivateKey.
/**
* Decrypt private key with symmetric AES encryption, GCM mode mode and no padding
*
* @param privateKey byte64 encoded string representation of private key, IV separated with "|"
* @param keyPhrase key used for encryption, e.g. 12 random words
* {@link EncryptionUtils#getRandomWords(int, Context)}
* @return decrypted string
*/
public static String decryptPrivateKey(String privateKey, String keyPhrase) throws NoSuchPaddingException, NoSuchAlgorithmException, NoSuchProviderException, InvalidKeyException, BadPaddingException, IllegalBlockSizeException, InvalidKeySpecException, InvalidAlgorithmParameterException {
// split up iv, salt
String[] strings = privateKey.split(ivDelimiter);
String realPrivateKey = strings[0];
byte[] iv = decodeStringToBase64Bytes(strings[1]);
byte[] salt = decodeStringToBase64Bytes(strings[2]);
Cipher cipher = Cipher.getInstance(AES_CIPHER);
SecretKeyFactory factory = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA1");
KeySpec spec = new PBEKeySpec(keyPhrase.toCharArray(), salt, iterationCount, keyStrength);
SecretKey tmp = factory.generateSecret(spec);
SecretKeySpec key = new SecretKeySpec(tmp.getEncoded(), AES);
cipher.init(Cipher.DECRYPT_MODE, key, new IvParameterSpec(iv));
byte[] bytes = decodeStringToBase64Bytes(realPrivateKey);
byte[] decrypted = cipher.doFinal(bytes);
String pemKey = decodeBase64BytesToString(decrypted);
return pemKey.replaceAll("\n", "").replace("-----BEGIN PRIVATE KEY-----", "").replace("-----END PRIVATE KEY-----", "");
}
use of java.security.spec.KeySpec in project pentaho-kettle by pentaho.
the class CertificateGenEncryptUtil method decodeTransmittedKey.
public static Key decodeTransmittedKey(byte[] sessionKey, byte[] transmittedKey, boolean privateKey) throws InvalidKeySpecException, NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException {
KeySpec keySpec = null;
Key keyKey = null;
if (transmittedKey == null || sessionKey == null) {
return null;
}
if (!privateKey) {
keySpec = new X509EncodedKeySpec(sessionKey);
keyKey = KeyFactory.getInstance(PUBLIC_KEY_ALGORITHM).generatePublic(keySpec);
} else {
keySpec = new PKCS8EncodedKeySpec(sessionKey);
keyKey = KeyFactory.getInstance(PUBLIC_KEY_ALGORITHM).generatePrivate(keySpec);
}
Cipher keyCipher = Cipher.getInstance(TRANSMISSION_CIPHER_PARAMS);
keyCipher.init(Cipher.UNWRAP_MODE, keyKey);
return keyCipher.unwrap(transmittedKey, SINGLE_KEY_ALGORITHM, Cipher.SECRET_KEY);
}
use of java.security.spec.KeySpec in project chuidiang-ejemplos by chuidiang.
the class RSAAsymetricCrypto method loadPrivateKey.
private static PrivateKey loadPrivateKey(String fileName) throws Exception {
FileInputStream fis = new FileInputStream(fileName);
int numBtyes = fis.available();
byte[] bytes = new byte[numBtyes];
fis.read(bytes);
fis.close();
KeyFactory keyFactory = KeyFactory.getInstance("RSA");
KeySpec keySpec = new PKCS8EncodedKeySpec(bytes);
PrivateKey keyFromBytes = keyFactory.generatePrivate(keySpec);
return keyFromBytes;
}
use of java.security.spec.KeySpec in project chuidiang-ejemplos by chuidiang.
the class RSAAsymetricCrypto method loadPublicKey.
private static PublicKey loadPublicKey(String fileName) throws Exception {
FileInputStream fis = new FileInputStream(fileName);
int numBtyes = fis.available();
byte[] bytes = new byte[numBtyes];
fis.read(bytes);
fis.close();
KeyFactory keyFactory = KeyFactory.getInstance("RSA");
KeySpec keySpec = new X509EncodedKeySpec(bytes);
PublicKey keyFromBytes = keyFactory.generatePublic(keySpec);
return keyFromBytes;
}
Aggregations