Search in sources :

Example 41 with Cipher

use of javax.crypto.Cipher in project XobotOS by xamarin.

the class PEMUtilities method crypt.

static byte[] crypt(boolean encrypt, Provider provider, byte[] bytes, char[] password, String dekAlgName, byte[] iv) throws IOException {
    AlgorithmParameterSpec paramSpec = new IvParameterSpec(iv);
    String alg;
    String blockMode = "CBC";
    String padding = "PKCS5Padding";
    Key sKey;
    // Figure out block mode and padding.
    if (dekAlgName.endsWith("-CFB")) {
        blockMode = "CFB";
        padding = "NoPadding";
    }
    if (dekAlgName.endsWith("-ECB") || "DES-EDE".equals(dekAlgName) || "DES-EDE3".equals(dekAlgName)) {
        // ECB is actually the default (though seldom used) when OpenSSL
        // uses DES-EDE (des2) or DES-EDE3 (des3).
        blockMode = "ECB";
        paramSpec = null;
    }
    if (dekAlgName.endsWith("-OFB")) {
        blockMode = "OFB";
        padding = "NoPadding";
    }
    // Figure out algorithm and key size.
    if (dekAlgName.startsWith("DES-EDE")) {
        alg = "DESede";
        // "DES-EDE" is actually des2 in OpenSSL-speak!
        // "DES-EDE3" is des3.
        boolean des2 = !dekAlgName.startsWith("DES-EDE3");
        sKey = getKey(password, alg, 24, iv, des2);
    } else if (dekAlgName.startsWith("DES-")) {
        alg = "DES";
        sKey = getKey(password, alg, 8, iv);
    } else if (dekAlgName.startsWith("BF-")) {
        alg = "Blowfish";
        sKey = getKey(password, alg, 16, iv);
    } else if (dekAlgName.startsWith("RC2-")) {
        alg = "RC2";
        int keyBits = 128;
        if (dekAlgName.startsWith("RC2-40-")) {
            keyBits = 40;
        } else if (dekAlgName.startsWith("RC2-64-")) {
            keyBits = 64;
        }
        sKey = getKey(password, alg, keyBits / 8, iv);
        if (// ECB block mode
        paramSpec == null) {
            paramSpec = new RC2ParameterSpec(keyBits);
        } else {
            paramSpec = new RC2ParameterSpec(keyBits, iv);
        }
    } else if (dekAlgName.startsWith("AES-")) {
        alg = "AES";
        byte[] salt = iv;
        if (salt.length > 8) {
            salt = new byte[8];
            System.arraycopy(iv, 0, salt, 0, 8);
        }
        int keyBits;
        if (dekAlgName.startsWith("AES-128-")) {
            keyBits = 128;
        } else if (dekAlgName.startsWith("AES-192-")) {
            keyBits = 192;
        } else if (dekAlgName.startsWith("AES-256-")) {
            keyBits = 256;
        } else {
            throw new EncryptionException("unknown AES encryption with private key");
        }
        sKey = getKey(password, "AES", keyBits / 8, salt);
    } else {
        throw new EncryptionException("unknown encryption with private key");
    }
    String transformation = alg + "/" + blockMode + "/" + padding;
    try {
        Cipher c = Cipher.getInstance(transformation, provider);
        int mode = encrypt ? Cipher.ENCRYPT_MODE : Cipher.DECRYPT_MODE;
        if (// ECB block mode
        paramSpec == null) {
            c.init(mode, sKey);
        } else {
            c.init(mode, sKey, paramSpec);
        }
        return c.doFinal(bytes);
    } catch (Exception e) {
        throw new EncryptionException("exception using cipher - please check password and data.", e);
    }
}
Also used : IvParameterSpec(javax.crypto.spec.IvParameterSpec) RC2ParameterSpec(javax.crypto.spec.RC2ParameterSpec) Cipher(javax.crypto.Cipher) AlgorithmParameterSpec(java.security.spec.AlgorithmParameterSpec) Key(java.security.Key) SecretKey(javax.crypto.SecretKey) IOException(java.io.IOException)

Example 42 with Cipher

use of javax.crypto.Cipher in project ZI by yixia.

the class Crypto method rsaEncrypt.

public String rsaEncrypt(InputStream keyStream, byte[] data) {
    try {
        PublicKey pubKey = readKeyFromStream(keyStream);
        Cipher cipher = Cipher.getInstance("RSA/ECB/NoPadding");
        cipher.init(Cipher.ENCRYPT_MODE, pubKey);
        byte[] cipherData = cipher.doFinal(data);
        return Base64.encodeToString(cipherData, Base64.NO_WRAP);
    } catch (Exception e) {
        Log.e("rsaEncrypt", e);
        return "";
    }
}
Also used : PublicKey(java.security.PublicKey) Cipher(javax.crypto.Cipher) IOException(java.io.IOException) UnsupportedEncodingException(java.io.UnsupportedEncodingException)

Example 43 with Cipher

use of javax.crypto.Cipher in project WordPress-Android by wordpress-mobile.

the class WPLegacyMigrationUtils method decryptPassword.

private static String decryptPassword(String encryptedPwd) {
    try {
        DESKeySpec keySpec = new DESKeySpec(DEPRECATED_DB_PASSWORD_SECRET.getBytes("UTF-8"));
        SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
        SecretKey key = keyFactory.generateSecret(keySpec);
        byte[] encryptedWithoutB64 = Base64.decode(encryptedPwd, Base64.DEFAULT);
        Cipher cipher = Cipher.getInstance("DES");
        cipher.init(Cipher.DECRYPT_MODE, key);
        byte[] plainTextPwdBytes = cipher.doFinal(encryptedWithoutB64);
        return new String(plainTextPwdBytes);
    } catch (Exception e) {
    }
    return encryptedPwd;
}
Also used : SecretKey(javax.crypto.SecretKey) DESKeySpec(javax.crypto.spec.DESKeySpec) Cipher(javax.crypto.Cipher) SecretKeyFactory(javax.crypto.SecretKeyFactory) SQLException(android.database.SQLException)

Example 44 with Cipher

use of javax.crypto.Cipher in project translationstudio8 by heartsome.

the class InstallKeyEncrypt method decrypt.

public static byte[] decrypt(byte[] srcBytes) throws Exception {
    PKCS8EncodedKeySpec keySpec = new PKCS8EncodedKeySpec(privateKey);
    KeyFactory kf = KeyFactory.getInstance(algorithm);
    PrivateKey keyPrivate = kf.generatePrivate(keySpec);
    Cipher cipher = Cipher.getInstance(algorithm, new org.bouncycastle.jce.provider.BouncyCastleProvider());
    cipher.init(Cipher.DECRYPT_MODE, keyPrivate);
    int blockSize = cipher.getBlockSize();
    ByteArrayOutputStream bout = new ByteArrayOutputStream(blockSize);
    int j = 0;
    while (srcBytes.length - j * blockSize > 0) {
        byte[] temp = cipher.doFinal(srcBytes, j * blockSize, blockSize);
        bout.write(temp);
        j++;
    }
    return bout.toByteArray();
}
Also used : PrivateKey(java.security.PrivateKey) PKCS8EncodedKeySpec(java.security.spec.PKCS8EncodedKeySpec) Cipher(javax.crypto.Cipher) ByteArrayOutputStream(java.io.ByteArrayOutputStream) KeyFactory(java.security.KeyFactory)

Example 45 with Cipher

use of javax.crypto.Cipher in project platformlayer by platformlayer.

the class AesCbcUtils method buildDecryptor.

public static Cipher buildDecryptor(SecretKey key) {
    Cipher cipher = getCipher();
    CryptoUtils.initDecrypt(cipher, key);
    return cipher;
}
Also used : Cipher(javax.crypto.Cipher)

Aggregations

Cipher (javax.crypto.Cipher)700 SecretKeySpec (javax.crypto.spec.SecretKeySpec)193 SecretKey (javax.crypto.SecretKey)183 IvParameterSpec (javax.crypto.spec.IvParameterSpec)150 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)126 InvalidKeyException (java.security.InvalidKeyException)84 IOException (java.io.IOException)79 SecretKeyFactory (javax.crypto.SecretKeyFactory)76 GeneralSecurityException (java.security.GeneralSecurityException)69 IllegalBlockSizeException (javax.crypto.IllegalBlockSizeException)65 NoSuchPaddingException (javax.crypto.NoSuchPaddingException)62 Key (java.security.Key)60 BadPaddingException (javax.crypto.BadPaddingException)57 SecureRandom (java.security.SecureRandom)45 KeyGenerator (javax.crypto.KeyGenerator)45 UnsupportedEncodingException (java.io.UnsupportedEncodingException)43 PrivateKey (java.security.PrivateKey)43 PublicKey (java.security.PublicKey)41 PBEKeySpec (javax.crypto.spec.PBEKeySpec)38 AlgorithmParameterSpec (java.security.spec.AlgorithmParameterSpec)36