use of java.security.spec.KeySpec in project jdk8u_jdk by JetBrains.
the class Des3DkCrypto method getCipher.
protected Cipher getCipher(byte[] key, byte[] ivec, int mode) throws GeneralSecurityException {
// NoSuchAlgorithException
SecretKeyFactory factory = SecretKeyFactory.getInstance("desede");
// InvalidKeyException
KeySpec spec = new DESedeKeySpec(key, 0);
// InvalidKeySpecException
SecretKey secretKey = factory.generateSecret(spec);
// IV
if (ivec == null) {
ivec = ZERO_IV;
}
// NoSuchAlgorithmException, NoSuchPaddingException
// NoSuchProviderException
Cipher cipher = Cipher.getInstance("DESede/CBC/NoPadding");
IvParameterSpec encIv = new IvParameterSpec(ivec, 0, ivec.length);
// InvalidKeyException, InvalidAlgorithParameterException
cipher.init(mode, secretKey, encIv);
return cipher;
}
use of java.security.spec.KeySpec in project jdk8u_jdk by JetBrains.
the class DigestMD5Base method makeDesKeys.
/**
* Create parity-adjusted keys suitable for DES / DESede encryption.
*
* @param input A non-null byte array containing key material for
* DES / DESede.
* @param desStrength A string specifying eithe a DES or a DESede key.
* @return SecretKey An instance of either DESKeySpec or DESedeKeySpec.
*
* @throws NoSuchAlgorithmException if the either the DES or DESede
* algorithms cannote be lodaed by JCE.
* @throws InvalidKeyException if an invalid array of bytes is used
* as a key for DES or DESede.
* @throws InvalidKeySpecException in an invalid parameter is passed
* to either te DESKeySpec of the DESedeKeySpec constructors.
*/
private static SecretKey makeDesKeys(byte[] input, String desStrength) throws NoSuchAlgorithmException, InvalidKeyException, InvalidKeySpecException {
// Generate first subkey using first 7 bytes
byte[] subkey1 = addDesParity(input, 0, 7);
KeySpec spec = null;
SecretKeyFactory desFactory = SecretKeyFactory.getInstance(desStrength);
switch(desStrength) {
case "des":
spec = new DESKeySpec(subkey1, 0);
if (logger.isLoggable(Level.FINEST)) {
traceOutput(DP_CLASS_NAME, "makeDesKeys", "DIGEST42:DES key input: ", input);
traceOutput(DP_CLASS_NAME, "makeDesKeys", "DIGEST43:DES key parity-adjusted: ", subkey1);
traceOutput(DP_CLASS_NAME, "makeDesKeys", "DIGEST44:DES key material: ", ((DESKeySpec) spec).getKey());
logger.log(Level.FINEST, "DIGEST45: is parity-adjusted? {0}", Boolean.valueOf(DESKeySpec.isParityAdjusted(subkey1, 0)));
}
break;
case "desede":
// Generate second subkey using second 7 bytes
byte[] subkey2 = addDesParity(input, 7, 7);
// Construct 24-byte encryption-decryption-encryption sequence
byte[] ede = new byte[subkey1.length * 2 + subkey2.length];
System.arraycopy(subkey1, 0, ede, 0, subkey1.length);
System.arraycopy(subkey2, 0, ede, subkey1.length, subkey2.length);
System.arraycopy(subkey1, 0, ede, subkey1.length + subkey2.length, subkey1.length);
spec = new DESedeKeySpec(ede, 0);
if (logger.isLoggable(Level.FINEST)) {
traceOutput(DP_CLASS_NAME, "makeDesKeys", "DIGEST46:3DES key input: ", input);
traceOutput(DP_CLASS_NAME, "makeDesKeys", "DIGEST47:3DES key ede: ", ede);
traceOutput(DP_CLASS_NAME, "makeDesKeys", "DIGEST48:3DES key material: ", ((DESedeKeySpec) spec).getKey());
logger.log(Level.FINEST, "DIGEST49: is parity-adjusted? ", Boolean.valueOf(DESedeKeySpec.isParityAdjusted(ede, 0)));
}
break;
default:
throw new IllegalArgumentException("Invalid DES strength:" + desStrength);
}
return desFactory.generateSecret(spec);
}
use of java.security.spec.KeySpec in project android_frameworks_base by DirtyUnicorns.
the class BackupManagerService method buildCharArrayKey.
private SecretKey buildCharArrayKey(String algorithm, char[] pwArray, byte[] salt, int rounds) {
try {
SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(algorithm);
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 oxCore by GluuFederation.
the class StringEncrypter method decrypt.
/**
* Decrypt a string encrypted with this encrypter
*
* @param encryptedString
* Encrypted string
* @return Decrypted string
* @throws EncryptionException
*/
public String decrypt(final String encryptedString, String encryptionKey) throws EncryptionException {
lock.lock();
try {
final byte[] keyAsBytes = encryptionKey.getBytes(StringEncrypter.UNICODE_FORMAT);
String encryptionScheme = StringEncrypter.DESEDE_ENCRYPTION_SCHEME;
KeySpec keySpec;
if (encryptionScheme.equalsIgnoreCase(StringEncrypter.DESEDE_ENCRYPTION_SCHEME)) {
keySpec = new DESedeKeySpec(keyAsBytes);
} else if (encryptionScheme.equalsIgnoreCase(StringEncrypter.DES_ENCRYPTION_SCHEME)) {
keySpec = new DESKeySpec(keyAsBytes);
} else {
throw new IllegalArgumentException("Encryption scheme not supported: " + encryptionScheme);
}
return decrypt(encryptedString, keySpec);
} catch (final Exception e) {
throw new EncryptionException(e);
} finally {
lock.unlock();
}
}
use of java.security.spec.KeySpec in project oxCore by GluuFederation.
the class SamlConfiguration method loadPrivateKey.
/**
* loads the private key for digital signature
*
* @param prvKeyPath
* file path to location of private key
* @throws Exception
*/
public void loadPrivateKey(String prvKeyPath) throws Exception {
OpenSAMLUtil.initSamlEngine();
File privKeyFile = new File(prvKeyPath);
BufferedInputStream bis = null;
try {
bis = new BufferedInputStream(new FileInputStream(privKeyFile));
} catch (FileNotFoundException e) {
throw new Exception("Could not locate keyfile at '" + prvKeyPath + "'", e);
}
byte[] privKeyBytes = new byte[(int) privKeyFile.length()];
bis.read(privKeyBytes);
bis.close();
KeySpec ks = new PKCS8EncodedKeySpec(privKeyBytes);
KeyFactory keyFactory = KeyFactory.getInstance("RSA");
privateKey = keyFactory.generatePrivate(ks);
}
Aggregations