use of javax.crypto.spec.PBEParameterSpec in project ha-bridge by bwssytems.
the class BridgeSecurity method decrypt.
static String decrypt(String property) throws GeneralSecurityException, IOException {
SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("PBEWithMD5AndDES");
SecretKey key = keyFactory.generateSecret(new PBEKeySpec(habridgeKey));
Cipher pbeCipher = Cipher.getInstance("PBEWithMD5AndDES");
pbeCipher.init(Cipher.DECRYPT_MODE, key, new PBEParameterSpec(SALT, 20));
return new String(pbeCipher.doFinal(base64Decode(property)), "UTF-8");
}
use of javax.crypto.spec.PBEParameterSpec in project SKMCLauncher by SKCraft.
the class Launcher method getCipher.
/**
* Get a cipher.
*
* @param mode cipher mode (see {@link Cipher} constants)
* @param password password
* @return cipher
* @throws InvalidKeySpecException on cipher error
* @throws NoSuchAlgorithmException on cipher error
* @throws NoSuchPaddingException on cipher error
* @throws InvalidKeyException on cipher error
* @throws InvalidAlgorithmParameterException on cipher error
*/
public Cipher getCipher(int mode, String password) throws InvalidKeySpecException, NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException, InvalidAlgorithmParameterException {
Random random = new Random(0x29482c2L);
byte[] salt = new byte[8];
random.nextBytes(salt);
PBEParameterSpec paramSpec = new PBEParameterSpec(salt, 5);
SecretKeyFactory factory = SecretKeyFactory.getInstance("PBEWithMD5AndDES");
SecretKey key = factory.generateSecret(new PBEKeySpec(password.toCharArray()));
Cipher cipher = Cipher.getInstance("PBEWithMD5AndDES");
cipher.init(mode, key, paramSpec);
return cipher;
}
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 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());
}
}
Aggregations