Java 字符串异或加密

时间:2021-04-06 14:14:59   收藏:0   阅读:0

前言

有时需要加密保存数据,但是我不会用AES……所以选择了更简单的异或加密。

解决方案

    public static String xor(String data, String password) {
        //异或加密
        byte b1[] = data.getBytes();
        byte b2[] = password.getBytes();
        byte longbytes[], shortbytes[];
        if (b1.length >= b2.length) {
            longbytes = b1;
            shortbytes = b2;
        } else {
            longbytes = b2;
            shortbytes = b1;
        }
        byte xorstr[] = new byte[longbytes.length];
        int i = 0;
        for (; i < shortbytes.length; i++) {
            xorstr[i] = (byte) (shortbytes[i] ^ longbytes[i]);
        }
        for (; i < longbytes.length; i++) {
            xorstr[i] = longbytes[i];
        }
        return new String(xorstr);
    }
评论(0
© 2014 mamicode.com 版权所有 京ICP备13008772号-2  联系我们:gaon5@hotmail.com
迷上了代码!