Search in sources :

Example 51 with PBEParameterSpec

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");
}
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 52 with PBEParameterSpec

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

Example 53 with PBEParameterSpec

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

Example 54 with PBEParameterSpec

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);
    }
}
Also used : GeneralSecurityException(java.security.GeneralSecurityException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) Cipher(javax.crypto.Cipher) PBEParameterSpec(javax.crypto.spec.PBEParameterSpec)

Example 55 with PBEParameterSpec

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());
    }
}
Also used : PBEKeySpec(javax.crypto.spec.PBEKeySpec) PKCS12PBEParams(org.bouncycastle.asn1.pkcs.PKCS12PBEParams) ASN1OctetString(org.bouncycastle.asn1.ASN1OctetString) DERBMPString(org.bouncycastle.asn1.DERBMPString) BERConstructedOctetString(org.bouncycastle.asn1.BERConstructedOctetString) DEROctetString(org.bouncycastle.asn1.DEROctetString) Cipher(javax.crypto.Cipher) IOException(java.io.IOException) SecretKeyFactory(javax.crypto.SecretKeyFactory) PBEParameterSpec(javax.crypto.spec.PBEParameterSpec) KeyStoreException(java.security.KeyStoreException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) CertificateEncodingException(java.security.cert.CertificateEncodingException) UnrecoverableKeyException(java.security.UnrecoverableKeyException) IOException(java.io.IOException) CertificateException(java.security.cert.CertificateException)

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