use of javax.crypto.spec.PBEParameterSpec in project android-demos by novoda.
the class Encrypt method crypt.
public String crypt(int mode, String encryption_subject) throws CryptException {
final PBEParameterSpec ps = new javax.crypto.spec.PBEParameterSpec(SALT, 20);
final SecretKeyFactory kf = getSecretKeyFactory();
final SecretKey k = getSecretKey(kf);
final Cipher crypter = getCipherInstance();
String result;
switch(mode) {
case Cipher.DECRYPT_MODE:
initialise(ps, k, crypter, Cipher.DECRYPT_MODE);
result = getString(encryption_subject, crypter);
break;
case Cipher.ENCRYPT_MODE:
default:
initialise(ps, k, crypter, Cipher.ENCRYPT_MODE);
result = encode(encryption_subject, crypter);
}
return result;
}
use of javax.crypto.spec.PBEParameterSpec in project android-pbe by nelenkov.
the class Crypto method decryptPkcs12.
public static String decryptPkcs12(byte[] cipherBytes, SecretKey key, byte[] salt) {
try {
Cipher cipher = Cipher.getInstance(CIPHER_ALGORITHM);
PBEParameterSpec pbeSpec = new PBEParameterSpec(salt, ITERATION_COUNT);
cipher.init(Cipher.DECRYPT_MODE, key, pbeSpec);
Log.d(TAG, "Cipher IV: " + toHex(cipher.getIV()));
byte[] plainBytes = cipher.doFinal(cipherBytes);
String plainrStr = new String(plainBytes, "UTF-8");
return plainrStr;
} catch (GeneralSecurityException e) {
throw new RuntimeException(e);
} catch (UnsupportedEncodingException e) {
throw new RuntimeException(e);
}
}
use of javax.crypto.spec.PBEParameterSpec in project ButterRemote-Android by se-bastiaan.
the class ObscuredSharedPreferences method encrypt.
protected String encrypt(String value) {
try {
final byte[] bytes = value != null ? value.getBytes(UTF8) : new byte[0];
SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(PBE_WITH_MD5_AND_DES);
SecretKey key = keyFactory.generateSecret(new PBEKeySpec(SEKRIT));
Cipher pbeCipher = Cipher.getInstance(PBE_WITH_MD5_AND_DES);
pbeCipher.init(Cipher.ENCRYPT_MODE, key, new PBEParameterSpec(Settings.Secure.getString(context.getContentResolver(), Settings.System.ANDROID_ID).getBytes(UTF8), 20));
return new String(Base64.encode(pbeCipher.doFinal(bytes), Base64.NO_WRAP), UTF8);
} catch (Exception e) {
throw new RuntimeException(e);
}
}
use of javax.crypto.spec.PBEParameterSpec in project XobotOS by xamarin.
the class JDKPKCS12KeyStore method cryptData.
protected byte[] cryptData(boolean forEncryption, AlgorithmIdentifier algId, char[] password, boolean wrongPKCS12Zero, byte[] data) throws IOException {
String algorithm = algId.getObjectId().getId();
PKCS12PBEParams pbeParams = new PKCS12PBEParams((ASN1Sequence) algId.getParameters());
PBEKeySpec pbeSpec = new PBEKeySpec(password);
try {
SecretKeyFactory keyFact = SecretKeyFactory.getInstance(algorithm, bcProvider);
PBEParameterSpec defParams = new PBEParameterSpec(pbeParams.getIV(), pbeParams.getIterations().intValue());
JCEPBEKey key = (JCEPBEKey) keyFact.generateSecret(pbeSpec);
key.setTryWrongPKCS12Zero(wrongPKCS12Zero);
Cipher cipher = Cipher.getInstance(algorithm, bcProvider);
int mode = forEncryption ? Cipher.ENCRYPT_MODE : Cipher.DECRYPT_MODE;
cipher.init(mode, key, defParams);
return cipher.doFinal(data);
} catch (Exception e) {
throw new IOException("exception decrypting data - " + e.toString());
}
}
use of javax.crypto.spec.PBEParameterSpec in project XobotOS by xamarin.
the class JCEBlockCipher 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) {
pbeSpec = (PBEParameterSpec) params;
param = PBE.Util.makePBEParameters(k, params, cipher.getUnderlyingCipher().getAlgorithmName());
} else {
throw new InvalidAlgorithmParameterException("PBE requires PBE parameters to be set.");
}
if (param instanceof ParametersWithIV) {
ivParam = (ParametersWithIV) param;
}
} else if (params == null) {
param = new KeyParameter(key.getEncoded());
} else if (params instanceof IvParameterSpec) {
if (ivLength != 0) {
IvParameterSpec p = (IvParameterSpec) params;
if (p.getIV().length != ivLength && !isAEADModeName(modeName)) {
throw new InvalidAlgorithmParameterException("IV must be " + ivLength + " bytes long.");
}
param = new ParametersWithIV(new KeyParameter(key.getEncoded()), p.getIV());
ivParam = (ParametersWithIV) param;
} else {
if (modeName != null && modeName.equals("ECB")) {
throw new InvalidAlgorithmParameterException("ECB mode does not use an IV");
}
param = new KeyParameter(key.getEncoded());
}
} else // BEGIN android-removed
// else if (params instanceof GOST28147ParameterSpec)
// {
// GOST28147ParameterSpec gost28147Param = (GOST28147ParameterSpec)params;
//
// param = new ParametersWithSBox(
// new KeyParameter(key.getEncoded()), ((GOST28147ParameterSpec)params).getSbox());
//
// if (gost28147Param.getIV() != null && ivLength != 0)
// {
// param = new ParametersWithIV(param, gost28147Param.getIV());
// ivParam = (ParametersWithIV)param;
// }
// }
// else if (params instanceof RC2ParameterSpec)
// {
// RC2ParameterSpec rc2Param = (RC2ParameterSpec)params;
//
// param = new RC2Parameters(key.getEncoded(), ((RC2ParameterSpec)params).getEffectiveKeyBits());
//
// if (rc2Param.getIV() != null && ivLength != 0)
// {
// param = new ParametersWithIV(param, rc2Param.getIV());
// ivParam = (ParametersWithIV)param;
// }
// }
// else if (params instanceof RC5ParameterSpec)
// {
// RC5ParameterSpec rc5Param = (RC5ParameterSpec)params;
//
// param = new RC5Parameters(key.getEncoded(), ((RC5ParameterSpec)params).getRounds());
// if (baseEngine.getAlgorithmName().startsWith("RC5"))
// {
// if (baseEngine.getAlgorithmName().equals("RC5-32"))
// {
// if (rc5Param.getWordSize() != 32)
// {
// throw new InvalidAlgorithmParameterException("RC5 already set up for a word size of 32 not " + rc5Param.getWordSize() + ".");
// }
// }
// else if (baseEngine.getAlgorithmName().equals("RC5-64"))
// {
// if (rc5Param.getWordSize() != 64)
// {
// throw new InvalidAlgorithmParameterException("RC5 already set up for a word size of 64 not " + rc5Param.getWordSize() + ".");
// }
// }
// }
// else
// {
// throw new InvalidAlgorithmParameterException("RC5 parameters passed to a cipher that is not RC5.");
// }
// if ((rc5Param.getIV() != null) && (ivLength != 0))
// {
// param = new ParametersWithIV(param, rc5Param.getIV());
// ivParam = (ParametersWithIV)param;
// }
// }
// END android-removed
{
throw new InvalidAlgorithmParameterException("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 if (cipher.getUnderlyingCipher().getAlgorithmName().indexOf("PGPCFB") < 0) {
throw new InvalidAlgorithmParameterException("no IV set when one expected");
}
}
if (random != null && padded) {
param = new ParametersWithRandom(param, random);
}
try {
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:
throw new InvalidParameterException("unknown opmode " + opmode + " passed");
}
} catch (Exception e) {
throw new InvalidKeyException(e.getMessage());
}
}
Aggregations