文本的md5及AES加密(Android环境)

1.先来md5。
    public static String key2Md5(String _key){
        byte[] hash;
        try {
            hash = MessageDigest.getInstance("MD5").digest(_key.getBytes("UTF-8"));
            StringBuilder hex = new StringBuilder(hash.length * 2);
            for (byte b : hash) {
                if ((b & 0xFF) < 0x10) hex.append("0");
                hex.append(Integer.toHexString(b & 0xFF));
            }
            Log.d("LOGCAT","_key:"+_key+"-"+hex.toString());
            return hex.toString();
        }catch (NoSuchAlgorithmException e) {
            return null;
        }catch (UnsupportedEncodingException e) {
            return null;
        }
    }

这样就可以在不记录原始字符串的情况下做校验了。只要记录md5码就行了。而md5又是不可逆的,保护了原始的字符串。

2.接下来是AES,这个稍稍复杂点。

先来加密:

    /**
     * 加密
     * @param _seed 加密用的口令
     * @param _info 原文本信息
     * @return
     */
    public static String encodeWords(String _seed,String _info){
        Log.d("LOGCAT", "加密前的seed=" + _seed + ",内容为:" + _info);
        byte[] result = null;
        StringBuffer sb = new StringBuffer(16);
        sb.append(_seed);
        while (sb.length() < 16) {//需要补足位数
            sb.append("0");
        }
        try {
            result = encrypt(sb.toString().getBytes(), _info.getBytes());
        } catch (Exception e) {
            e.printStackTrace();
        }
        String content = toHex(result);
        Log.d("LOGCAT", "加密后的内容为:" + content);
        return content;
    }

    private static byte[] encrypt(byte[] raw, byte[] clear) throws Exception {
        SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES");
        Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
        cipher.init(Cipher.ENCRYPT_MODE, skeySpec, new IvParameterSpec(new byte[cipher.getBlockSize()]));
        byte[] encrypted = cipher.doFinal(clear);
        return encrypted;
    }

    public static String toHex(byte[] buf) {
        if (buf == null)
            return "";
        StringBuffer result = new StringBuffer(2 * buf.length);
        for (int i = 0; i < buf.length; i++) {
            appendHex(result, buf[i]);
        }
        return result.toString();
    }

    private static void appendHex(StringBuffer sb, byte b) {
        final String HEX = "0123456789ABCDEF";
        sb.append(HEX.charAt((b >> 4) & 0x0f)).append(HEX.charAt(b & 0x0f));
    }

AES是可逆的,所以接下来是解密:

    public static String decodeWords(String seed,String _info){
        Log.d("LOGCAT", "解密前的seed=" + seed + ",内容为:" + _info);
        StringBuffer sb = new StringBuffer(16);
        sb.append(seed);
        while (sb.length() < 16) {
            sb.append("0");
        }
        try {
            byte[] enc = toByte(_info);
            byte[] result = decrypt(sb.toString().getBytes(), enc);
            String coentn = new String(result);
            Log.d("LOGCAT", "解密后的内容为:" + coentn);
            return coentn;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    private static byte[] decrypt(byte[] raw, byte[] encrypted) throws Exception {
        SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES");
//        Cipher cipher = Cipher.getInstance("AES");
        Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
        cipher.init(Cipher.DECRYPT_MODE, skeySpec, new IvParameterSpec(new byte[cipher.getBlockSize()]));
        byte[] decrypted = cipher.doFinal(encrypted);
        return decrypted;
    }

    public static byte[] toByte(String hexString) {
        int len = hexString.length() / 2;
        byte[] result = new byte[len];
        for (int i = 0; i < len; i++)
            result[i] = Integer.valueOf(hexString.substring(2 * i, 2 * i + 2),16).byteValue();
        return result;
    }

对于加密的认识还很粗浅,感觉深入下去的话又是个大坑。
相关项目github:password-note

你可能感兴趣的:(文本的md5及AES加密(Android环境))