?? ideatest.java
字號:
t1 = Z[k++]; DK[j--] = Z[k++]; DK[j--] = t1; t1 = inv(Z[k++]); t2 = -Z[k++] & 0xffff; t3 = -Z[k++] & 0xffff; DK[j--] = inv(Z[k++]); DK[j--] = t2; DK[j--] = t3; DK[j--] = t1; } t1 = Z[k++]; DK[j--] = Z[k++]; DK[j--] = t1; t1 = inv(Z[k++]); t2 = -Z[k++] & 0xffff; t3 = -Z[k++] & 0xffff; DK[j--] = inv(Z[k++]); DK[j--] = t3; DK[j--] = t2; DK[j--] = t1;}/** cipher_idea** IDEA encryption/decryption algorithm. It processes plaintext in* 64-bit blocks, one at a time, breaking the block into four 16-bit* unsigned subblocks. It goes through eight rounds of processing* using 6 new subkeys each time, plus four for last step. The source* text is in array text1, the destination text goes into array text2* The routine represents 16-bit subblocks and subkeys as type int so* that they can be treated more easily as unsigned. Multiplication* modulo 0x10001 interprets a zero sub-block as 0x10000; it must to* fit in 16 bits.*/private void cipher_idea(byte [] text1, byte [] text2, int [] key){int i1 = 0; // Index into first text array.int i2 = 0; // Index into second text array.int ik; // Index into key array.int x1, x2, x3, x4, t1, t2; // Four "16-bit" blocks, two temps.int r; // Eight rounds of processing.for (int i = 0; i < text1.length; i += 8){ ik = 0; // Restart key index. r = 8; // Eight rounds of processing. // Load eight plain1 bytes as four 16-bit "unsigned" integers. // Masking with 0xff prevents sign extension with cast to int. x1 = text1[i1++] & 0xff; // Build 16-bit x1 from 2 bytes, x1 |= (text1[i1++] & 0xff) << 8; // assuming low-order byte first. x2 = text1[i1++] & 0xff; x2 |= (text1[i1++] & 0xff) << 8; x3 = text1[i1++] & 0xff; x3 |= (text1[i1++] & 0xff) << 8; x4 = text1[i1++] & 0xff; x4 |= (text1[i1++] & 0xff) << 8; do { // 1) Multiply (modulo 0x10001), 1st text sub-block // with 1st key sub-block. x1 = (int) ((long) x1 * key[ik++] % 0x10001L & 0xffff); // 2) Add (modulo 0x10000), 2nd text sub-block // with 2nd key sub-block. x2 = x2 + key[ik++] & 0xffff; // 3) Add (modulo 0x10000), 3rd text sub-block // with 3rd key sub-block. x3 = x3 + key[ik++] & 0xffff; // 4) Multiply (modulo 0x10001), 4th text sub-block // with 4th key sub-block. x4 = (int) ((long) x4 * key[ik++] % 0x10001L & 0xffff); // 5) XOR results from steps 1 and 3. t2 = x1 ^ x3; // 6) XOR results from steps 2 and 4. // Included in step 8. // 7) Multiply (modulo 0x10001), result of step 5 // with 5th key sub-block. t2 = (int) ((long) t2 * key[ik++] % 0x10001L & 0xffff); // 8) Add (modulo 0x10000), results of steps 6 and 7. t1 = t2 + (x2 ^ x4) & 0xffff; // 9) Multiply (modulo 0x10001), result of step 8 // with 6th key sub-block. t1 = (int) ((long) t1 * key[ik++] % 0x10001L & 0xffff); // 10) Add (modulo 0x10000), results of steps 7 and 9. t2 = t1 + t2 & 0xffff; // 11) XOR results from steps 1 and 9. x1 ^= t1; // 14) XOR results from steps 4 and 10. (Out of order). x4 ^= t2; // 13) XOR results from steps 2 and 10. (Out of order). t2 ^= x2; // 12) XOR results from steps 3 and 9. (Out of order). x2 = x3 ^ t1; x3 = t2; // Results of x2 and x3 now swapped. } while(--r != 0); // Repeats seven more rounds. // Final output transform (4 steps). // 1) Multiply (modulo 0x10001), 1st text-block // with 1st key sub-block. x1 = (int) ((long) x1 * key[ik++] % 0x10001L & 0xffff); // 2) Add (modulo 0x10000), 2nd text sub-block // with 2nd key sub-block. It says x3, but that is to undo swap // of subblocks 2 and 3 in 8th processing round. x3 = x3 + key[ik++] & 0xffff; // 3) Add (modulo 0x10000), 3rd text sub-block // with 3rd key sub-block. It says x2, but that is to undo swap // of subblocks 2 and 3 in 8th processing round. x2 = x2 + key[ik++] & 0xffff; // 4) Multiply (modulo 0x10001), 4th text-block // with 4th key sub-block. x4 = (int) ((long) x4 * key[ik++] % 0x10001L & 0xffff); // Repackage from 16-bit sub-blocks to 8-bit byte array text2. text2[i2++] = (byte) x1; text2[i2++] = (byte) (x1 >>> 8); text2[i2++] = (byte) x3; // x3 and x2 are switched text2[i2++] = (byte) (x3 >>> 8); // only in name. text2[i2++] = (byte) x2; text2[i2++] = (byte) (x2 >>> 8); text2[i2++] = (byte) x4; text2[i2++] = (byte) (x4 >>> 8);} // End for loop.} // End routine./** mul** Performs multiplication, modulo (2**16)+1. This code is structured* on the assumption that untaken branches are cheaper than taken* branches, and that the compiler doesn't schedule branches.* Java: Must work with 32-bit int and one 64-bit long to keep* 16-bit values and their products "unsigned." The routine assumes* that both a and b could fit in 16 bits even though they come in* as 32-bit ints. Lots of "& 0xFFFF" masks here to keep things 16-bit.* Also, because the routine stores mod (2**16)+1 results in a 2**16* space, the result is truncated to zero whenever the result would* zero, be 2**16. And if one of the multiplicands is 0, the result* is not zero, but (2**16) + 1 minus the other multiplicand (sort* of an additive inverse mod 0x10001).* NOTE: The java conversion of this routine works correctly, but* is half the speed of using Java's modulus division function (%)* on the multiplication with a 16-bit masking of the result--running* in the Symantec Caje IDE. So it's not called for now; the test* uses Java % instead.*/private int mul(int a, int b) throws ArithmeticException{ long p; // Large enough to catch 16-bit multiply // without hitting sign bit. if (a != 0) { if(b != 0) { p = (long) a * b; b = (int) p & 0xFFFF; // Lower 16 bits. a = (int) p >>> 16; // Upper 16 bits. return (b - a + (b < a ? 1 : 0) & 0xFFFF); } else return ((1 - a) & 0xFFFF); // If b = 0, then same as // 0x10001 - a. } else // If a = 0, then return return((1 - b) & 0xFFFF); // same as 0x10001 - b.}/** inv** Compute multiplicative inverse of x, modulo (2**16)+1 using* extended Euclid's GCD (greatest common divisor) algorithm.* It is unrolled twice to avoid swapping the meaning of* the registers. And some subtracts are changed to adds.* Java: Though it uses signed 32-bit ints, the interpretation* of the bits within is strictly unsigned 16-bit.*/private int inv(int x){ int t0, t1; int q, y; if (x <= 1) // Assumes positive x. return(x); // 0 and 1 are self-inverse. t1 = 0x10001 / x; // (2**16+1)/x; x is >= 2, so fits 16 bits. y = 0x10001 % x; if (y == 1) return((1 - t1) & 0xFFFF); t0 = 1; do { q = x / y; x = x % y; t0 += q * t1; if (x == 1) return(t0); q = y / x; y = y % x; t1 += q * t0; } while (y != 1); return((1 - t1) & 0xFFFF);}/** freeTestData** Nulls arrays and forces garbage collection to free up memory.*/void freeTestData(){ plain1 = null; crypt1 = null; plain2 = null; p_plain1 = null; p_crypt1 = null; p_plain2 = null; userkey = null; Z = null; DK = null; System.gc(); // Force garbage collection.}}
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -