【验证可用】java异或自定义加解密工具,没有第三方依赖,灵活扩展(排除MD5/RSA/SHA1/AES/DES)

需求背景

  • 不适使用第三方的加密工具MD5/RSA/SHA1/AES/DES,自定义一个可以加解密的工具类;
  • 除了JDK不引入其他三方包;

话不多说,上代码

1、字节数组加密(动态随机)

/**
     * 字节数组加密
     *
     * @param bytes 入参
     * @return 出参
     */
    public byte[] internalEncrypt(byte[] bytes) {
        //获取16以内的随机数
        Random random = new Random();
        int seed = random.nextInt(xorKey.length);
        //数组长度
        int len = bytes.length;
        //新数组
        byte[] newBytes = new byte[len + 5];
        //随机数放到新数组首位
        newBytes[0] = (byte) seed;
        //数据长度放到新数据前面
        convertByte(newBytes, len);

        //新数组初始下标
        int m = 5;
        //需要加密数据跟秘钥进行异或操作
        for (int i = 0; i < len; i++) {
            newBytes[i + m] = (byte) (bytes[i] ^ xorKey[seed % xorKey.length]);
            seed++;
        }
        return newBytes;
    }

2、字节数组解密

/**
     * 字节数组解密
     *
     * @param bytes 入参
     * @return 出参
     */
    public byte[] internalDecrypt(byte[] bytes) {
        if (bytes.length < 6) {
            return new byte[0];
        }
        //获取加密时生成的随机数
        int seed = bytes[0];
        //校验随机数
        if (seed > xorKey.length - 1) {
            return bytes;
        }
        //校验是否是加密数据,校验数据长度
        int len = convertInt(bytes);
        if (bytes.length != len + 5) {
            return bytes;
        }
        //反向异或解密
        byte[] newBytes = new byte[len];
        for (int i = 5; i < bytes.length; i++) {
            newBytes[i - 5] = (byte) (bytes[i] ^ xorKey[seed % xorKey.length]);
            seed++;
        }
        return newBytes;
    }

3、字符串加密&解密

/**
     * 字符串加密
     *
     * @param str 入参
     * @return 出参
     */
    public String encrypt(String str) {
        if (str == null || str.isEmpty()) {
            return "";
        }
        //字符串转byte
        byte[] strByte;
        byte[] newByte;
        try {
            strByte = str.getBytes("GBK");
        } catch (UnsupportedEncodingException e) {
            throw new RuntimeException(e);
        }
        newByte = internalEncrypt(strByte);
        return replaceBase64(newByte);
    }

    /**
     * 字符串解密
     *
     * @param str 入参
     * @return 出参
     */
    public String decrypt(String str) {
        if (str == null || str.isEmpty()) {
            return "";
        }
        //特殊字符替换
        str = strReplace(str);
        //base64解密
        Base64.Decoder decoder = Base64.getDecoder();
        byte[] bytes = decoder.decode(str);
        //字节数组解密
        byte[] newBytes = internalDecrypt(bytes);
        //获取原始数据
        try {
            return new String(newBytes, "GBK");
        } catch (UnsupportedEncodingException e) {
            throw new RuntimeException(e);
        }
    }

4、静态加密&解密

/**
     * 静态加密
     *
     * @param str 入参
     * @return 出参
     */
    public String staticEncrypt(String str) {
        byte[] bytes;
        try {
            bytes = str.getBytes("GBK");
        } catch (UnsupportedEncodingException e) {
            throw new RuntimeException(e);
        }
        //新数组
        byte[] newBytes = new byte[bytes.length];
        byte sumCode = 0;
        //异或操作
        for (int i = 0; i < bytes.length; i++) {
            int xorNum = bytes[i] ^ staticXorKey[i % staticXorKey.length];
            newBytes[i] = (byte) xorNum;
            if (i == 0) {
                sumCode = newBytes[i];
            } else {
                sumCode = (byte) (sumCode ^ newBytes[i]);
            }
        }
        //拼接字符
        StringBuilder sb = new StringBuilder();
        //以16进制存储(大写)
        sb.append(Integer.toHexString(sumCode & 0xFF).toUpperCase());
        for (byte b : newBytes) {
            // 将ASCII值转换为十六进制表示形式
            String xorStr = Integer.toHexString(b & 0xFF).toUpperCase();
            //不足两位,补全两位
            if (xorStr.length() < 2) {
                xorStr = "0" + xorStr;
            }
            sb.append(xorStr);
        }
        return sb.toString();
    }

    /**
     * 静态解密
     *
     * @param str 入参
     * @return 出参
     */
    public String staticDecrypt(String str) {
        if (str==null||str.isEmpty()) {
            return "";
        }
        //异或处理的字符串有个位数的解密会失败,直接返回原字符串
        if (str.length() % 2 != 0) {
            return str;
        }
        //校验是否为16进制字符
        for (char c : str.toCharArray()) {
            if (hExChar.indexOf(c) == -1) {
                return str;
            }
        }
        byte[] bytes = new byte[str.length() / 2];
        for (int i = 0; i < bytes.length; i++) {
            int seed = i * 2; // 每两位作为一个字节
            // 获取高四位的值
            int highNibble = Character.digit(str.charAt(seed), 16);
            // 获取低四位的值
            int lowNibble = Character.digit(str.charAt(seed + 1), 16);

            // 合并高低四位得到完整的byte值
            bytes[i] = (byte) ((highNibble << 4) | lowNibble);
        }

        byte sumCode = 0;
        for (int i = 1; i < bytes.length; i++) {
            if (i == 1) {
                sumCode = bytes[i];
            } else {
                sumCode = (byte) (sumCode ^ bytes[i]);
            }
        }
        if (sumCode != bytes[0]) {
            return str;
        }
        //如果str长度为4,且前两个字符和后两个字符相等,则不可能是加密后的数据(正常字符的B1异常或后不可能和原文相同)
        if (str.length() == 4 && str.substring(0, 2).equals(str.substring(2, 4))) {
            return str;
        }

        byte[] decodeBytes = new byte[bytes.length - 1];

        for (int i = 1; i < bytes.length; i++) {
            decodeBytes[i - 1] = (byte) (bytes[i] & 0xFF ^ staticXorKey[(i - 1) % staticXorKey.length]);
        }
        try {
            return new String(decodeBytes, "GBK");
        } catch (UnsupportedEncodingException e) {
            throw new RuntimeException(e);
        }
    }

5、私有方法

/**
     * base64特殊字符替换(正向)
     *
     * @param bytes 入参
     * @return 结果
     */
    private String replaceBase64(byte[] bytes) {
        Base64.Encoder encoder = Base64.getEncoder();

        bytes = encoder.encode(bytes);

        String str = new String(bytes, StandardCharsets.UTF_8);
        return str.replace("+", "-").replace("/", "_");
    }

    /**
     * base64特殊字符替换(反向)
     *
     * @param str 入参
     * @return 结果
     */
    private String strReplace(String str) {
        return str.replace("-", "+").replace("_", "/");
    }

    /**
     * 把int按位存入到byte数组中
     *
     * @param sourceByte byte数组
     * @param value      int值
     * @return 填充后的数组
     */
    private byte[] convertByte(byte[] sourceByte, int value) {
        //获取第1个byte赋值给数组 ,“>>>”“零扩展”:无论正负,都在高位插入0
        sourceByte[1] = (byte) (0);
        //获取第2个byte赋值给数组
        sourceByte[2] = (byte) (value >> 16 & 0xFF);
        //获取第3个byte赋值给数组
        sourceByte[3] = (byte) (value >> 8 & 0xFF);
        //获取第4个byte赋值给数组
        sourceByte[4] = (byte) (value & 0xFF);
        return sourceByte;
    }

    /**
     * byte数组转为int
     *
     * @param bytes 源byte数组
     * @return int数据
     */
    private int convertInt(byte[] bytes) {
        return (bytes[1] & 0xFF) << 24 |
                (bytes[2] & 0xFF) << 16 |
                (bytes[3] & 0xFF) << 8 |
                (bytes[4] & 0xFF);
    }

6、全局变量

//加密秘钥
    private final byte[] xorKey = {0X28, 0X51, 0X22, 0X31, 0X18, 0X16, 0X15, 0X13, 0X18, 0X16, 0X10, 0X14};
    //静态秘钥
    private final byte[] staticXorKey = {0X21, 0X18, 0X22, 0X21, 0X28, 0X26, 0X15, 0X23, 0X28, 0X26, 0X10, 0X21};
    //16进制静态字符
    private final String hExChar = "0123456789abcdefABCDEF";

7、参考测试类

 @Test
    void test2()  {
        EncryptUtil encryptUtil = new EncryptUtil();
        String result = encryptUtil.encrypt("hello word 1234 你好 饕餮盛宴 魑魅魍魉 ⑩,hello word 1234 你好 饕餮盛宴 魑魅魍魉 ⑩hello word 1234 你好 饕餮盛宴 魑魅魍魉 ⑩hello word 1234 你好 饕餮盛宴 魑魅魍魉 ⑩hello word 1234 你好 饕餮盛宴 魑魅魍魉 ⑩hello word 1234 你好 饕餮盛宴 魑魅魍魉 ⑩hello word 1234 你好 饕餮盛宴 魑魅魍魉 ⑩hello word 1234 你好 饕餮盛宴 魑魅魍魉 ⑩hello word 1234 你好 饕餮盛宴 魑魅魍魉 ⑩hello word 1234 你好 饕餮盛宴 魑魅魍魉 ⑩hello word 1234 你好 饕餮盛宴 魑魅魍魉 ⑩hello word 1234 你好 饕餮盛宴 魑魅魍魉 ⑩hello word 1234 你好 饕餮盛宴 魑魅魍魉 ⑩hello word 1234 你好 饕餮盛宴 魑魅魍魉 ⑩hello word 1234 你好 饕餮盛宴 魑魅魍魉 ⑩hello word 1234 你好 饕餮盛宴 魑魅魍魉 ⑩hello word 1234 你好 饕餮盛宴 魑魅魍魉 ⑩hello word 1234 你好 饕餮盛宴 魑魅魍魉 ⑩hello word 1234 你好 饕餮盛宴 魑魅魍魉 ⑩hello word 1234 你好 饕餮盛宴 魑魅魍魉 ⑩hello word 1234 你好 饕餮盛宴 魑魅魍魉 ⑩");
        logger.info("加密结果:" + result);
        String newResult = encryptUtil.decrypt(result);
        logger.info("解密结果:" + newResult);
    }

    @Test
    void test5() {
        EncryptUtil encryptUtil = new EncryptUtil();
        String result = encryptUtil.staticEncrypt("hello word 1234 浑浑噩噩 饕餮盛宴 魑魅魍魉 ①hello word 1234 你好 饕餮盛宴 魑魅魍魉 ⑩,hello word 1234 你好 饕餮盛宴 魑魅魍魉 ⑩hello word 1234 你好 饕餮盛宴 魑魅魍魉 ⑩hello word 1234 你好 饕餮盛宴 魑魅魍魉 ");
        logger.info("加密结果:" + result);
        String newResult = encryptUtil.staticDecrypt(result);
        logger.info("解密结果:" + newResult);
    }

学习是痛苦的,成长是快乐的!

  • 8
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值