要求将程序补充完整,然后运行,写出结果。
public class Test { public static void encode(byte[] in, byte[] out, int password) {
int len = in.length; int seed = password ^ 0x90654604;
for (int i = 0; i < len; ++i) {
int m=in[i] ^ seed;
byte a = (byte) ((in[i] ^ seed) >>> 2);
byte b = (byte) (((((int) in[i]) << 18) ^ seed) >>> (18 - 6));
a &= 0x3f;
b &= 0xc0;
out[i] = (byte) (a | b);
seed = (((seed << 7) ^ seed ^ out[i]) + 1792013);
seed=seed;
}
} public static void decode(byte[] in, byte[] out, int password) {
int len = in.length; int seed = password ^ 0x90654604;
for (int i = 0; i < len; ++i) {
// (题目要求将for循环的循环体补充完整)
}
} public static void main(String[] args) throws Exception {
int password = 0xab6d1028;
byte[] buf1 = { -2, 14, 37, -56, -67, 109, 19, 57, -38, 57, -32, -36,
-49, -69, 115, 29, -33, 90, -107, 121, 82, -65, 93, 92, -45,
74, 122, -91, -27, -26, 99, 43, 63, -12, -52, -110, };
byte[] buf2 = new byte[buf1.length];
encode(buf1, buf2, password);
decode(buf2, buf1,password);
System.out.println(new String(buf1, "GBK"));
}}
希望能详细讲讲 过程!!
public class Test { public static void encode(byte[] in, byte[] out, int password) {
int len = in.length; int seed = password ^ 0x90654604;
for (int i = 0; i < len; ++i) {
int m=in[i] ^ seed;
byte a = (byte) ((in[i] ^ seed) >>> 2);
byte b = (byte) (((((int) in[i]) << 18) ^ seed) >>> (18 - 6));
a &= 0x3f;
b &= 0xc0;
out[i] = (byte) (a | b);
seed = (((seed << 7) ^ seed ^ out[i]) + 1792013);
seed=seed;
}
} public static void decode(byte[] in, byte[] out, int password) {
int len = in.length; int seed = password ^ 0x90654604;
for (int i = 0; i < len; ++i) {
// (题目要求将for循环的循环体补充完整)
}
} public static void main(String[] args) throws Exception {
int password = 0xab6d1028;
byte[] buf1 = { -2, 14, 37, -56, -67, 109, 19, 57, -38, 57, -32, -36,
-49, -69, 115, 29, -33, 90, -107, 121, 82, -65, 93, 92, -45,
74, 122, -91, -27, -26, 99, 43, 63, -12, -52, -110, };
byte[] buf2 = new byte[buf1.length];
encode(buf1, buf2, password);
decode(buf2, buf1,password);
System.out.println(new String(buf1, "GBK"));
}}
希望能详细讲讲 过程!!
解决方案 »
- 求助关于数据类型的问题。。。
- 一家美资企业的面试题
- 看代码注释说明。报错:找不到符号
- 关于从socket读取数据被挂起的问题
- 求质数问题 谁帮我看看到底怎么错了
- 关于Map和Iterator的问题
- 为什么我下载的j2re1.4.1_01的bin下没有jar,javac这些应用程序。在先等待。
- 请问在企业级应用开发中,要运用设计模式么
- getAppletContext().showDocument(new url())错误java.lang.NullPointerException
- 关于ArrayList扩容时的效率问题。
- 问一个关于protected修饰符的问题。
- 学了java学什么数据库比较好?
http://topic.csdn.net/u/20110908/12/a852b3a1-fb14-41b3-9776-41c93c62706e.html以下代码是在该帖子的回复,代码差不多,LZ自己参考修改一下吧,懒得写了public static void decode(byte[] in, byte[] out, int password) {
int len = in.length; int seed = password ^ 0xd6445ad8;
for (int i = 0; i < len; ++i) {
byte a = (byte)(in[i] & 0x1f);
byte b = (byte)(in[i] & 0xe0);
a = (byte)(((a << 3) ^ seed) & 0xf8);
b = (byte)((((((int)b) << (13-5)) ^ seed) >> 13) & 0x07);
out[i] = (byte) (a | b);
seed = ((seed ^ in[i]) * 5393887 + in[i]);
}
}
public static void decode(byte[] in, byte[] out, int password) {
int len = in.length; int seed = password ^ 0x90654604;
for (int i = 0; i < len; ++i) {
byte a = (byte)(in[i] & 0x3f);
byte b = (byte)(in[i] & 0xc0);
a = (byte)(((a << 2) ^ seed) & 0xfc);
b = (byte)((((((int)b) << (18-6)) ^ seed) >> 18) & 0x03);
out[i] = (byte)(a | b);
seed = (((seed << 7) ^ seed ^ in[i]) + 1792013);
}
}
b = (byte)((((((int)b) << (18-6)) ^ seed) >> 18) & 0x03);
麻烦问一下,这两个表达式中的0xfc和0x03是怎么来的呀?
首先要知道一点
c = a ^ b 那么 a == (c ^ b),也就是一个数xor b,那么xor的结果再 xor b 就得到原数
再看加密的时候
byte a = (byte) ((in[i] ^ seed) >>> 2);
in[i] ^ seed 得到一个结果,用这个结果再 ^ seed 就可以得到 in[i]
但是可惜的是,这个结果被 >>> 2,即右移2位了,所以结果丢失了右2位信息
那么就先把该结果左移2位,然后 ^ seed,这样得到了原数in[i]的左6位是可靠的,右2位因为上面丢失了信息,所以此时得到的数的右2位是不可靠的,所以要把右2位信息去掉,即 & 0xfc
那么右2位信息要怎么恢复呢,这就要从b来得到
byte b = (byte) (((((int) in[i]) << 18) ^ seed) >>> (18 - 6));
象a的恢复一样,先把b << (18-6),然后 ^ seed,然后再 >> 18,此时只需要得到b信息的右2位就可以了,所以再 & 0x03
最后 a|b,即in[i]的可靠的左6位信息|in[i]的可靠的右2位信息既可以得到原来的in[i]
int len = in.length; int seed = password ^ 0x90654604;
for (int i = 0; i < len; ++i) {
byte a = (byte)(in[i] & 0x3f);
byte b = (byte)(in[i] & 0xc0);
a = (byte)(((a << 2) ^ seed) & 0xfc);
b = (byte)((((((int)b) << (18-6)) ^ seed) >> 18) & 0x03);
out[i] = (byte)(a | b);
seed = (((seed << 7) ^ seed ^ in[i]) + 1792013);
}
}
怎么执行不了呢