Java 字符串異或加密

前言

有時須要加密保存數據,可是我不會用AES……因此選擇了更簡單的異或加密。ide

解決方案

    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);
    }
相關文章
相關標籤/搜索