use of javax.crypto.spec.PBEKeySpec 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.PBEKeySpec 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.PBEKeySpec in project XobotOS by xamarin.
the class JDKKeyStore method makePBECipher.
protected Cipher makePBECipher(String algorithm, int mode, char[] password, byte[] salt, int iterationCount) throws IOException {
try {
PBEKeySpec pbeSpec = new PBEKeySpec(password);
SecretKeyFactory keyFact = SecretKeyFactory.getInstance(algorithm, BouncyCastleProvider.PROVIDER_NAME);
PBEParameterSpec defParams = new PBEParameterSpec(salt, iterationCount);
Cipher cipher = Cipher.getInstance(algorithm, BouncyCastleProvider.PROVIDER_NAME);
cipher.init(mode, keyFact.generateSecret(pbeSpec), defParams);
return cipher;
} catch (Exception e) {
throw new IOException("Error initialising store of key store: " + e);
}
}
use of javax.crypto.spec.PBEKeySpec in project XobotOS by xamarin.
the class JDKPKCS12KeyStore method calculatePbeMac.
private static byte[] calculatePbeMac(DERObjectIdentifier oid, byte[] salt, int itCount, char[] password, boolean wrongPkcs12Zero, byte[] data) throws Exception {
SecretKeyFactory keyFact = SecretKeyFactory.getInstance(oid.getId(), bcProvider);
PBEParameterSpec defParams = new PBEParameterSpec(salt, itCount);
PBEKeySpec pbeSpec = new PBEKeySpec(password);
JCEPBEKey key = (JCEPBEKey) keyFact.generateSecret(pbeSpec);
key.setTryWrongPKCS12Zero(wrongPkcs12Zero);
Mac mac = Mac.getInstance(oid.getId(), bcProvider);
mac.init(key, defParams);
mac.update(data);
return mac.doFinal();
}
use of javax.crypto.spec.PBEKeySpec in project OpenAM by OpenRock.
the class DataEncryptor method encryptWithSymmetricKey.
/**
* Encrypts the given data with a symmetric key that was generated
* using given shared secret.
* @param data the data to be encrypted.
* @param encAlgorithm the encryption algorithm to be used.
* The encryption algorithm must be one of the supported
* algorithm by the underlying JCE encryption provider.
* For password based encryptions, the encryption algorithm
* PBEWithMD5AndDES is commonly used.
* @param secret the shared secret to be used for symmetric encryption.
* @return the encrypted data in Base64 encoded format.
*/
public static String encryptWithSymmetricKey(String data, String encAlgorithm, String secret) throws Exception {
try {
String algorithm = encAlgorithm;
if (!algorithm.startsWith("PBEWith")) {
algorithm = "PBEWithMD5And" + encAlgorithm;
}
SecretKeyFactory skFactory = SecretKeyFactory.getInstance(algorithm);
PBEKeySpec pbeKeySpec = new PBEKeySpec(secret.toCharArray());
SecretKey sKey = skFactory.generateSecret(pbeKeySpec);
Cipher cipher = Cipher.getInstance(algorithm);
cipher.init(Cipher.ENCRYPT_MODE, sKey, pbeParameterSpec);
byte[] encData = cipher.doFinal(data.getBytes("UTF-8"));
encData = addPrefix(encData);
return Base64.encode(encData);
} catch (NoSuchAlgorithmException nse) {
throw new Exception(nse.getMessage());
}
}
Aggregations