AES加密(AES/ECB/PKCS5Padding)key UTF8 取前十六個字節

版權聲明:本文爲博主原創文章,未經博主允許不得轉載。 https://blog.csdn.net/qq_35180232/article/details/78204167
/**
     * 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  需要加密的參數



發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章