Android AES Decrypt decryptHex(String content, String username, String password)

Here you can find the source of decryptHex(String content, String username, String password)

Description

decrypt Hex

Declaration

public static String decryptHex(String content, String username,
            String password) 

Method Source Code

//package com.java2s;
import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import javax.crypto.Cipher;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;

public class Main {
    private static final String UTF_8 = "UTF-8";
    private static final byte[] defaultIV = { 127, 24, 123, 23, 93, 7, 15,
            0, 9, 4, 8, 15, 16, 23, 42, 1 };

    public static String decryptHex(String content, String key) {
        try {//from w ww.ja v a 2  s  . c om
            byte[] contentBytes = hexDecode(content);
            byte[] result = decrypt(contentBytes, key);
            return new String(result, UTF_8);
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        return null;
    }

    public static String decryptHex(String content, String username,
            String password) {
        return decryptHex(content, password + username);
    }

    public static byte[] hexDecode(String data) {
        byte[] r = new byte[data.length() / 2];

        char[] a = data.toCharArray();

        for (int i = 0; i < a.length; i += 2) {
            char c1 = a[i], c2 = a[i + 1];
            int v1 = valueOf(c1);
            int v2 = valueOf(c2);
            r[i >> 1] = (byte) ((((v1 & 0xf) << 4) | (v2 & 0xf)) & 0xff);
        }

        return r;
    }

    public static byte[] decrypt(byte[] content, String key) {
        try {
            Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
            IvParameterSpec ivs = new IvParameterSpec(defaultIV);
            cipher.init(Cipher.DECRYPT_MODE, getSecretKey(key), ivs);

            return cipher.doFinal(content);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    private static int valueOf(char c) {
        c = Character.toLowerCase(c);
        if (c >= '0' && c <= '9') {
            return c - '0';
        } else if (c >= 'a' && c <= 'f') {
            return c - 'a' + 10;
        }
        return -1;
    }

    private static SecretKeySpec getSecretKey(String key) {
        MessageDigest digest;
        try {
            digest = MessageDigest.getInstance("md5");
            SecretKeySpec keySpec = new SecretKeySpec(digest.digest(key
                    .getBytes(UTF_8)), "AES");
            return keySpec;
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        return null;
    }
}

Related

  1. decryptBase64Text(String key, String src)
  2. decryptBytes(String key, byte[] src)
  3. decryptBytes(String seed, byte[] encrypted)
  4. decryptBytes(byte[] data, byte[] key)
  5. decryptHex(String content, String key)
  6. decryptNumberWithAES(String encrypted)
  7. decryptUrlDecode(String dataPassword, String encrypted)
  8. aesDecode(String seed, String encrypted)
  9. decrypt(byte[] raw, byte[] encrypted)