日期:2014-05-20 浏览次数:20921 次
public class ValidateTest
{
public static void main(String[] args)
{
ValidateTest t = new ValidateTest();
String s = "abcdefghijklmnopqrstuvwxyz12345678987654321";
byte[] buf = t.getBuffer(s);
byte[] res = new byte[4];
t.getChkSum(buf.length, buf, res);
}
byte[] getBuffer(String s)
{
byte[] buf = s.getBytes();
int len = buf.length;
int temp = 0;
if (len % 4 != 0)
{
temp = 4 - len % 4;
}
byte[] buf1 = new byte[len + temp];
System.arraycopy(buf, 0, buf1, 0, len);
return buf1;
}
void getChkSum(int len, byte[] buf, byte[] res)
{
for (int i = 0; i < len; i += 4)
{
res[0] ^= buf[0 + i];
res[1] ^= buf[1 + i];
res[2] ^= buf[2 + i];
res[3] ^= buf[3 + i];
}
res[0] = (byte) ~res[0];
res[1] = (byte) ~res[1];
res[2] = (byte) ~res[2];
res[3] = (byte) ~res[3];
}
}