use of javax.crypto.SecretKeyFactory in project tomcat by apache.
the class SecretKeyCredentialHandler method setAlgorithm.
@Override
public void setAlgorithm(String algorithm) throws NoSuchAlgorithmException {
SecretKeyFactory secretKeyFactory = SecretKeyFactory.getInstance(algorithm);
this.secretKeyFactory = secretKeyFactory;
}
use of javax.crypto.SecretKeyFactory in project translationstudio8 by heartsome.
the class DESImpl method decrypt.
/**
* 解密.
* @param src
* 数据源
* @param key
* 密钥,长度必须是8的倍数
* @return byte[]
* 解密后的原始数据
* @throws Exception
* the exception
*/
public static byte[] decrypt(byte[] src, byte[] key) throws Exception {
SecureRandom sr = new SecureRandom();
DESKeySpec dks = new DESKeySpec(key);
SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(DES);
SecretKey securekey = keyFactory.generateSecret(dks);
Cipher cipher = Cipher.getInstance(DES);
cipher.init(Cipher.DECRYPT_MODE, securekey, sr);
return cipher.doFinal(src);
}
use of javax.crypto.SecretKeyFactory in project translationstudio8 by heartsome.
the class DESImpl method encrypt.
/**
* 加密.
* @param src
* 数据源
* @param key
* 密钥,长度必须是8的倍数
* @return byte[]
* 加密后的数据
* @throws Exception
* the exception
*/
public static byte[] encrypt(byte[] src, byte[] key) throws Exception {
SecureRandom sr = new SecureRandom();
DESKeySpec dks = new DESKeySpec(key);
SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(DES);
SecretKey securekey = keyFactory.generateSecret(dks);
Cipher cipher = Cipher.getInstance(DES);
cipher.init(Cipher.ENCRYPT_MODE, securekey, sr);
return cipher.doFinal(src);
}
use of javax.crypto.SecretKeyFactory in project musicbrainz-android by jdamcd.
the class SimpleEncrypt method crypto.
private static String crypto(int mode, String secret, String input) throws InvalidKeyException, UnsupportedEncodingException, NoSuchAlgorithmException, InvalidKeySpecException, NoSuchPaddingException, IllegalBlockSizeException, BadPaddingException {
DESKeySpec keySpec = new DESKeySpec(secret.getBytes(CHARSET));
SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(ALGORITHM);
SecretKey encryptKey = keyFactory.generateSecret(keySpec);
Cipher cipher = Cipher.getInstance(ALGORITHM);
cipher.init(mode, encryptKey);
if (mode == Cipher.ENCRYPT_MODE) {
byte[] plainText = input.getBytes(CHARSET);
return Base64.encodeToString(cipher.doFinal(plainText), Base64.DEFAULT);
} else if (mode == Cipher.DECRYPT_MODE) {
byte[] cipherText = Base64.decode(input, Base64.DEFAULT);
return new String(cipher.doFinal(cipherText), CHARSET);
}
return null;
}
use of javax.crypto.SecretKeyFactory in project orientdb by orientechnologies.
the class OSymmetricKey method create.
protected void create() {
try {
SecureRandom secureRandom = new SecureRandom();
byte[] salt = secureRandom.generateSeed(saltLength);
KeySpec keySpec = new PBEKeySpec(seedPhrase.toCharArray(), salt, iteration, keySize);
SecretKeyFactory factory = SecretKeyFactory.getInstance(seedAlgorithm);
SecretKey tempKey = factory.generateSecret(keySpec);
secretKey = new SecretKeySpec(tempKey.getEncoded(), secretKeyAlgorithm);
} catch (Exception ex) {
throw new OSecurityException("OSymmetricKey.create() Exception: " + ex);
}
}
Aggregations