AES(CBC模式)加密(将MD5加密后的密码再次加密)

/**
    * @author 将MD5加密后的密码使用AES(CBC模式)对称加密
    * @date 2019/4/24
    * @Param [password, apiSecret]
    * @return java.lang.String
    */
    public static String getAesEncrypt(String password,String apiSecret)throws Exception{
        String key = apiSecret.substring(0,16);
        String iv = apiSecret.substring(16);
        String md5Pass = getMd5(password);
        System.out.println("MD5加密后的密码结果:"+md5Pass);
        SecretKeySpec keyspec = new SecretKeySpec(key.getBytes("UTF-8"), "AES");
        IvParameterSpec ivspec = new IvParameterSpec(iv.getBytes("UTF-8"));
        Cipher cipher = Cipher.getInstance("AES/CBC/NoPadding");
        cipher.init(Cipher.ENCRYPT_MODE, keyspec, ivspec);
        byte[] encrypted = cipher.doFinal(md5Pass.getBytes("UTF-8"));
        return bytesToHexString(encrypted);

    }
    /**
     * @author 将AES加密后的结果二进制转换成16进制
     * @date 2019/4/24
     * @Param [bs]
     * @return java.lang.String
     */
    public static String bytesToHexString(byte[] bs) {
        StringBuffer sb = new StringBuffer();
        String hex = "";
        for (int i = 0; i < bs.length; i++) {
            hex = Integer.toHexString(bs[i] & 0xFF);
            if (hex.length() == 1) {
                hex = '0' + hex;
            }
            sb.append(hex);
        }
        System.out.println("AES加密转码后结果:"+sb);
        return sb.toString();
    }

 

你可能感兴趣的:(java)