use of javax.crypto.SecretKeyFactory in project DataX by alibaba.
the class DESCipher method decrypt.
/**
* * 解密
*
* *
*
* * @param src
*
* * 密文(字节)
*
* * @param key
*
* * 密钥,长度必须是8的倍数
*
* * @return 明文(字节)
*
* * @throws Exception
*
*
*/
public static byte[] decrypt(byte[] src, byte[] key) throws Exception {
// DES算法要求有一个可信任的随机数源
SecureRandom sr = new SecureRandom();
// 从原始密匙数据创建一个DESKeySpec对象
DESKeySpec dks = new DESKeySpec(key);
// 创建一个密匙工厂,然后用它把DESKeySpec对象转换成
// 一个SecretKey对象
SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(DES);
SecretKey securekey = keyFactory.generateSecret(dks);
// Cipher对象实际完成解密操作
Cipher cipher = Cipher.getInstance(DES);
// 用密匙初始化Cipher对象
cipher.init(Cipher.DECRYPT_MODE, securekey, sr);
return cipher.doFinal(src);
}
use of javax.crypto.SecretKeyFactory in project android-pbe by nelenkov.
the class Crypto method deriveKeyPkcs12.
public static SecretKey deriveKeyPkcs12(byte[] salt, String password) {
try {
long start = System.currentTimeMillis();
KeySpec keySpec = new PBEKeySpec(password.toCharArray(), salt, ITERATION_COUNT, KEY_LENGTH);
SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(PKCS12_DERIVATION_ALGORITHM);
SecretKey result = keyFactory.generateSecret(keySpec);
long elapsed = System.currentTimeMillis() - start;
Log.d(TAG, String.format("PKCS#12 key derivation took %d [ms].", elapsed));
return result;
} catch (GeneralSecurityException e) {
throw new RuntimeException(e);
}
}
use of javax.crypto.SecretKeyFactory in project android-pbe by nelenkov.
the class Crypto method deriveKeyPbkdf2.
public static SecretKey deriveKeyPbkdf2(byte[] salt, String password) {
try {
long start = System.currentTimeMillis();
KeySpec keySpec = new PBEKeySpec(password.toCharArray(), salt, ITERATION_COUNT, KEY_LENGTH);
SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(PBKDF2_DERIVATION_ALGORITHM);
byte[] keyBytes = keyFactory.generateSecret(keySpec).getEncoded();
Log.d(TAG, "key bytes: " + toHex(keyBytes));
SecretKey result = new SecretKeySpec(keyBytes, "AES");
long elapsed = System.currentTimeMillis() - start;
Log.d(TAG, String.format("PBKDF2 key derivation took %d [ms].", elapsed));
return result;
} catch (GeneralSecurityException e) {
throw new RuntimeException(e);
}
}
use of javax.crypto.SecretKeyFactory in project netty by netty.
the class SslContext method generateKeySpec.
/**
* Generates a key specification for an (encrypted) private key.
*
* @param password characters, if {@code null} an unencrypted key is assumed
* @param key bytes of the DER encoded private key
*
* @return a key specification
*
* @throws IOException if parsing {@code key} fails
* @throws NoSuchAlgorithmException if the algorithm used to encrypt {@code key} is unkown
* @throws NoSuchPaddingException if the padding scheme specified in the decryption algorithm is unkown
* @throws InvalidKeySpecException if the decryption key based on {@code password} cannot be generated
* @throws InvalidKeyException if the decryption key based on {@code password} cannot be used to decrypt
* {@code key}
* @throws InvalidAlgorithmParameterException if decryption algorithm parameters are somehow faulty
*/
protected static PKCS8EncodedKeySpec generateKeySpec(char[] password, byte[] key) throws IOException, NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeySpecException, InvalidKeyException, InvalidAlgorithmParameterException {
if (password == null) {
return new PKCS8EncodedKeySpec(key);
}
EncryptedPrivateKeyInfo encryptedPrivateKeyInfo = new EncryptedPrivateKeyInfo(key);
SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(encryptedPrivateKeyInfo.getAlgName());
PBEKeySpec pbeKeySpec = new PBEKeySpec(password);
SecretKey pbeKey = keyFactory.generateSecret(pbeKeySpec);
Cipher cipher = Cipher.getInstance(encryptedPrivateKeyInfo.getAlgName());
cipher.init(Cipher.DECRYPT_MODE, pbeKey, encryptedPrivateKeyInfo.getAlgParameters());
return encryptedPrivateKeyInfo.getKeySpec(cipher);
}
use of javax.crypto.SecretKeyFactory in project orientdb by orientechnologies.
the class OSecurityManager method getPbkdf2.
private byte[] getPbkdf2(final String iPassword, final byte[] salt, final int iterations, final int bytes, final String algorithm) {
String cacheKey = null;
final String hashedPassword = createSHA256(iPassword + new String(salt));
if (SALT_CACHE != null) {
// SEARCH IN CACHE FIRST
cacheKey = hashedPassword + "|" + Arrays.toString(salt) + "|" + iterations + "|" + bytes;
final byte[] encoded = SALT_CACHE.get(cacheKey);
if (encoded != null)
return encoded;
}
final PBEKeySpec spec = new PBEKeySpec(iPassword.toCharArray(), salt, iterations, bytes * 8);
final SecretKeyFactory skf;
try {
skf = SecretKeyFactory.getInstance(algorithm);
final byte[] encoded = skf.generateSecret(spec).getEncoded();
if (SALT_CACHE != null) {
// SAVE IT IN CACHE
SALT_CACHE.put(cacheKey, encoded);
}
return encoded;
} catch (Exception e) {
throw OException.wrapException(new OSecurityException("Cannot create a key with '" + algorithm + "' algorithm"), e);
}
}
Aggregations