在“字节数组”位置访问字节数组

时间:2014-02-19 15:49:14

标签: arrays javacard bitwise-and

我想这个问题听起来很混乱,试图让它更清楚。

我想用java卡实现一个指定的非泄漏映射,根据我的伪代码,我应该实现这样的东西:

JCArrayInt[] f = new JCArrayInt[2];
f[0] = new JCArrayInt(size);
f[1] = new JCArrayInt(size);
byte[] r = new byte[6];
byte[] help = new byte[6];

help = bit_and(r, 0x000000000001);
return f[help[5]].jcint;

基本上JCArrayInt充当二维数组,由两个大小为6的字节数组组成(48位无符号整数)。

所有我想做的是按位,并且“和”字节数组r用常量0x00...1,如果结果是1,我继续f[1]处的字节数组,否则使用byte[] f[0]

我现在所做的是,对于返回值,我只需采取上面显示的步骤。但由于这是“硬编码”,我对f[help[5]].jcint感觉不好,并希望知道更顺畅的方式。

1 个答案:

答案 0 :(得分:0)

我认为您可以像这样指定f1:

final byte[] f1 = new byte[] {(byte) 0x35, (byte) 0xB0,(byte) 0x88,(byte) 0xCC,(byte) 0xE1,(byte) 0x73}; 

你可以像这样指定6字节常量:

final byte[] constant = new byte[] {(byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x01};

你可以像这样左转:

public static byte[] ROTL48 (byte[] data) {
    byte  x = (byte)((data[0] >>> 7) & 0x001);
    short len = (short) (data.length - 1);
    for (short i = 0; i < len; ++i) {
        data[i] = (byte)(((data[i] << 1) & 0x0FE) | ((data[i + 1] >>> 7) & 0x001));
    }
    data[len] = (byte)(((data[len] << 1) & 0x0FE) | x);
    return data;
}

然后你可以按位使用它:

for (short i = 1; i < 47; i++) {
    R  = ROTL48(R);
    R1 = Utils.AND(R, constant);
    R  = Utils.XOR(R, Red[R1[5]].jcint);
    Y  = ROTL48(Y);
    Y1 = Utils.AND(Y, constant);
    R  = Utils.XOR(R, Mul[Y1[5]].jcint);}
    return R;
}

不知道这对你有用,但这个对我有用