MCPcopy Create free account
hub / github.com/0opslab/opslabJutil / setKey

Method setKey

src/main/java/com/opslab/util/encrypt/Blowfish.java:433–502  ·  view source on GitHub ↗
(byte[] key)

Source from the content-addressed store, hash-verified

431 }
432
433 private void setKey(byte[] key) {
434/*
435* - comments are from _Applied Crypto_, Schneier, p338
436* please be careful comparing the two, AC numbers the
437* arrays from 1, the enclosed code from 0.
438*
439* (1)
440* Initialise the S-boxes and the P-array, with a fixed string
441* This string contains the hexadecimal digits of pi (3.141...)
442*/
443 System.arraycopy(KS0, 0, S0, 0, SBOX_SK);
444 System.arraycopy(KS1, 0, S1, 0, SBOX_SK);
445 System.arraycopy(KS2, 0, S2, 0, SBOX_SK);
446 System.arraycopy(KS3, 0, S3, 0, SBOX_SK);
447
448 System.arraycopy(KP, 0, P, 0, P_SZ);
449
450/*
451* (2)
452* Now, XOR P[0] with the first 32 bits of the key, XOR P[1] with the
453* second 32-bits of the key, and so on for all bits of the key
454* (up to P[17]). Repeatedly cycle through the key bits until the
455* entire P-array has been XOR-ed with the key bits
456*/
457 int keyLength = key.length;
458 int keyIndex = 0;
459
460 for (int i = 0; i < P_SZ; i++) {
461// get the 32 bits of the key, in 4 * 8 bit chunks
462 int data = 0x0000000;
463 for (int j = 0; j < 4; j++) {
464// create a 32 bit block
465 data = (data << 8) | (key[keyIndex++] & 0xff);
466
467// wrap when we get to the end of the key
468 if (keyIndex >= keyLength) {
469 keyIndex = 0;
470 }
471 }
472// XOR the newly created 32 bit chunk onto the P-array
473 P[i] ^= data;
474 }
475
476/*
477* (3)
478* Encrypt the all-zero string with the Blowfish algorithm, using
479* the subkeys described in (1) and (2)
480*
481* (4)
482* Replace P1 and P2 with the output of step (3)
483*
484* (5)
485* Encrypt the output of step(3) using the Blowfish algorithm,
486* with the modified subkeys.
487*
488* (6)
489* Replace P3 and P4 with the output of step (5)
490*

Callers 1

initMethod · 0.95

Calls 1

processTableMethod · 0.95

Tested by

no test coverage detected