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;
}
}
java 实现逐字节异或,做加密或者教研常用方法
最新推荐文章于 2023-06-21 14:35:48 发布