use of javax.crypto.SecretKey in project Gradle-demo by Arisono.
the class DESUtil method encryptDES.
/**
* 加密
* @throws Exception
*/
public static byte[] encryptDES(byte[] data, byte[] key) throws Exception {
//获得密钥
SecretKey secretKey = new SecretKeySpec(key, "DES");
//Cipher完成加密
Cipher cipher = Cipher.getInstance("DES");
//初始化cipher
cipher.init(Cipher.ENCRYPT_MODE, secretKey);
//加密
byte[] encrypt = cipher.doFinal(data);
return encrypt;
}
use of javax.crypto.SecretKey in project Gradle-demo by Arisono.
the class DESede method initKey.
/**
* 生成密钥
* @throws Exception
*/
public static byte[] initKey() throws Exception {
//密钥生成器
KeyGenerator keyGen = KeyGenerator.getInstance("DESede");
//初始化密钥生成器
//可指定密钥长度为112或168,默认168
keyGen.init(168);
//生成密钥
SecretKey secretKey = keyGen.generateKey();
return secretKey.getEncoded();
}
use of javax.crypto.SecretKey in project Gradle-demo by Arisono.
the class DESede method decrypt3DES.
/**
* 解密
*/
public static byte[] decrypt3DES(byte[] data, byte[] key) throws Exception {
//恢复密钥
SecretKey secretKey = new SecretKeySpec(key, "DESede");
//Cipher完成解密
Cipher cipher = Cipher.getInstance("DESede");
//初始化cipher
cipher.init(Cipher.DECRYPT_MODE, secretKey);
byte[] plain = cipher.doFinal(data);
return plain;
}
use of javax.crypto.SecretKey in project graylog2-server by Graylog2.
the class KeyUtil method createKeySpec.
private static PKCS8EncodedKeySpec createKeySpec(byte[] keyBytes, String password) throws IOException, NoSuchAlgorithmException, InvalidKeySpecException, NoSuchPaddingException, InvalidKeyException, InvalidAlgorithmParameterException {
if (Strings.isNullOrEmpty(password)) {
return new PKCS8EncodedKeySpec(keyBytes);
}
final EncryptedPrivateKeyInfo pkInfo = new EncryptedPrivateKeyInfo(keyBytes);
final SecretKeyFactory kf = SecretKeyFactory.getInstance(pkInfo.getAlgName());
final PBEKeySpec keySpec = new PBEKeySpec(password.toCharArray());
final SecretKey secretKey = kf.generateSecret(keySpec);
@SuppressWarnings("InsecureCryptoUsage") final Cipher cipher = Cipher.getInstance(pkInfo.getAlgName());
cipher.init(Cipher.DECRYPT_MODE, secretKey, pkInfo.getAlgParameters());
return pkInfo.getKeySpec(cipher);
}
use of javax.crypto.SecretKey in project AndroidChromium by JackyAndroid.
the class WebappAuthenticator method getMac.
/**
* @return A Mac, or null if it is not possible to instantiate one.
*/
private static Mac getMac(Context context) {
try {
SecretKey key = getKey(context);
if (key == null) {
// random number generator, and we must not claim that authentication can work.
return null;
}
Mac mac = Mac.getInstance(MAC_ALGORITHM_NAME);
mac.init(key);
return mac;
} catch (GeneralSecurityException e) {
Log.w(TAG, "Error in creating MAC instance", e);
return null;
}
}
Aggregations