Search in sources :

Example 96 with PBEParameterSpec

use of javax.crypto.spec.PBEParameterSpec in project browsermator by pcalkins.

the class Protector method encrypt.

public static String encrypt(String property) throws GeneralSecurityException, UnsupportedEncodingException {
    SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("PBEWithMD5AndDES");
    SecretKey key = keyFactory.generateSecret(new PBEKeySpec(PASSWORD));
    Cipher pbeCipher = Cipher.getInstance("PBEWithMD5AndDES");
    pbeCipher.init(Cipher.ENCRYPT_MODE, key, new PBEParameterSpec(SALT, 20));
    return base64Encode(pbeCipher.doFinal(property.getBytes("UTF-8")));
}
Also used : PBEKeySpec(javax.crypto.spec.PBEKeySpec) SecretKey(javax.crypto.SecretKey) Cipher(javax.crypto.Cipher) SecretKeyFactory(javax.crypto.SecretKeyFactory) PBEParameterSpec(javax.crypto.spec.PBEParameterSpec)

Example 97 with PBEParameterSpec

use of javax.crypto.spec.PBEParameterSpec in project wechat by dllwh.

the class PasswordUtil method decrypt.

/**
 * @方法描述: 解密密文字符串
 * @创建者: 皇族灬战狼
 * @创建时间: 2016年5月23日 下午6:48:45
 * @param ciphertext
 *            待解密的密文字符串
 * @param password
 *            生成密钥时所使用的密码(如需解密,该参数需要与加密时使用的一致)
 * @param salt
 *            盐值(如需解密,该参数需要与加密时使用的一致)
 * @return 解密后的明文字符串
 */
public static String decrypt(String ciphertext, String password) {
    Key key = getPBEKey(password);
    byte[] passDec = null;
    PBEParameterSpec parameterSpec = new PBEParameterSpec(getStaticSalt(), ITERATIONCOUNT);
    try {
        Cipher cipher = Cipher.getInstance(ALGORITHM);
        cipher.init(Cipher.DECRYPT_MODE, key, parameterSpec);
        passDec = cipher.doFinal(hexStringToBytes(ciphertext));
    } catch (Exception e) {
        e.printStackTrace();
    }
    return new String(passDec);
}
Also used : Cipher(javax.crypto.Cipher) Key(java.security.Key) SecretKey(javax.crypto.SecretKey) PBEParameterSpec(javax.crypto.spec.PBEParameterSpec)

Example 98 with PBEParameterSpec

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);
    }
}
Also used : PBEKeySpec(javax.crypto.spec.PBEKeySpec) SecretKey(javax.crypto.SecretKey) Cipher(javax.crypto.Cipher) SecretKeyFactory(javax.crypto.SecretKeyFactory) PBEParameterSpec(javax.crypto.spec.PBEParameterSpec)

Example 99 with PBEParameterSpec

use of javax.crypto.spec.PBEParameterSpec in project ha-bridge by bwssytems.

the class BridgeSecurity method encrypt.

static String encrypt(String property) throws GeneralSecurityException, UnsupportedEncodingException {
    SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("PBEWithMD5AndDES");
    SecretKey key = keyFactory.generateSecret(new PBEKeySpec(habridgeKey));
    Cipher pbeCipher = Cipher.getInstance("PBEWithMD5AndDES");
    pbeCipher.init(Cipher.ENCRYPT_MODE, key, new PBEParameterSpec(SALT, 20));
    return base64Encode(pbeCipher.doFinal(property.getBytes("UTF-8")));
}
Also used : PBEKeySpec(javax.crypto.spec.PBEKeySpec) SecretKey(javax.crypto.SecretKey) Cipher(javax.crypto.Cipher) SecretKeyFactory(javax.crypto.SecretKeyFactory) PBEParameterSpec(javax.crypto.spec.PBEParameterSpec)

Example 100 with PBEParameterSpec

use of javax.crypto.spec.PBEParameterSpec in project yyl_example by Relucent.

the class PbeWithMd5AndDesExample method decrypt.

/**
 * 将加密文本进行解密
 * @param encryptText String
 * @return String
 */
public String decrypt(String encryptText) throws Exception {
    if (encryptText == null || encryptText.length() == 0) {
        return "";
    }
    PBEKeySpec pbks = new PBEKeySpec((password).toCharArray());
    SecretKeyFactory skf = SecretKeyFactory.getInstance("PBEWithMD5AndDES");
    SecretKey k = skf.generateSecret(pbks);
    StringTokenizer st = new StringTokenizer(hex2string(encryptText), " ");
    int num = 0;
    byte[] salt = new byte[8];
    while (st.hasMoreTokens() && (num < 8)) {
        salt[num] = (byte) (Integer.parseInt(st.nextToken()));
        num++;
    }
    int count = 0;
    byte[] cbtemp = new byte[2000];
    while (st.hasMoreTokens()) {
        cbtemp[count] = (byte) (Integer.parseInt(st.nextToken()));
        count++;
    }
    byte[] cb = new byte[count];
    for (int i = 0; i < cb.length; i++) {
        cb[i] = cbtemp[i];
    }
    Cipher cp = Cipher.getInstance("PBEWithMD5AndDES");
    PBEParameterSpec ps = new PBEParameterSpec(salt, 1000);
    cp.init(Cipher.DECRYPT_MODE, k, ps);
    byte[] ptext = cp.doFinal(cb);
    return new String(ptext, encoding);
}
Also used : PBEKeySpec(javax.crypto.spec.PBEKeySpec) SecretKey(javax.crypto.SecretKey) StringTokenizer(java.util.StringTokenizer) Cipher(javax.crypto.Cipher) SecretKeyFactory(javax.crypto.SecretKeyFactory) PBEParameterSpec(javax.crypto.spec.PBEParameterSpec)

Aggregations

PBEParameterSpec (javax.crypto.spec.PBEParameterSpec)101 SecretKey (javax.crypto.SecretKey)72 Cipher (javax.crypto.Cipher)65 PBEKeySpec (javax.crypto.spec.PBEKeySpec)59 SecretKeyFactory (javax.crypto.SecretKeyFactory)51 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)19 IvParameterSpec (javax.crypto.spec.IvParameterSpec)18 InvalidAlgorithmParameterException (java.security.InvalidAlgorithmParameterException)17 InvalidKeyException (java.security.InvalidKeyException)17 KeyStoreException (java.security.KeyStoreException)14 UnrecoverableKeyException (java.security.UnrecoverableKeyException)14 CertificateException (java.security.cert.CertificateException)14 AlgorithmParameters (java.security.AlgorithmParameters)12 SecureRandom (java.security.SecureRandom)12 CipherParameters (org.bouncycastle.crypto.CipherParameters)12 KeyParameter (org.bouncycastle.crypto.params.KeyParameter)12 ParametersWithIV (org.bouncycastle.crypto.params.ParametersWithIV)12 IOException (java.io.IOException)11 AlgorithmParameterSpec (java.security.spec.AlgorithmParameterSpec)9 Key (java.security.Key)8