AES加密(AES/ECB/PKCS5Padding)key UTF8 取前十六个字节

/**
     * AES加密(AES/ECB/PKCS5Padding)key UTF8 取前十六个字节
     *
     * @param str
     * @return 加密后base64字符串
     */

    public static String getAESdata(String sSrc) throws Exception {
        if (sKey == null) {
            return null;
        }
        byte[] raw = new byte[16];
        byte[] bytekeys = sKey.getBytes("utf-8");
        int iv = bytekeys.length;
        if (iv > 16)
            iv = 16;
        System.arraycopy(bytekeys, 0, raw, 0, iv);
        SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES");
        Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");// "算法/模式/补码方式"
        cipher.init(Cipher.ENCRYPT_MODE, skeySpec);
        byte[] encrypted = cipher.doFinal(sSrc.getBytes("utf-8"));

        return new Base64().encodeToString(encrypted);// 此处使用BASE64做转码功能

    }


sSrc  需要加密的参数



猜你喜欢

转载自blog.csdn.net/qq_35180232/article/details/78204167
今日推荐