位置加密applet基本实现,目录下包含c java参考代码、打包工具、说明文档、 #2
@@ -4,9 +4,11 @@ package com.cscn;
|
||||
import static com.cscn.Zuc256Util.L1;
|
||||
import static com.cscn.Zuc256Util.L2;
|
||||
import static com.cscn.Zuc256Util.add31;
|
||||
import static com.cscn.Zuc256Util.add31_pair;
|
||||
import static com.cscn.Zuc256Util.makeU31;
|
||||
import static com.cscn.Zuc256Util.makeU32;
|
||||
import static com.cscn.Zuc256Util.rot31;
|
||||
import static com.cscn.Zuc256Util.rot31_pair;
|
||||
|
||||
/**
|
||||
* ZUC-256 核心:状态初始化、密钥字生成、密钥流生成。
|
||||
|
||||
@@ -1,15 +1,31 @@
|
||||
package com.cscn;
|
||||
|
||||
/**
|
||||
* MAC上下文类
|
||||
* MAC上下文类(JavaCard版,int 拆分为两个 short)
|
||||
*/
|
||||
public final class Zuc256MacCtx {
|
||||
int[] LFSR = new int[16];
|
||||
int R1;
|
||||
int R2;
|
||||
// LFSR: 原本 int[16],拆成 hi/lo 各 16 short
|
||||
short[] LFSR_hi = new short[16];
|
||||
short[] LFSR_lo = new short[16];
|
||||
|
||||
// R1、R2: 原本 int,拆成 hi/lo
|
||||
short R1_hi;
|
||||
short R1_lo;
|
||||
short R2_hi;
|
||||
short R2_lo;
|
||||
|
||||
// 缓冲区
|
||||
byte[] buf = new byte[4];
|
||||
int buflen;
|
||||
int[] T = new int[4];
|
||||
int[] K0 = new int[4];
|
||||
int macbits;
|
||||
short buflen;
|
||||
|
||||
// T: 原本 int[4],拆成 hi/lo
|
||||
short[] T_hi = new short[4];
|
||||
short[] T_lo = new short[4];
|
||||
|
||||
// K0: 原本 int[4],拆成 hi/lo
|
||||
short[] K0_hi = new short[4];
|
||||
short[] K0_lo = new short[4];
|
||||
|
||||
// macbits: 原本 int,改成 short 足够
|
||||
short macbits;
|
||||
}
|
||||
|
||||
@@ -1,10 +1,16 @@
|
||||
package com.cscn;
|
||||
|
||||
/**
|
||||
* ZUC状态类
|
||||
* ZUC状态类(JavaCard版,int 拆为 hi/lo short)
|
||||
*/
|
||||
public final class Zuc256State {
|
||||
int[] LFSR = new int[16]; // 线性反馈移位寄存器
|
||||
int R1; // 寄存器1
|
||||
int R2; // 寄存器2
|
||||
// LFSR: 原 int[16] -> hi/lo 各 16
|
||||
public short[] LFSR_hi = new short[16];
|
||||
public short[] LFSR_lo = new short[16];
|
||||
|
||||
// R1, R2: 原 int -> hi/lo
|
||||
public short R1_hi;
|
||||
public short R1_lo;
|
||||
public short R2_hi;
|
||||
public short R2_lo;
|
||||
}
|
||||
|
||||
@@ -9,7 +9,7 @@ public final class Zuc256Tables {
|
||||
private Zuc256Tables() {}
|
||||
|
||||
// S盒:S0, S1
|
||||
public static final int[] S0 = {
|
||||
public static final short[] S0 = {
|
||||
0x3e,0x72,0x5b,0x47,0xca,0xe0,0x00,0x33,0x04,0xd1,0x54,0x98,0x09,0xb9,0x6d,0xcb,
|
||||
0x7b,0x1b,0xf9,0x32,0xaf,0x9d,0x6a,0xa5,0xb8,0x2d,0xfc,0x1d,0x08,0x53,0x03,0x90,
|
||||
0x4d,0x4e,0x84,0x99,0xe4,0xce,0xd9,0x91,0xdd,0xb6,0x85,0x48,0x8b,0x29,0x6e,0xac,
|
||||
@@ -28,7 +28,7 @@ public final class Zuc256Tables {
|
||||
0x8d,0x27,0x1a,0xdb,0x81,0xb3,0xa0,0xf4,0x45,0x7a,0x19,0xdf,0xee,0x78,0x34,0x60
|
||||
};
|
||||
|
||||
public static final int[] S1 = {
|
||||
public static final short[] S1 = {
|
||||
0x55,0xc2,0x63,0x71,0x3b,0xc8,0x47,0x86,0x9f,0x3c,0xda,0x5b,0x29,0xaa,0xfd,0x77,
|
||||
0x8c,0xc5,0x94,0x0c,0xa6,0x1a,0x13,0x00,0xe3,0xa8,0x16,0x72,0x40,0xf9,0xf8,0x42,
|
||||
0x44,0x26,0x68,0x96,0x81,0xd9,0x45,0x3e,0x10,0x76,0xc6,0xa7,0x8b,0x39,0x43,0xe1,
|
||||
@@ -50,7 +50,7 @@ public final class Zuc256Tables {
|
||||
/**
|
||||
* 常量数组 D
|
||||
*/
|
||||
public static final int[][] ZUC256_D = {
|
||||
public static final short[][] ZUC256_D = {
|
||||
{0x22,0x2F,0x24,0x2A,0x6D,0x40,0x40,0x40,0x40,0x40,0x40,0x40,0x40,0x52,0x10,0x30},
|
||||
{0x22,0x2F,0x25,0x2A,0x6D,0x40,0x40,0x40,0x40,0x40,0x40,0x40,0x40,0x52,0x10,0x30},
|
||||
{0x23,0x2F,0x24,0x2A,0x6D,0x40,0x40,0x40,0x40,0x40,0x40,0x40,0x40,0x52,0x10,0x30},
|
||||
|
||||
@@ -7,22 +7,36 @@ public final class Zuc256Util {
|
||||
|
||||
private Zuc256Util() {}
|
||||
|
||||
/** 辅助方法:将字节数组转换为32位整数 */
|
||||
public static int getU32(byte[] p, int offset) {
|
||||
return ((p[offset] & 0xFF) << 24) |
|
||||
((p[offset + 1] & 0xFF) << 16) |
|
||||
((p[offset + 2] & 0xFF) << 8) |
|
||||
(p[offset + 3] & 0xFF);
|
||||
/** 辅助方法:从字节数组取出 32 位整数,存放到 short[2] (lo, hi) */
|
||||
public static void getU32(byte[] p, short offset, short[] out32 /* len=2 */) {
|
||||
out32[0] = (short) (((p[offset + 2] & 0xFF) << 8) | (p[offset + 3] & 0xFF)); //低16位
|
||||
out32[1] = (short) (((p[offset] & 0xFF) << 8) | (p[offset + 1] & 0xFF)); //高16位
|
||||
}
|
||||
|
||||
/** 辅助方法:将32位整数转换为字节数组 */
|
||||
public static void putU32(byte[] p, int offset, int v) {
|
||||
p[offset] = (byte) (v >> 24);
|
||||
p[offset + 1] = (byte) (v >> 16);
|
||||
p[offset + 2] = (byte) (v >> 8);
|
||||
p[offset + 3] = (byte) v;
|
||||
|
||||
// /** 辅助方法:将32位整数转换为字节数组 */
|
||||
// public static void putU32(byte[] p, int offset, int v) {
|
||||
// p[offset] = (byte) (v >> 24);
|
||||
// p[offset + 1] = (byte) (v >> 16);
|
||||
// p[offset + 2] = (byte) (v >> 8);
|
||||
// p[offset + 3] = (byte) v;
|
||||
// }
|
||||
|
||||
/** 辅助方法:将32位整数(vlo=低16位, vhi=高16位)写入字节数组 */
|
||||
public static void putU32(byte[] p, short offset, short vlo, short vhi) {
|
||||
// 写高16位
|
||||
putU16(p, offset, vhi);
|
||||
// 写低16位
|
||||
putU16(p, (short)(offset + 2), vlo);
|
||||
}
|
||||
|
||||
/** 辅助方法:将16位整数(short)写入字节数组,高字节在前 */
|
||||
public static void putU16(byte[] p, short offset, short v) {
|
||||
p[offset] = (byte) ((v >> 8) & 0xFF);
|
||||
p[offset + 1] = (byte) (v & 0xFF);
|
||||
}
|
||||
|
||||
|
||||
// === 31/32 位运算 ===
|
||||
|
||||
/** 31位加法 */
|
||||
|
||||
Reference in New Issue
Block a user