国密算法 java实现_国密算法Java代码的标准实现

1 importjava.io.ByteArrayInputStream;2 importjava.io.ByteArrayOutputStream;3

4 public classSM4 {5 public static final int[] CK = new int[]{462357, 472066609, 943670861, 1415275113, 1886879365, -1936483679, -1464879427, -993275175, -521670923, -66909679, 404694573, 876298825, 1347903077, 1819507329, -2003855715, -1532251463, -1060647211, -589042959, -117504499, 337322537, 808926789, 1280531041, 1752135293, -2071227751, -1599623499, -1128019247, -656414995, -184876535, 269950501, 741554753, 1213159005, 1684763257};6 public static final int[] FK = new int[]{-1548633402, 1453994832, 1736282519, -1301273892};7 public static final int SM4_DECRYPT = 0;8 public static final int SM4_ENCRYPT = 1;9 public static final byte[] SboxTable = new byte[]{-42, -112, -23, -2, -52, -31, 61, -73, 22, -74, 20, -62, 40, -5, 44, 5, 43, 103, -102, 118, 42, -66, 4, -61, -86, 68, 19, 38, 73, -122, 6, -103, -100, 66, 80, -12, -111, -17, -104, 122, 51, 84, 11, 67, -19, -49, -84, 98, -28, -77, 28, -87, -55, 8, -24, -107, -128, -33, -108, -6, 117, -113, 63, -90, 71, 7, -89, -4, -13, 115, 23, -70, -125, 89, 60, 25, -26, -123, 79, -88, 104, 107, -127, -78, 113, 100, -38, -117, -8, -21, 15, 75, 112, 86, -99, 53, 30, 36, 14, 94, 99, 88, -47, -94, 37, 34, 124, 59, 1, 33, 120, -121, -44, 0, 70, 87, -97, -45, 39, 82, 76, 54, 2, -25, -96, -60, -56, -98, -22, -65, -118, -46, 64, -57, 56, -75, -93, -9, -14, -50, -7, 97, 21, -95, -32, -82, 93, -92, -101, 52, 26, 85, -83, -109, 50, 48, -11, -116, -79, -29, 29, -10, -30, 46, -126, 102, -54, 96, -64, 41, 35, -85, 13, 83, 78, 111, -43, -37, 55, 69, -34, -3, -114, 47, 3, -1, 106, 114, 109, 108, 91, 81, -115, 27, -81, -110, -69, -35, -68, 127, 17, -39, 92, 65, 31, 16, 90, -40, 10, -63, 49, -120, -91, -51, 123, -67, 45, 116, -48, 18, -72, -27, -76, -80, -119, 105, -105, 74, 12, -106, 119, 126, 101, -71, -15, 9, -59, 110, -58, -124, 24, -16, 125, -20, 58, -36, 77, 32, 121, -18, 95, 62, -41, -53, 57, 72};10

11 publicSM4() {12 super();13 }14

15 private long GET_ULONG_BE(byte[] arg7, intarg8) {16 return (((long)(arg7[arg8] & 255))) << 24 | (((long)((arg7[arg8 + 1] & 255) << 16))) | (((long)((arg7[arg8 + 2] & 255) << 8))) | (((long)(arg7[arg8 + 3] & 255))) & 4294967295L;17 }18

19 private void PUT_ULONG_BE(long arg7, byte[] arg9, intarg10) {20 arg9[arg10] = ((byte)(((int)(arg7 >> 24 & 255))));21 arg9[arg10 + 1] = ((byte)(((int)(arg7 >> 16 & 255))));22 arg9[arg10 + 2] = ((byte)(((int)(arg7 >> 8 & 255))));23 arg9[arg10 + 3] = ((byte)(((int)(arg7 & 255))));24 }25

26 private long ROTL(long arg5, intarg7) {27 return this.SHL(arg5, arg7) | arg5 >> 32 -arg7;28 }29

30 private long SHL(long arg3, intarg5) {31 return (-1 & arg3) <

34 private void SWAP(long[] arg5, intarg6) {35 long v0 =arg5[arg6];36 arg5[arg6] = arg5[31 -arg6];37 arg5[31 - arg6] =v0;38 }39

40 private byte[] padding(byte[] arg6, intarg7) {41 intv1;42 byte[] v0 = null;43 if(arg6 == null) {44 returnv0;45 }46

47 if(arg7 == 1) {48 v1 = 16 - arg6.length % 16;49 v0 = new byte[arg6.length +v1];50 System.arraycopy(arg6, 0, v0, 0, arg6.length);51 intv2;52 for(v2 = 0; v2 < v1; ++v2) {53 v0[arg6.length + v2] = ((byte)v1);54 }55 }56 else{57 v1 = arg6[arg6.length - 1];58 v0 = new byte[arg6.length -v1];59 System.arraycopy(arg6, 0, v0, 0, arg6.length -v1);60 }61

62 returnv0;63 }64

65 private long sm4CalciRK(longarg11) {66 byte[] v5 = new byte[4];67 byte[] v4 = new byte[4];68 this.PUT_ULONG_BE(arg11, v5, 0);69 v4[0] = this.sm4Sbox(v5[0]);70 v4[1] = this.sm4Sbox(v5[1]);71 v4[2] = this.sm4Sbox(v5[2]);72 v4[3] = this.sm4Sbox(v5[3]);73 long v0 = this.GET_ULONG_BE(v4, 0);74 return this.ROTL(v0, 13) ^ v0 ^ this.ROTL(v0, 23);75 }76

77 private long sm4F(long arg3, long arg5, long arg7, long arg9, longarg11) {78 return this.sm4Lt(arg5 ^ arg7 ^ arg9 ^ arg11) ^arg3;79 }80

81 private long sm4Lt(longarg11) {82 byte[] v5 = new byte[4];83 byte[] v4 = new byte[4];84 this.PUT_ULONG_BE(arg11, v5, 0);85 v4[0] = this.sm4Sbox(v5[0]);86 v4[1] = this.sm4Sbox(v5[1]);87 v4[2] = this.sm4Sbox(v5[2]);88 v4[3] = this.sm4Sbox(v5[3]);89 long v0 = this.GET_ULONG_BE(v4, 0);90 return this.ROTL(v0, 2) ^ v0 ^ this.ROTL(v0, 10) ^ this.ROTL(v0, 18) ^ this.ROTL(v0, 24);91 }92

93 private byte sm4Sbox(bytearg3) {94 return SM4.SboxTable[arg3 & 255];95 }96

97 public byte[] sm4_crypt_cbc(SM4_Context arg13, byte[] arg14, byte[] arg15) throwsException {98 byte[] v8;99 byte[] v6;100 byte[] v2;101 if(arg14 != null) {102 int v1 = 16;103 if(arg14.length ==v1) {104 if(arg15 != null) {105 if((arg13.isPadding) && arg13.mode == 1) {106 arg15 = this.padding(arg15, 1);107 }108

109 int v3 =arg15.length;110 ByteArrayInputStream v4 = newByteArrayInputStream(arg15);111 ByteArrayOutputStream v5 = newByteArrayOutputStream();112 if(arg13.mode == 1) {113 while(v3 > 0) {114 v2 = new byte[v1];115 v6 = new byte[v1];116 v8 = new byte[v1];117 v4.read(v2);118 intv0;119 for(v0 = 0; v0 < v1; ++v0) {120 v6[v0] = ((byte)(v2[v0] ^arg14[v0]));121 }122

123 this.sm4_one_round(arg13.sk, v6, v8);124 System.arraycopy(v8, 0, arg14, 0, v1);125 v5.write(v8);126 v3 += -16;127 }128 }129 else{130 v2 = new byte[v1];131 while(v3 > 0) {132 v6 = new byte[v1];133 v8 = new byte[v1];134 byte[] v9 = new byte[v1];135 v4.read(v6);136 System.arraycopy(v6, 0, v2, 0, v1);137 this.sm4_one_round(arg13.sk, v6, v8);138 intv0;139 for(v0 = 0; v0 < v1; ++v0) {140 v9[v0] = ((byte)(v8[v0] ^arg14[v0]));141 }142

143 System.arraycopy(v2, 0, arg14, 0, v1);144 v5.write(v9);145 v3 += -16;146 }147 }148

149 byte[] v1_1 =v5.toByteArray();150 if((arg13.isPadding) && arg13.mode == 0) {151 v1_1 = this.padding(v1_1, 0);152 }153

154 v4.close();155 v5.close();156 returnv1_1;157 }158 else{159 throw new Exception("input is null!");160 }161 }162 }163

164 throw new Exception("iv error!");165 }166

167 public byte[] sm4_crypt_ecb(SM4_Context arg7, byte[] arg8) throwsException {168 byte[] v3;169 if(arg8 != null) {170 if((arg7.isPadding) && arg7.mode == 1) {171 arg8 = this.padding(arg8, 1);172 }173

174 int v0 =arg8.length;175 ByteArrayInputStream v1 = newByteArrayInputStream(arg8);176 ByteArrayOutputStream v2 = newByteArrayOutputStream();177 while(v0 > 0) {178 byte[] v4 = new byte[16];179 v3 = new byte[16];180 v1.read(v4);181 this.sm4_one_round(arg7.sk, v4, v3);182 v2.write(v3);183 v0 += -16;184 }185

186 v3 =v2.toByteArray();187 if((arg7.isPadding) && arg7.mode == 0) {188 v3 = this.padding(v3, 0);189 }190

191 v1.close();192 v2.close();193 returnv3;194 }195

196 throw new Exception("input is null!");197 }198

199 private void sm4_one_round(long[] arg23, byte[] arg24, byte[] arg25) {200 intv0;201 SM4 v11 = this;202 byte[] v13 =arg25;203 long[] v14 = new long[36];204 v14[0] = v11.GET_ULONG_BE(arg24, 0);205 v14[1] = v11.GET_ULONG_BE(arg24, 4);206 v14[2] = v11.GET_ULONG_BE(arg24, 8);207 v14[3] = v11.GET_ULONG_BE(arg24, 12);208 intv8;209 for(v8 = 0; true; ++v8) {210 v0 = 32;211 if(v8 >=v0) {212 break;213 }214

215 v14[v8 + 4] = this.sm4F(v14[v8], v14[v8 + 1], v14[v8 + 2], v14[v8 + 3], arg23[v8]);216 }217

218 v11.PUT_ULONG_BE(v14[35], v13, 0);219 v11.PUT_ULONG_BE(v14[34], v13, 4);220 v11.PUT_ULONG_BE(v14[33], v13, 8);221 v11.PUT_ULONG_BE(v14[v0], v13, 12);222 }223

224 private void sm4_setkey(long[] arg13, byte[] arg14) {225 long[] v1 = new long[4];226 long[] v2 = new long[36];227 int v3 = 0;228 v1[0] = this.GET_ULONG_BE(arg14, 0);229 v1[1] = this.GET_ULONG_BE(arg14, 4);230 v1[2] = this.GET_ULONG_BE(arg14, 8);231 v1[3] = this.GET_ULONG_BE(arg14, 12);232 long v5 = v1[0];233 int[] v9 =SM4.FK;234 v2[0] = v5 ^ (((long)v9[0]));235 v2[1] = v1[1] ^ (((long)v9[1]));236 v2[2] = v1[2] ^ (((long)v9[2]));237 v2[3] = v1[3] ^ (((long)v9[3]));238 while(v3 < 32) {239 v2[v3 + 4] = v2[v3] ^ this.sm4CalciRK(v2[v3 + 1] ^ v2[v3 + 2] ^ v2[v3 + 3] ^ (((long)SM4.CK[v3])));240 arg13[v3] = v2[v3 + 4];241 ++v3;242 }243 }244

245 public void sm4_setkey_dec(SM4_Context arg4, byte[] arg5) throwsException {246 if(arg4 != null) {247 if(arg5 != null) {248 int v1 = 16;249 if(arg5.length ==v1) {250 arg4.mode = 0;251 this.sm4_setkey(arg4.sk, arg5);252 intv0;253 for(v0 = 0; v0 < v1; ++v0) {254 this.SWAP(arg4.sk, v0);255 }256

257 return;258 }259 }260

261 throw new Exception("key error!");262 }263

264 throw new Exception("ctx is null!");265 }266

267 public void sm4_setkey_enc(SM4_Context arg3, byte[] arg4) throwsException {268 if(arg3 != null) {269 if(arg4 != null && arg4.length == 16) {270 arg3.mode = 1;271 this.sm4_setkey(arg3.sk, arg4);272 return;273 }274

275 throw new Exception("key error!");276 }277

278 throw new Exception("ctx is null!");279 }280 }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值