use of javax.crypto.spec.IvParameterSpec in project XobotOS by xamarin.
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!");
}
}
use of javax.crypto.spec.IvParameterSpec in project android_frameworks_base by ResurrectionRemix.
the class CryptoHelper method decryptBundle.
@Nullable
/* default */
Bundle decryptBundle(@NonNull Bundle bundle) throws GeneralSecurityException {
Preconditions.checkNotNull(bundle, "Cannot decrypt null bundle.");
byte[] iv = bundle.getByteArray(KEY_IV);
byte[] encryptedBytes = bundle.getByteArray(KEY_CIPHER);
byte[] mac = bundle.getByteArray(KEY_MAC);
if (!verifyMac(encryptedBytes, iv, mac)) {
Log.w(TAG, "Escrow mac mismatched!");
return null;
}
IvParameterSpec ivSpec = new IvParameterSpec(iv);
Cipher cipher = Cipher.getInstance(CIPHER_ALGORITHM);
cipher.init(Cipher.DECRYPT_MODE, mEncryptionKey, ivSpec);
byte[] decryptedBytes = cipher.doFinal(encryptedBytes);
Parcel decryptedParcel = Parcel.obtain();
decryptedParcel.unmarshall(decryptedBytes, 0, decryptedBytes.length);
decryptedParcel.setDataPosition(0);
Bundle decryptedBundle = new Bundle();
decryptedBundle.readFromParcel(decryptedParcel);
decryptedParcel.recycle();
return decryptedBundle;
}
use of javax.crypto.spec.IvParameterSpec in project wechat by motianhuo.
the class DES method decryptDES.
/**
* 数据解密
*
* @param decryptString
* @return
* @throws Exception
*/
public static String decryptDES(String decryptString) throws Exception {
byte[] byteMi = new BASE64().decode(decryptString);
IvParameterSpec zeroIv = new IvParameterSpec(iv);
SecretKeySpec key = new SecretKeySpec(getkeys().getBytes(), "DES");
Cipher cipher = Cipher.getInstance("DES/CBC/PKCS5Padding");
cipher.init(Cipher.DECRYPT_MODE, key, zeroIv);
byte[] decryptedData = cipher.doFinal(byteMi);
return new String(decryptedData);
}
use of javax.crypto.spec.IvParameterSpec in project perun by CESNET.
the class Utils method cipherInput.
/**
* Return en/decrypted version of input using AES/CBC/PKCS5PADDING cipher.
* Perun's internal secretKey and initVector are used (you can configure them in
* perun.properties file).
*
* @param plainText text to en/decrypt
* @param decrypt TRUE = decrypt input / FALSE = encrypt input
* @return en/decrypted text
* @throws cz.metacentrum.perun.core.api.exceptions.InternalErrorException if anything fails
*/
public static String cipherInput(String plainText, boolean decrypt) throws InternalErrorException {
try {
String encryptionKey = BeansUtils.getCoreConfig().getPwdresetSecretKey();
String initVector = BeansUtils.getCoreConfig().getPwdresetInitVector();
Cipher c = Cipher.getInstance("AES/CBC/PKCS5PADDING");
SecretKeySpec k = new SecretKeySpec(encryptionKey.getBytes("UTF-8"), "AES");
c.init((decrypt) ? Cipher.DECRYPT_MODE : Cipher.ENCRYPT_MODE, k, new IvParameterSpec(initVector.getBytes("UTF-8")));
if (decrypt) {
byte[] bytes = Base64.decodeBase64(plainText.getBytes("UTF-8"));
return new String(c.doFinal(bytes), "UTF-8");
} else {
byte[] bytes = Base64.encodeBase64(c.doFinal(plainText.getBytes("UTF-8")));
return new String(bytes, "UTF-8");
}
} catch (Exception ex) {
throw new InternalErrorException("Error when encrypting message", ex);
}
}
use of javax.crypto.spec.IvParameterSpec in project graylog2-server by Graylog2.
the class AESTools method decrypt.
@Nullable
public static String decrypt(String cipherText, String encryptionKey, String salt) {
try {
@SuppressFBWarnings("CIPHER_INTEGRITY") Cipher cipher = Cipher.getInstance("AES/CBC/ISO10126Padding", "SunJCE");
SecretKeySpec key = new SecretKeySpec(encryptionKey.getBytes("UTF-8"), "AES");
cipher.init(Cipher.DECRYPT_MODE, key, new IvParameterSpec(salt.getBytes("UTF-8")));
return new String(cipher.doFinal(Hex.decode(cipherText)), "UTF-8");
} catch (Exception e) {
LOG.error("Could not decrypt value.", e);
}
return null;
}
Aggregations