MCPcopy Index your code
hub / github.com/f0ng/autoDecoder / sm4_crypt_cbc

Method sm4_crypt_cbc

src/com/autoDecoder/util/SM4.java:272–328  ·  view source on GitHub ↗
(SM4_Context ctx, byte[] iv, byte[] input)

Source from the content-addressed store, hash-verified

270 }
271
272 public byte[] sm4_crypt_cbc(SM4_Context ctx, byte[] iv, byte[] input) throws Exception {
273 if (iv == null || iv.length != 16) {
274 throw new Exception("iv error!");
275 }
276
277 if (input == null) {
278 throw new Exception("input is null!");
279 }
280
281 if (ctx.isPadding && ctx.mode == SM4_ENCRYPT) {
282 input = padding(input, SM4_ENCRYPT);
283 }
284
285 int i = 0;
286 int length = input.length;
287 ByteArrayInputStream bins = new ByteArrayInputStream(input);
288 ByteArrayOutputStream bous = new ByteArrayOutputStream();
289 if (ctx.mode == SM4_ENCRYPT) {
290 for (; length > 0; length -= 16) {
291 byte[] in = new byte[16];
292 byte[] out = new byte[16];
293 byte[] out1 = new byte[16];
294
295 bins.read(in);
296 for (i = 0; i < 16; i++) {
297 out[i] = ((byte) (in[i] ^ iv[i]));
298 }
299 sm4_one_round(ctx.sk, out, out1);
300 System.arraycopy(out1, 0, iv, 0, 16);
301 bous.write(out1);
302 }
303 } else {
304 byte[] temp = new byte[16];
305 for (; length > 0; length -= 16) {
306 byte[] in = new byte[16];
307 byte[] out = new byte[16];
308 byte[] out1 = new byte[16];
309
310 bins.read(in);
311 System.arraycopy(in, 0, temp, 0, 16);
312 sm4_one_round(ctx.sk, in, out);
313 for (i = 0; i < 16; i++) {
314 out1[i] = ((byte) (out[i] ^ iv[i]));
315 }
316 System.arraycopy(temp, 0, iv, 0, 16);
317 bous.write(out1);
318 }
319 }
320
321 byte[] output = bous.toByteArray();
322 if (ctx.isPadding && ctx.mode == SM4_DECRYPT) {
323 output = padding(output, SM4_DECRYPT);
324 }
325 bins.close();
326 bous.close();
327 return output;
328 }
329}

Callers 2

encryptData_CBCMethod · 0.95
decryptData_CBCMethod · 0.95

Calls 2

paddingMethod · 0.95
sm4_one_roundMethod · 0.95

Tested by

no test coverage detected