use of javax.crypto.spec.PBEParameterSpec in project com.revolsys.open by revolsys.
the class PasswordUtil method encrypt.
public static byte[] encrypt(final byte[] data, final char[] password, final byte[] salt, final int noIterations) {
try {
final String method = "PBEWithMD5AndTripleDES";
final SecretKeyFactory kf = SecretKeyFactory.getInstance(method);
final PBEKeySpec keySpec = new PBEKeySpec(password);
final SecretKey key = kf.generateSecret(keySpec);
final Cipher ciph = Cipher.getInstance(method);
final PBEParameterSpec params = new PBEParameterSpec(salt, noIterations);
return ciph.doFinal(data);
} catch (final Exception e) {
throw new RuntimeException("Spurious encryption error");
}
}
use of javax.crypto.spec.PBEParameterSpec in project keystore-explorer by kaikramer.
the class Pkcs8Util method getEncrypted.
/**
* PKCS #8 encode and encrypt a private key.
*
* @return The encrypted encoding
* @param privateKey
* The private key
* @param pbeType
* PBE algorithm to use for encryption
* @param password
* Encryption password
* @throws CryptoException
* Problem encountered while getting the encoded private key
* @throws IOException
* If an I/O error occurred
*/
public static byte[] getEncrypted(PrivateKey privateKey, Pkcs8PbeType pbeType, Password password) throws CryptoException, IOException {
try {
byte[] pkcs8 = get(privateKey);
// Generate PBE secret key from password
SecretKeyFactory keyFact = SecretKeyFactory.getInstance(pbeType.jce());
PBEKeySpec pbeKeySpec = new PBEKeySpec(password.toCharArray());
SecretKey pbeKey = keyFact.generateSecret(pbeKeySpec);
// Generate random salt and iteration count
byte[] salt = generateSalt();
int iterationCount = generateIterationCount();
// Store in algorithm parameters
PBEParameterSpec pbeParameterSpec = new PBEParameterSpec(salt, iterationCount);
AlgorithmParameters params = AlgorithmParameters.getInstance(pbeType.jce());
params.init(pbeParameterSpec);
// Create PBE cipher from key and params
Cipher cipher = Cipher.getInstance(pbeType.jce());
cipher.init(Cipher.ENCRYPT_MODE, pbeKey, params);
// Encrypt key
byte[] encPkcs8 = cipher.doFinal(pkcs8);
// Create and return encrypted private key information
EncryptedPrivateKeyInfo encPrivateKeyInfo = new EncryptedPrivateKeyInfo(params, encPkcs8);
return encPrivateKeyInfo.getEncoded();
} catch (GeneralSecurityException ex) {
throw new CryptoException("NoEncryptPkcs8PrivateKey.exception.message", ex);
}
}
use of javax.crypto.spec.PBEParameterSpec in project entando-core by entando.
the class PageTokenManager method encrypt.
@Override
public String encrypt(String property) {
SecretKeyFactory keyFactory;
try {
keyFactory = SecretKeyFactory.getInstance("PBEWithMD5AndDES");
SecretKey key = keyFactory.generateSecret(new PBEKeySpec(this.getPasswordCharArray()));
Cipher pbeCipher = Cipher.getInstance("PBEWithMD5AndDES");
pbeCipher.init(Cipher.ENCRYPT_MODE, key, new PBEParameterSpec(this.getSalt().getBytes(), 20));
return base64Encode(pbeCipher.doFinal(property.getBytes("UTF-8")));
} catch (GeneralSecurityException e) {
logger.error("Error in encrypt", e);
} catch (UnsupportedEncodingException e) {
logger.error("Error in encrypt", e);
}
return null;
}
use of javax.crypto.spec.PBEParameterSpec in project protools by SeanDragon.
the class ToolPBE method decrypt.
/**
* 解密
*
* @param data
* 数据
* @param password
* 密码
* @param salt
* 盐
*
* @return byte[] 解密数据
*
* @throws Exception
*/
public static byte[] decrypt(byte[] data, String password, byte[] salt) throws InvalidKeySpecException, NoSuchAlgorithmException, NoSuchPaddingException, InvalidAlgorithmParameterException, InvalidKeyException, BadPaddingException, IllegalBlockSizeException {
// 转换密钥
Key key = toKey(password);
// 实例化PBE参数材料
PBEParameterSpec paramSpec = new PBEParameterSpec(salt, 100);
// 实例化
Cipher cipher = Cipher.getInstance(ALGORITHM);
// 初始化
cipher.init(Cipher.DECRYPT_MODE, key, paramSpec);
// 执行操作
return cipher.doFinal(data);
}
use of javax.crypto.spec.PBEParameterSpec in project keepass2android by PhilippC.
the class JCEStreamCipher method engineInit.
protected void engineInit(int opmode, Key key, AlgorithmParameterSpec params, SecureRandom random) throws InvalidKeyException, InvalidAlgorithmParameterException {
CipherParameters param;
this.pbeSpec = null;
this.pbeAlgorithm = null;
this.engineParams = null;
//
if (!(key instanceof SecretKey)) {
throw new InvalidKeyException("Key for algorithm " + key.getAlgorithm() + " not suitable for symmetric enryption.");
}
if (key instanceof JCEPBEKey) {
JCEPBEKey k = (JCEPBEKey) key;
if (k.getOID() != null) {
pbeAlgorithm = k.getOID().getId();
} else {
pbeAlgorithm = k.getAlgorithm();
}
if (k.getParam() != null) {
param = k.getParam();
pbeSpec = new PBEParameterSpec(k.getSalt(), k.getIterationCount());
} else if (params instanceof PBEParameterSpec) {
param = PBE.Util.makePBEParameters(k, params, cipher.getAlgorithmName());
pbeSpec = (PBEParameterSpec) params;
} else {
throw new InvalidAlgorithmParameterException("PBE requires PBE parameters to be set.");
}
if (k.getIvSize() != 0) {
ivParam = (ParametersWithIV) param;
}
} else if (params == null) {
param = new KeyParameter(key.getEncoded());
} else if (params instanceof IvParameterSpec) {
param = new ParametersWithIV(new KeyParameter(key.getEncoded()), ((IvParameterSpec) params).getIV());
ivParam = (ParametersWithIV) param;
} else {
throw new IllegalArgumentException("unknown parameter type.");
}
if ((ivLength != 0) && !(param instanceof ParametersWithIV)) {
SecureRandom ivRandom = random;
if (ivRandom == null) {
ivRandom = new SecureRandom();
}
if ((opmode == Cipher.ENCRYPT_MODE) || (opmode == Cipher.WRAP_MODE)) {
byte[] iv = new byte[ivLength];
ivRandom.nextBytes(iv);
param = new ParametersWithIV(param, iv);
ivParam = (ParametersWithIV) param;
} else {
throw new InvalidAlgorithmParameterException("no IV set when one expected");
}
}
switch(opmode) {
case Cipher.ENCRYPT_MODE:
case Cipher.WRAP_MODE:
cipher.init(true, param);
break;
case Cipher.DECRYPT_MODE:
case Cipher.UNWRAP_MODE:
cipher.init(false, param);
break;
default:
System.out.println("eeek!");
}
}
Aggregations