AES encryption (AES/ECB/PKCS5Padding) key UTF8 takes the first sixteen bytes

/**
     * 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");// "Algorithm/Mode/Complement Mode"
        cipher.init(Cipher.ENCRYPT_MODE, skeySpec);
        byte[] encrypted = cipher.doFinal(sSrc.getBytes ("utf-8"));

        return new Base64().encodeToString(encrypted);// BASE64 is used here for transcoding

    }


sSrc parameter that needs to be encrypted



Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325556403&siteId=291194637