Search in sources :

Example 56 with Cipher

use of javax.crypto.Cipher 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 57 with Cipher

use of javax.crypto.Cipher 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 58 with Cipher

use of javax.crypto.Cipher in project android_frameworks_base by AOSPA.

the class CryptoHelper method encryptBundle.

@NonNull
/* default */
Bundle encryptBundle(@NonNull Bundle bundle) throws GeneralSecurityException {
    Preconditions.checkNotNull(bundle, "Cannot encrypt null bundle.");
    Parcel parcel = Parcel.obtain();
    bundle.writeToParcel(parcel, 0);
    byte[] clearBytes = parcel.marshall();
    parcel.recycle();
    Cipher cipher = Cipher.getInstance(CIPHER_ALGORITHM);
    cipher.init(Cipher.ENCRYPT_MODE, mEncryptionKey);
    byte[] encryptedBytes = cipher.doFinal(clearBytes);
    byte[] iv = cipher.getIV();
    byte[] mac = createMac(encryptedBytes, iv);
    Bundle encryptedBundle = new Bundle();
    encryptedBundle.putByteArray(KEY_CIPHER, encryptedBytes);
    encryptedBundle.putByteArray(KEY_MAC, mac);
    encryptedBundle.putByteArray(KEY_IV, iv);
    return encryptedBundle;
}
Also used : Parcel(android.os.Parcel) Bundle(android.os.Bundle) Cipher(javax.crypto.Cipher) NonNull(android.annotation.NonNull)

Example 59 with Cipher

use of javax.crypto.Cipher in project android_frameworks_base by AOSPA.

the class CryptoHelper method decryptBundle.

@Nullable
/* default */
Bundle decryptBundle(@NonNull Bundle bundle) throws GeneralSecurityException {
    Preconditions.checkNotNull(bundle, "Cannot decrypt null bundle.");
    byte[] iv = bundle.getByteArray(KEY_IV);
    byte[] encryptedBytes = bundle.getByteArray(KEY_CIPHER);
    byte[] mac = bundle.getByteArray(KEY_MAC);
    if (!verifyMac(encryptedBytes, iv, mac)) {
        Log.w(TAG, "Escrow mac mismatched!");
        return null;
    }
    IvParameterSpec ivSpec = new IvParameterSpec(iv);
    Cipher cipher = Cipher.getInstance(CIPHER_ALGORITHM);
    cipher.init(Cipher.DECRYPT_MODE, mEncryptionKey, ivSpec);
    byte[] decryptedBytes = cipher.doFinal(encryptedBytes);
    Parcel decryptedParcel = Parcel.obtain();
    decryptedParcel.unmarshall(decryptedBytes, 0, decryptedBytes.length);
    decryptedParcel.setDataPosition(0);
    Bundle decryptedBundle = new Bundle();
    decryptedBundle.readFromParcel(decryptedParcel);
    decryptedParcel.recycle();
    return decryptedBundle;
}
Also used : Parcel(android.os.Parcel) Bundle(android.os.Bundle) IvParameterSpec(javax.crypto.spec.IvParameterSpec) Cipher(javax.crypto.Cipher) Nullable(android.annotation.Nullable)

Example 60 with Cipher

use of javax.crypto.Cipher in project intellij-community by JetBrains.

the class PropertiesEncryptionSupport method encrypt.

private static byte[] encrypt(byte[] msgBytes, Key key) throws Exception {
    final Cipher ciph = Cipher.getInstance("AES/CBC/PKCS5Padding");
    ciph.init(Cipher.ENCRYPT_MODE, key);
    final byte[] body = ciph.doFinal(msgBytes);
    final byte[] iv = ciph.getIV();
    final byte[] data = new byte[4 + iv.length + body.length];
    final int length = body.length;
    data[0] = (byte) ((length >> 24) & 0xFF);
    data[1] = (byte) ((length >> 16) & 0xFF);
    data[2] = (byte) ((length >> 8) & 0xFF);
    data[3] = (byte) (length & 0xFF);
    System.arraycopy(iv, 0, data, 4, iv.length);
    System.arraycopy(body, 0, data, 4 + iv.length, body.length);
    return data;
}
Also used : Cipher(javax.crypto.Cipher)

Aggregations

Cipher (javax.crypto.Cipher)1531 SecretKeySpec (javax.crypto.spec.SecretKeySpec)516 SecretKey (javax.crypto.SecretKey)377 IvParameterSpec (javax.crypto.spec.IvParameterSpec)368 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)349 InvalidKeyException (java.security.InvalidKeyException)257 IOException (java.io.IOException)202 NoSuchPaddingException (javax.crypto.NoSuchPaddingException)202 IllegalBlockSizeException (javax.crypto.IllegalBlockSizeException)196 BadPaddingException (javax.crypto.BadPaddingException)183 GeneralSecurityException (java.security.GeneralSecurityException)161 SecretKeyFactory (javax.crypto.SecretKeyFactory)149 Key (java.security.Key)148 InvalidAlgorithmParameterException (java.security.InvalidAlgorithmParameterException)134 SecureRandom (java.security.SecureRandom)105 PrivateKey (java.security.PrivateKey)93 PublicKey (java.security.PublicKey)86 UnsupportedEncodingException (java.io.UnsupportedEncodingException)84 PBEKeySpec (javax.crypto.spec.PBEKeySpec)82 KeyGenerator (javax.crypto.KeyGenerator)78