最近要改写一个核心加密认证类,从C#改写成Java。
发现在调试时,加密的数据无论如何也对不上。
经过跟踪,发现问题出在C#和Java byte类型的区别上:在C#里 byte类型是无符号的,而Java里是有符号的,所以C#里的129到Java里就成了负数。
发现了问题,解决就比较容易了,针对Java的byte,采用Int来进行存储。
通过如下代码从byte到int进行转换:
- /**
- * from byte to int, because of byte in java is signed
- */
- private static int toInt(int b) {
- return b >= 0 ? (int)b : (int)(b + 256);
- }
对于下面C#的代码:
- private static AuthenticationTicket FromByteArray(byte[] buf)
- {
- MemoryStream ms = new MemoryStream(buf);
- BinaryReader reader = new BinaryReader(ms);
- short version = reader.ReadInt16();
- short scope = reader.ReadInt16();
- int key = reader.ReadInt32();
- }
改写为如下形式,相当于重新实现BinaryReader的ReadInt16和ReadInt32方法。
- private static AuthenticationTicket FromByteArray(int[] bufInt)
- {
- int version = readInt16(bufInt);
- int scope = readInt16(bufInt);
- long key = readInt32(bufInt);
- }
- private static int readInt16(int[] bufInt) {
- int i = 0;
- for(int j = 0; j < 2; readArrayIndex++, j++) {
- i += bufInt[readArrayIndex] << (j << 3);
- }
- return i;
- }
- private static long readInt32(int[] bufInt) {
- long i = 0;
- for(int j = 0; j < 4; readArrayIndex++, j++) {
- i += bufInt[readArrayIndex] << (j << 3);
- }
- return i;
- }
上面的例子说明,c#和Java虽然非常相像,但是一些关键细节的不同是需要仔细考虑的。