Search in sources :

Example 6 with SecretKey

use of javax.crypto.SecretKey in project UltimateAndroid by cymcsg.

the class TripleDES method decrypt.

/**
     * Decrypt the message with TripleDES
     *
     * @param message
     * @return
     * @throws Exception
     */
public static String decrypt(String message) throws Exception {
    if (message == null || message == "")
        return "";
    byte[] values = Base64decoding(message, 0);
    final MessageDigest md = MessageDigest.getInstance("SHA-1");
    final byte[] digestOfPassword = md.digest(token.getBytes("utf-8"));
    final byte[] keyBytes = copyOf(digestOfPassword, 24);
    for (int j = 0, k = 16; j < 8; ) {
        keyBytes[k++] = keyBytes[j++];
    }
    final SecretKey key = new SecretKeySpec(keyBytes, "DESede");
    String s1 = "12345678";
    byte[] bytes = s1.getBytes();
    final IvParameterSpec iv = new IvParameterSpec(bytes);
    final Cipher decipher = Cipher.getInstance("DESede/CBC/PKCS5Padding");
    decipher.init(Cipher.DECRYPT_MODE, key, iv);
    final byte[] plainText = decipher.doFinal(values);
    return new String(plainText, "UTF-8");
}
Also used : SecretKey(javax.crypto.SecretKey) SecretKeySpec(javax.crypto.spec.SecretKeySpec) IvParameterSpec(javax.crypto.spec.IvParameterSpec) Cipher(javax.crypto.Cipher) MessageDigest(java.security.MessageDigest)

Example 7 with SecretKey

use of javax.crypto.SecretKey in project UltimateAndroid by cymcsg.

the class TripleDES method decrypt.

/**
     * Decrypt the message with TripleDES
     *
     * @param message
     * @return
     * @throws Exception
     */
public static String decrypt(byte[] message) throws Exception {
    byte[] values = Base64decodingByte(message, 0);
    final MessageDigest md = MessageDigest.getInstance("SHA-1");
    final byte[] digestOfPassword = md.digest(token.getBytes("utf-8"));
    final byte[] keyBytes = copyOf(digestOfPassword, 24);
    for (int j = 0, k = 16; j < 8; ) {
        keyBytes[k++] = keyBytes[j++];
    }
    final SecretKey key = new SecretKeySpec(keyBytes, "DESede");
    String s1 = "12345678";
    byte[] bytes = s1.getBytes();
    final IvParameterSpec iv = new IvParameterSpec(bytes);
    final Cipher decipher = Cipher.getInstance("DESede/CBC/PKCS5Padding");
    decipher.init(Cipher.DECRYPT_MODE, key, iv);
    final byte[] plainText = decipher.doFinal(values);
    return new String(plainText, "UTF-8");
}
Also used : SecretKey(javax.crypto.SecretKey) SecretKeySpec(javax.crypto.spec.SecretKeySpec) IvParameterSpec(javax.crypto.spec.IvParameterSpec) Cipher(javax.crypto.Cipher) MessageDigest(java.security.MessageDigest)

Example 8 with SecretKey

use of javax.crypto.SecretKey in project UltimateAndroid by cymcsg.

the class TripleDES method decrypt.

/**
     * Decrypt the message with TripleDES
     *
     * @param message
     * @return
     * @throws Exception
     */
public static String decrypt(String message) throws Exception {
    if (message == null || message == "")
        return "";
    byte[] values = Base64decoding(message, 0);
    final MessageDigest md = MessageDigest.getInstance("SHA-1");
    final byte[] digestOfPassword = md.digest(token.getBytes("utf-8"));
    final byte[] keyBytes = copyOf(digestOfPassword, 24);
    for (int j = 0, k = 16; j < 8; ) {
        keyBytes[k++] = keyBytes[j++];
    }
    final SecretKey key = new SecretKeySpec(keyBytes, "DESede");
    String s1 = "12345678";
    byte[] bytes = s1.getBytes();
    final IvParameterSpec iv = new IvParameterSpec(bytes);
    final Cipher decipher = Cipher.getInstance("DESede/CBC/PKCS5Padding");
    decipher.init(Cipher.DECRYPT_MODE, key, iv);
    final byte[] plainText = decipher.doFinal(values);
    return new String(plainText, "UTF-8");
}
Also used : SecretKey(javax.crypto.SecretKey) SecretKeySpec(javax.crypto.spec.SecretKeySpec) IvParameterSpec(javax.crypto.spec.IvParameterSpec) Cipher(javax.crypto.Cipher) MessageDigest(java.security.MessageDigest)

Example 9 with SecretKey

use of javax.crypto.SecretKey in project android_frameworks_base by ParanoidAndroid.

the class Pm method runInstall.

private void runInstall() {
    int installFlags = PackageManager.INSTALL_ALL_USERS;
    String installerPackageName = null;
    String opt;
    String algo = null;
    byte[] iv = null;
    byte[] key = null;
    String macAlgo = null;
    byte[] macKey = null;
    byte[] tag = null;
    String originatingUriString = null;
    String referrer = null;
    while ((opt = nextOption()) != null) {
        if (opt.equals("-l")) {
            installFlags |= PackageManager.INSTALL_FORWARD_LOCK;
        } else if (opt.equals("-r")) {
            installFlags |= PackageManager.INSTALL_REPLACE_EXISTING;
        } else if (opt.equals("-i")) {
            installerPackageName = nextOptionData();
            if (installerPackageName == null) {
                System.err.println("Error: no value specified for -i");
                return;
            }
        } else if (opt.equals("-t")) {
            installFlags |= PackageManager.INSTALL_ALLOW_TEST;
        } else if (opt.equals("-s")) {
            // Override if -s option is specified.
            installFlags |= PackageManager.INSTALL_EXTERNAL;
        } else if (opt.equals("-f")) {
            // Override if -s option is specified.
            installFlags |= PackageManager.INSTALL_INTERNAL;
        } else if (opt.equals("-d")) {
            installFlags |= PackageManager.INSTALL_ALLOW_DOWNGRADE;
        } else if (opt.equals("--algo")) {
            algo = nextOptionData();
            if (algo == null) {
                System.err.println("Error: must supply argument for --algo");
                return;
            }
        } else if (opt.equals("--iv")) {
            iv = hexToBytes(nextOptionData());
            if (iv == null) {
                System.err.println("Error: must supply argument for --iv");
                return;
            }
        } else if (opt.equals("--key")) {
            key = hexToBytes(nextOptionData());
            if (key == null) {
                System.err.println("Error: must supply argument for --key");
                return;
            }
        } else if (opt.equals("--macalgo")) {
            macAlgo = nextOptionData();
            if (macAlgo == null) {
                System.err.println("Error: must supply argument for --macalgo");
                return;
            }
        } else if (opt.equals("--mackey")) {
            macKey = hexToBytes(nextOptionData());
            if (macKey == null) {
                System.err.println("Error: must supply argument for --mackey");
                return;
            }
        } else if (opt.equals("--tag")) {
            tag = hexToBytes(nextOptionData());
            if (tag == null) {
                System.err.println("Error: must supply argument for --tag");
                return;
            }
        } else if (opt.equals("--originating-uri")) {
            originatingUriString = nextOptionData();
            if (originatingUriString == null) {
                System.err.println("Error: must supply argument for --originating-uri");
                return;
            }
        } else if (opt.equals("--referrer")) {
            referrer = nextOptionData();
            if (referrer == null) {
                System.err.println("Error: must supply argument for --referrer");
                return;
            }
        } else {
            System.err.println("Error: Unknown option: " + opt);
            return;
        }
    }
    final ContainerEncryptionParams encryptionParams;
    if (algo != null || iv != null || key != null || macAlgo != null || macKey != null || tag != null) {
        if (algo == null || iv == null || key == null) {
            System.err.println("Error: all of --algo, --iv, and --key must be specified");
            return;
        }
        if (macAlgo != null || macKey != null || tag != null) {
            if (macAlgo == null || macKey == null || tag == null) {
                System.err.println("Error: all of --macalgo, --mackey, and --tag must " + "be specified");
                return;
            }
        }
        try {
            final SecretKey encKey = new SecretKeySpec(key, "RAW");
            final SecretKey macSecretKey;
            if (macKey == null || macKey.length == 0) {
                macSecretKey = null;
            } else {
                macSecretKey = new SecretKeySpec(macKey, "RAW");
            }
            encryptionParams = new ContainerEncryptionParams(algo, new IvParameterSpec(iv), encKey, macAlgo, null, macSecretKey, tag, -1, -1, -1);
        } catch (InvalidAlgorithmParameterException e) {
            e.printStackTrace();
            return;
        }
    } else {
        encryptionParams = null;
    }
    final Uri apkURI;
    final Uri verificationURI;
    final Uri originatingURI;
    final Uri referrerURI;
    if (originatingUriString != null) {
        originatingURI = Uri.parse(originatingUriString);
    } else {
        originatingURI = null;
    }
    if (referrer != null) {
        referrerURI = Uri.parse(referrer);
    } else {
        referrerURI = null;
    }
    // Populate apkURI, must be present
    final String apkFilePath = nextArg();
    System.err.println("\tpkg: " + apkFilePath);
    if (apkFilePath != null) {
        apkURI = Uri.fromFile(new File(apkFilePath));
    } else {
        System.err.println("Error: no package specified");
        return;
    }
    // Populate verificationURI, optionally present
    final String verificationFilePath = nextArg();
    if (verificationFilePath != null) {
        System.err.println("\tver: " + verificationFilePath);
        verificationURI = Uri.fromFile(new File(verificationFilePath));
    } else {
        verificationURI = null;
    }
    PackageInstallObserver obs = new PackageInstallObserver();
    try {
        VerificationParams verificationParams = new VerificationParams(verificationURI, originatingURI, referrerURI, VerificationParams.NO_UID, null);
        mPm.installPackageWithVerificationAndEncryption(apkURI, obs, installFlags, installerPackageName, verificationParams, encryptionParams);
        synchronized (obs) {
            while (!obs.finished) {
                try {
                    obs.wait();
                } catch (InterruptedException e) {
                }
            }
            if (obs.result == PackageManager.INSTALL_SUCCEEDED) {
                System.out.println("Success");
            } else {
                System.err.println("Failure [" + installFailureToString(obs.result) + "]");
            }
        }
    } catch (RemoteException e) {
        System.err.println(e.toString());
        System.err.println(PM_NOT_RUNNING_ERR);
    }
}
Also used : InvalidAlgorithmParameterException(java.security.InvalidAlgorithmParameterException) ContainerEncryptionParams(android.content.pm.ContainerEncryptionParams) VerificationParams(android.content.pm.VerificationParams) Uri(android.net.Uri) SecretKey(javax.crypto.SecretKey) SecretKeySpec(javax.crypto.spec.SecretKeySpec) IvParameterSpec(javax.crypto.spec.IvParameterSpec) RemoteException(android.os.RemoteException) File(java.io.File) IPackageInstallObserver(android.content.pm.IPackageInstallObserver)

Example 10 with SecretKey

use of javax.crypto.SecretKey in project LolliPin by OrangeGangsters.

the class FingerprintUiHelper method initCipher.

/**
     * Initialize the {@link Cipher} instance with the created key in the {@link #createKey()}
     * method.
     *
     * @return {@code true} if initialization is successful, {@code false} if the lock screen has
     * been disabled or reset after the key was generated, or if a fingerprint got enrolled after
     * the key was generated.
     */
private boolean initCipher() {
    try {
        if (mKeyStore == null) {
            mKeyStore = KeyStore.getInstance("AndroidKeyStore");
        }
        createKey();
        mKeyStore.load(null);
        SecretKey key = (SecretKey) mKeyStore.getKey(KEY_NAME, null);
        mCipher = Cipher.getInstance("AES/CBC/PKCS7Padding");
        mCipher.init(Cipher.ENCRYPT_MODE, key);
        return true;
    } catch (NoSuchPaddingException | KeyStoreException | CertificateException | UnrecoverableKeyException | IOException | NoSuchAlgorithmException | InvalidKeyException e) {
        return false;
    }
}
Also used : SecretKey(javax.crypto.SecretKey) UnrecoverableKeyException(java.security.UnrecoverableKeyException) NoSuchPaddingException(javax.crypto.NoSuchPaddingException) CertificateException(java.security.cert.CertificateException) KeyStoreException(java.security.KeyStoreException) IOException(java.io.IOException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) InvalidKeyException(java.security.InvalidKeyException)

Aggregations

SecretKey (javax.crypto.SecretKey)437 Cipher (javax.crypto.Cipher)160 SecretKeySpec (javax.crypto.spec.SecretKeySpec)127 KeyGenerator (javax.crypto.KeyGenerator)112 SecretKeyFactory (javax.crypto.SecretKeyFactory)83 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)78 SecureRandom (java.security.SecureRandom)58 InvalidKeyException (java.security.InvalidKeyException)57 PBEKeySpec (javax.crypto.spec.PBEKeySpec)53 IvParameterSpec (javax.crypto.spec.IvParameterSpec)42 IOException (java.io.IOException)41 PBEParameterSpec (javax.crypto.spec.PBEParameterSpec)34 NoSuchPaddingException (javax.crypto.NoSuchPaddingException)32 KeyStore (java.security.KeyStore)30 IllegalBlockSizeException (javax.crypto.IllegalBlockSizeException)30 Test (org.junit.Test)30 BadPaddingException (javax.crypto.BadPaddingException)29 PrivateKey (java.security.PrivateKey)28 Mac (javax.crypto.Mac)28 GeneralSecurityException (java.security.GeneralSecurityException)26