java 实现逐字节异或,做加密或者教研常用方法

public class a {
    public a() {
    }

    public static void main(String[] args) {
        a a = new a();
        String abc = null;
        String strIn = "aabbcc呵呵呵1234哈哈哈";
        String strW = "";
        String strU = "";
        strW = new String(a.wrapStr(strIn));
        try {        
            System.out.println("111=="+strU);
        } catch (Exception ex1) {
            System.out.println(ex1);
        }

    }

    /**
     * 打包
     * @param str String
     * @return byte[]
     * 逐个直接进行异或
     */
    public static byte[] wrapStr(String str) {
        byte[] bReturn = null;
        String strReturn = "";
        byte bFirst = 0x02;
        byte bEnd = 0x03;
        if (str != null) {
            if (str.length() == 0) {
                return null;
            }
            byte[] b = str.getBytes();
            int iLen = b.length;
            
            str = fillChar(iLen + "", '0', true, 4) + str;
            System.out.println("str====="+str);
            byte[] bb = str.getBytes();
            int ii = bb.length;
            byte[] bAll = new byte[ii + 3];
            bAll[0] = bFirst;
            bAll[ii + 1] = bEnd;
            for (int i = 0; i < ii; i++) {
                bAll = bb;
            }

            byte bTemp = bFirst;
            for (int i = 0; i < bAll.length - 2; i++) {
                bTemp = (byte) (bTemp ^ bAll);
            }
            bAll[bAll.length - 1] = bTemp;
            bReturn = bAll;
        } else {
            return null;
        }
        System.out.println(bReturn.toString());
        for (int i = 0; i < bReturn.length - 1; i++) {
            System.out.println("abc====="+bReturn);
        }
        
        return bReturn;
    }

    /**
     * 解包
     * @param str String
     * @return byte[]
     */


    private static String fillChar(String str, char c, boolean orient,
                                   int totallenth) {
        if (str == null) {
            return fillChar("", ' ', false, totallenth);
        }
        byte[] b = str.getBytes();
        int strlen = b.length;
        int len = totallenth - strlen;
        for (int i = 0; i < len; i++) {
            if (orient == true) {
                str = c + str;
            } else {
                str = str + c;
            }
        }
        return str;
    }

}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值