Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

DesCipher.java 18KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496
  1. //
  2. // This DES class has been extracted from package Acme.Crypto for use in VNC.
  3. // The bytebit[] array has been reversed so that the most significant bit
  4. // in each byte of the key is ignored, not the least significant. Also the
  5. // unnecessary odd parity code has been removed.
  6. //
  7. // These changes are:
  8. // Copyright (C) 1999 AT&T Laboratories Cambridge. All Rights Reserved.
  9. //
  10. // This software is distributed in the hope that it will be useful,
  11. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  13. //
  14. // DesCipher - the DES encryption method
  15. //
  16. // The meat of this code is by Dave Zimmerman <dzimm@widget.com>, and is:
  17. //
  18. // Copyright (c) 1996 Widget Workshop, Inc. All Rights Reserved.
  19. //
  20. // Permission to use, copy, modify, and distribute this software
  21. // and its documentation for NON-COMMERCIAL or COMMERCIAL purposes and
  22. // without fee is hereby granted, provided that this copyright notice is kept
  23. // intact.
  24. //
  25. // WIDGET WORKSHOP MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY
  26. // OF THE SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
  27. // TO THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
  28. // PARTICULAR PURPOSE, OR NON-INFRINGEMENT. WIDGET WORKSHOP SHALL NOT BE LIABLE
  29. // FOR ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR
  30. // DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES.
  31. //
  32. // THIS SOFTWARE IS NOT DESIGNED OR INTENDED FOR USE OR RESALE AS ON-LINE
  33. // CONTROL EQUIPMENT IN HAZARDOUS ENVIRONMENTS REQUIRING FAIL-SAFE
  34. // PERFORMANCE, SUCH AS IN THE OPERATION OF NUCLEAR FACILITIES, AIRCRAFT
  35. // NAVIGATION OR COMMUNICATION SYSTEMS, AIR TRAFFIC CONTROL, DIRECT LIFE
  36. // SUPPORT MACHINES, OR WEAPONS SYSTEMS, IN WHICH THE FAILURE OF THE
  37. // SOFTWARE COULD LEAD DIRECTLY TO DEATH, PERSONAL INJURY, OR SEVERE
  38. // PHYSICAL OR ENVIRONMENTAL DAMAGE ("HIGH RISK ACTIVITIES"). WIDGET WORKSHOP
  39. // SPECIFICALLY DISCLAIMS ANY EXPRESS OR IMPLIED WARRANTY OF FITNESS FOR
  40. // HIGH RISK ACTIVITIES.
  41. //
  42. //
  43. // The rest is:
  44. //
  45. // Copyright (C) 1996 by Jef Poskanzer <jef@acme.com>. All rights reserved.
  46. //
  47. // Redistribution and use in source and binary forms, with or without
  48. // modification, are permitted provided that the following conditions
  49. // are met:
  50. // 1. Redistributions of source code must retain the above copyright
  51. // notice, this list of conditions and the following disclaimer.
  52. // 2. Redistributions in binary form must reproduce the above copyright
  53. // notice, this list of conditions and the following disclaimer in the
  54. // documentation and/or other materials provided with the distribution.
  55. //
  56. // THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
  57. // ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  58. // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  59. // ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
  60. // FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  61. // DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  62. // OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  63. // HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  64. // LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  65. // OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  66. // SUCH DAMAGE.
  67. //
  68. // Visit the ACME Labs Java page for up-to-date versions of this and other
  69. // fine Java utilities: http://www.acme.com/java/
  70. import java.io.*;
  71. /// The DES encryption method.
  72. // <P>
  73. // This is surprisingly fast, for pure Java. On a SPARC 20, wrapped
  74. // in Acme.Crypto.EncryptedOutputStream or Acme.Crypto.EncryptedInputStream,
  75. // it does around 7000 bytes/second.
  76. // <P>
  77. // Most of this code is by Dave Zimmerman <dzimm@widget.com>, and is
  78. // Copyright (c) 1996 Widget Workshop, Inc. See the source file for details.
  79. // <P>
  80. // <A HREF="/resources/classes/Acme/Crypto/DesCipher.java">Fetch the software.</A><BR>
  81. // <A HREF="/resources/classes/Acme.tar.Z">Fetch the entire Acme package.</A>
  82. // <P>
  83. // @see Des3Cipher
  84. // @see EncryptedOutputStream
  85. // @see EncryptedInputStream
  86. public class DesCipher
  87. {
  88. // Constructor, byte-array key.
  89. public DesCipher( byte[] key )
  90. {
  91. setKey( key );
  92. }
  93. // Key routines.
  94. private int[] encryptKeys = new int[32];
  95. private int[] decryptKeys = new int[32];
  96. /// Set the key.
  97. public void setKey( byte[] key )
  98. {
  99. deskey( key, true, encryptKeys );
  100. deskey( key, false, decryptKeys );
  101. }
  102. // Turn an 8-byte key into internal keys.
  103. private void deskey( byte[] keyBlock, boolean encrypting, int[] KnL )
  104. {
  105. int i, j, l, m, n;
  106. int[] pc1m = new int[56];
  107. int[] pcr = new int[56];
  108. int[] kn = new int[32];
  109. for ( j = 0; j < 56; ++j )
  110. {
  111. l = pc1[j];
  112. m = l & 07;
  113. pc1m[j] = ( (keyBlock[l >>> 3] & bytebit[m]) != 0 )? 1: 0;
  114. }
  115. for ( i = 0; i < 16; ++i )
  116. {
  117. if ( encrypting )
  118. m = i << 1;
  119. else
  120. m = (15-i) << 1;
  121. n = m+1;
  122. kn[m] = kn[n] = 0;
  123. for ( j = 0; j < 28; ++j )
  124. {
  125. l = j+totrot[i];
  126. if ( l < 28 )
  127. pcr[j] = pc1m[l];
  128. else
  129. pcr[j] = pc1m[l-28];
  130. }
  131. for ( j=28; j < 56; ++j )
  132. {
  133. l = j+totrot[i];
  134. if ( l < 56 )
  135. pcr[j] = pc1m[l];
  136. else
  137. pcr[j] = pc1m[l-28];
  138. }
  139. for ( j = 0; j < 24; ++j )
  140. {
  141. if ( pcr[pc2[j]] != 0 )
  142. kn[m] |= bigbyte[j];
  143. if ( pcr[pc2[j+24]] != 0 )
  144. kn[n] |= bigbyte[j];
  145. }
  146. }
  147. cookey( kn, KnL );
  148. }
  149. private void cookey( int[] raw, int KnL[] )
  150. {
  151. int raw0, raw1;
  152. int rawi, KnLi;
  153. int i;
  154. for ( i = 0, rawi = 0, KnLi = 0; i < 16; ++i )
  155. {
  156. raw0 = raw[rawi++];
  157. raw1 = raw[rawi++];
  158. KnL[KnLi] = (raw0 & 0x00fc0000) << 6;
  159. KnL[KnLi] |= (raw0 & 0x00000fc0) << 10;
  160. KnL[KnLi] |= (raw1 & 0x00fc0000) >>> 10;
  161. KnL[KnLi] |= (raw1 & 0x00000fc0) >>> 6;
  162. ++KnLi;
  163. KnL[KnLi] = (raw0 & 0x0003f000) << 12;
  164. KnL[KnLi] |= (raw0 & 0x0000003f) << 16;
  165. KnL[KnLi] |= (raw1 & 0x0003f000) >>> 4;
  166. KnL[KnLi] |= (raw1 & 0x0000003f);
  167. ++KnLi;
  168. }
  169. }
  170. // Block encryption routines.
  171. private int[] tempInts = new int[2];
  172. /// Encrypt a block of eight bytes.
  173. public void encrypt( byte[] clearText, int clearOff, byte[] cipherText, int cipherOff )
  174. {
  175. squashBytesToInts( clearText, clearOff, tempInts, 0, 2 );
  176. des( tempInts, tempInts, encryptKeys );
  177. spreadIntsToBytes( tempInts, 0, cipherText, cipherOff, 2 );
  178. }
  179. /// Decrypt a block of eight bytes.
  180. public void decrypt( byte[] cipherText, int cipherOff, byte[] clearText, int clearOff )
  181. {
  182. squashBytesToInts( cipherText, cipherOff, tempInts, 0, 2 );
  183. des( tempInts, tempInts, decryptKeys );
  184. spreadIntsToBytes( tempInts, 0, clearText, clearOff, 2 );
  185. }
  186. // The DES function.
  187. private void des( int[] inInts, int[] outInts, int[] keys )
  188. {
  189. int fval, work, right, leftt;
  190. int round;
  191. int keysi = 0;
  192. leftt = inInts[0];
  193. right = inInts[1];
  194. work = ((leftt >>> 4) ^ right) & 0x0f0f0f0f;
  195. right ^= work;
  196. leftt ^= (work << 4);
  197. work = ((leftt >>> 16) ^ right) & 0x0000ffff;
  198. right ^= work;
  199. leftt ^= (work << 16);
  200. work = ((right >>> 2) ^ leftt) & 0x33333333;
  201. leftt ^= work;
  202. right ^= (work << 2);
  203. work = ((right >>> 8) ^ leftt) & 0x00ff00ff;
  204. leftt ^= work;
  205. right ^= (work << 8);
  206. right = (right << 1) | ((right >>> 31) & 1);
  207. work = (leftt ^ right) & 0xaaaaaaaa;
  208. leftt ^= work;
  209. right ^= work;
  210. leftt = (leftt << 1) | ((leftt >>> 31) & 1);
  211. for ( round = 0; round < 8; ++round )
  212. {
  213. work = (right << 28) | (right >>> 4);
  214. work ^= keys[keysi++];
  215. fval = SP7[ work & 0x0000003f ];
  216. fval |= SP5[(work >>> 8) & 0x0000003f ];
  217. fval |= SP3[(work >>> 16) & 0x0000003f ];
  218. fval |= SP1[(work >>> 24) & 0x0000003f ];
  219. work = right ^ keys[keysi++];
  220. fval |= SP8[ work & 0x0000003f ];
  221. fval |= SP6[(work >>> 8) & 0x0000003f ];
  222. fval |= SP4[(work >>> 16) & 0x0000003f ];
  223. fval |= SP2[(work >>> 24) & 0x0000003f ];
  224. leftt ^= fval;
  225. work = (leftt << 28) | (leftt >>> 4);
  226. work ^= keys[keysi++];
  227. fval = SP7[ work & 0x0000003f ];
  228. fval |= SP5[(work >>> 8) & 0x0000003f ];
  229. fval |= SP3[(work >>> 16) & 0x0000003f ];
  230. fval |= SP1[(work >>> 24) & 0x0000003f ];
  231. work = leftt ^ keys[keysi++];
  232. fval |= SP8[ work & 0x0000003f ];
  233. fval |= SP6[(work >>> 8) & 0x0000003f ];
  234. fval |= SP4[(work >>> 16) & 0x0000003f ];
  235. fval |= SP2[(work >>> 24) & 0x0000003f ];
  236. right ^= fval;
  237. }
  238. right = (right << 31) | (right >>> 1);
  239. work = (leftt ^ right) & 0xaaaaaaaa;
  240. leftt ^= work;
  241. right ^= work;
  242. leftt = (leftt << 31) | (leftt >>> 1);
  243. work = ((leftt >>> 8) ^ right) & 0x00ff00ff;
  244. right ^= work;
  245. leftt ^= (work << 8);
  246. work = ((leftt >>> 2) ^ right) & 0x33333333;
  247. right ^= work;
  248. leftt ^= (work << 2);
  249. work = ((right >>> 16) ^ leftt) & 0x0000ffff;
  250. leftt ^= work;
  251. right ^= (work << 16);
  252. work = ((right >>> 4) ^ leftt) & 0x0f0f0f0f;
  253. leftt ^= work;
  254. right ^= (work << 4);
  255. outInts[0] = right;
  256. outInts[1] = leftt;
  257. }
  258. // Tables, permutations, S-boxes, etc.
  259. private static byte[] bytebit = {
  260. (byte)0x01, (byte)0x02, (byte)0x04, (byte)0x08,
  261. (byte)0x10, (byte)0x20, (byte)0x40, (byte)0x80
  262. };
  263. private static int[] bigbyte = {
  264. 0x800000, 0x400000, 0x200000, 0x100000,
  265. 0x080000, 0x040000, 0x020000, 0x010000,
  266. 0x008000, 0x004000, 0x002000, 0x001000,
  267. 0x000800, 0x000400, 0x000200, 0x000100,
  268. 0x000080, 0x000040, 0x000020, 0x000010,
  269. 0x000008, 0x000004, 0x000002, 0x000001
  270. };
  271. private static byte[] pc1 = {
  272. (byte)56, (byte)48, (byte)40, (byte)32, (byte)24, (byte)16, (byte) 8,
  273. (byte) 0, (byte)57, (byte)49, (byte)41, (byte)33, (byte)25, (byte)17,
  274. (byte) 9, (byte) 1, (byte)58, (byte)50, (byte)42, (byte)34, (byte)26,
  275. (byte)18, (byte)10, (byte) 2, (byte)59, (byte)51, (byte)43, (byte)35,
  276. (byte)62, (byte)54, (byte)46, (byte)38, (byte)30, (byte)22, (byte)14,
  277. (byte) 6, (byte)61, (byte)53, (byte)45, (byte)37, (byte)29, (byte)21,
  278. (byte)13, (byte) 5, (byte)60, (byte)52, (byte)44, (byte)36, (byte)28,
  279. (byte)20, (byte)12, (byte) 4, (byte)27, (byte)19, (byte)11, (byte)3
  280. };
  281. private static int[] totrot = {
  282. 1, 2, 4, 6, 8, 10, 12, 14, 15, 17, 19, 21, 23, 25, 27, 28
  283. };
  284. private static byte[] pc2 = {
  285. (byte)13, (byte)16, (byte)10, (byte)23, (byte) 0, (byte) 4,
  286. (byte) 2, (byte)27, (byte)14, (byte) 5, (byte)20, (byte) 9,
  287. (byte)22, (byte)18, (byte)11, (byte)3 , (byte)25, (byte) 7,
  288. (byte)15, (byte) 6, (byte)26, (byte)19, (byte)12, (byte) 1,
  289. (byte)40, (byte)51, (byte)30, (byte)36, (byte)46, (byte)54,
  290. (byte)29, (byte)39, (byte)50, (byte)44, (byte)32, (byte)47,
  291. (byte)43, (byte)48, (byte)38, (byte)55, (byte)33, (byte)52,
  292. (byte)45, (byte)41, (byte)49, (byte)35, (byte)28, (byte)31,
  293. };
  294. private static int[] SP1 = {
  295. 0x01010400, 0x00000000, 0x00010000, 0x01010404,
  296. 0x01010004, 0x00010404, 0x00000004, 0x00010000,
  297. 0x00000400, 0x01010400, 0x01010404, 0x00000400,
  298. 0x01000404, 0x01010004, 0x01000000, 0x00000004,
  299. 0x00000404, 0x01000400, 0x01000400, 0x00010400,
  300. 0x00010400, 0x01010000, 0x01010000, 0x01000404,
  301. 0x00010004, 0x01000004, 0x01000004, 0x00010004,
  302. 0x00000000, 0x00000404, 0x00010404, 0x01000000,
  303. 0x00010000, 0x01010404, 0x00000004, 0x01010000,
  304. 0x01010400, 0x01000000, 0x01000000, 0x00000400,
  305. 0x01010004, 0x00010000, 0x00010400, 0x01000004,
  306. 0x00000400, 0x00000004, 0x01000404, 0x00010404,
  307. 0x01010404, 0x00010004, 0x01010000, 0x01000404,
  308. 0x01000004, 0x00000404, 0x00010404, 0x01010400,
  309. 0x00000404, 0x01000400, 0x01000400, 0x00000000,
  310. 0x00010004, 0x00010400, 0x00000000, 0x01010004
  311. };
  312. private static int[] SP2 = {
  313. 0x80108020, 0x80008000, 0x00008000, 0x00108020,
  314. 0x00100000, 0x00000020, 0x80100020, 0x80008020,
  315. 0x80000020, 0x80108020, 0x80108000, 0x80000000,
  316. 0x80008000, 0x00100000, 0x00000020, 0x80100020,
  317. 0x00108000, 0x00100020, 0x80008020, 0x00000000,
  318. 0x80000000, 0x00008000, 0x00108020, 0x80100000,
  319. 0x00100020, 0x80000020, 0x00000000, 0x00108000,
  320. 0x00008020, 0x80108000, 0x80100000, 0x00008020,
  321. 0x00000000, 0x00108020, 0x80100020, 0x00100000,
  322. 0x80008020, 0x80100000, 0x80108000, 0x00008000,
  323. 0x80100000, 0x80008000, 0x00000020, 0x80108020,
  324. 0x00108020, 0x00000020, 0x00008000, 0x80000000,
  325. 0x00008020, 0x80108000, 0x00100000, 0x80000020,
  326. 0x00100020, 0x80008020, 0x80000020, 0x00100020,
  327. 0x00108000, 0x00000000, 0x80008000, 0x00008020,
  328. 0x80000000, 0x80100020, 0x80108020, 0x00108000
  329. };
  330. private static int[] SP3 = {
  331. 0x00000208, 0x08020200, 0x00000000, 0x08020008,
  332. 0x08000200, 0x00000000, 0x00020208, 0x08000200,
  333. 0x00020008, 0x08000008, 0x08000008, 0x00020000,
  334. 0x08020208, 0x00020008, 0x08020000, 0x00000208,
  335. 0x08000000, 0x00000008, 0x08020200, 0x00000200,
  336. 0x00020200, 0x08020000, 0x08020008, 0x00020208,
  337. 0x08000208, 0x00020200, 0x00020000, 0x08000208,
  338. 0x00000008, 0x08020208, 0x00000200, 0x08000000,
  339. 0x08020200, 0x08000000, 0x00020008, 0x00000208,
  340. 0x00020000, 0x08020200, 0x08000200, 0x00000000,
  341. 0x00000200, 0x00020008, 0x08020208, 0x08000200,
  342. 0x08000008, 0x00000200, 0x00000000, 0x08020008,
  343. 0x08000208, 0x00020000, 0x08000000, 0x08020208,
  344. 0x00000008, 0x00020208, 0x00020200, 0x08000008,
  345. 0x08020000, 0x08000208, 0x00000208, 0x08020000,
  346. 0x00020208, 0x00000008, 0x08020008, 0x00020200
  347. };
  348. private static int[] SP4 = {
  349. 0x00802001, 0x00002081, 0x00002081, 0x00000080,
  350. 0x00802080, 0x00800081, 0x00800001, 0x00002001,
  351. 0x00000000, 0x00802000, 0x00802000, 0x00802081,
  352. 0x00000081, 0x00000000, 0x00800080, 0x00800001,
  353. 0x00000001, 0x00002000, 0x00800000, 0x00802001,
  354. 0x00000080, 0x00800000, 0x00002001, 0x00002080,
  355. 0x00800081, 0x00000001, 0x00002080, 0x00800080,
  356. 0x00002000, 0x00802080, 0x00802081, 0x00000081,
  357. 0x00800080, 0x00800001, 0x00802000, 0x00802081,
  358. 0x00000081, 0x00000000, 0x00000000, 0x00802000,
  359. 0x00002080, 0x00800080, 0x00800081, 0x00000001,
  360. 0x00802001, 0x00002081, 0x00002081, 0x00000080,
  361. 0x00802081, 0x00000081, 0x00000001, 0x00002000,
  362. 0x00800001, 0x00002001, 0x00802080, 0x00800081,
  363. 0x00002001, 0x00002080, 0x00800000, 0x00802001,
  364. 0x00000080, 0x00800000, 0x00002000, 0x00802080
  365. };
  366. private static int[] SP5 = {
  367. 0x00000100, 0x02080100, 0x02080000, 0x42000100,
  368. 0x00080000, 0x00000100, 0x40000000, 0x02080000,
  369. 0x40080100, 0x00080000, 0x02000100, 0x40080100,
  370. 0x42000100, 0x42080000, 0x00080100, 0x40000000,
  371. 0x02000000, 0x40080000, 0x40080000, 0x00000000,
  372. 0x40000100, 0x42080100, 0x42080100, 0x02000100,
  373. 0x42080000, 0x40000100, 0x00000000, 0x42000000,
  374. 0x02080100, 0x02000000, 0x42000000, 0x00080100,
  375. 0x00080000, 0x42000100, 0x00000100, 0x02000000,
  376. 0x40000000, 0x02080000, 0x42000100, 0x40080100,
  377. 0x02000100, 0x40000000, 0x42080000, 0x02080100,
  378. 0x40080100, 0x00000100, 0x02000000, 0x42080000,
  379. 0x42080100, 0x00080100, 0x42000000, 0x42080100,
  380. 0x02080000, 0x00000000, 0x40080000, 0x42000000,
  381. 0x00080100, 0x02000100, 0x40000100, 0x00080000,
  382. 0x00000000, 0x40080000, 0x02080100, 0x40000100
  383. };
  384. private static int[] SP6 = {
  385. 0x20000010, 0x20400000, 0x00004000, 0x20404010,
  386. 0x20400000, 0x00000010, 0x20404010, 0x00400000,
  387. 0x20004000, 0x00404010, 0x00400000, 0x20000010,
  388. 0x00400010, 0x20004000, 0x20000000, 0x00004010,
  389. 0x00000000, 0x00400010, 0x20004010, 0x00004000,
  390. 0x00404000, 0x20004010, 0x00000010, 0x20400010,
  391. 0x20400010, 0x00000000, 0x00404010, 0x20404000,
  392. 0x00004010, 0x00404000, 0x20404000, 0x20000000,
  393. 0x20004000, 0x00000010, 0x20400010, 0x00404000,
  394. 0x20404010, 0x00400000, 0x00004010, 0x20000010,
  395. 0x00400000, 0x20004000, 0x20000000, 0x00004010,
  396. 0x20000010, 0x20404010, 0x00404000, 0x20400000,
  397. 0x00404010, 0x20404000, 0x00000000, 0x20400010,
  398. 0x00000010, 0x00004000, 0x20400000, 0x00404010,
  399. 0x00004000, 0x00400010, 0x20004010, 0x00000000,
  400. 0x20404000, 0x20000000, 0x00400010, 0x20004010
  401. };
  402. private static int[] SP7 = {
  403. 0x00200000, 0x04200002, 0x04000802, 0x00000000,
  404. 0x00000800, 0x04000802, 0x00200802, 0x04200800,
  405. 0x04200802, 0x00200000, 0x00000000, 0x04000002,
  406. 0x00000002, 0x04000000, 0x04200002, 0x00000802,
  407. 0x04000800, 0x00200802, 0x00200002, 0x04000800,
  408. 0x04000002, 0x04200000, 0x04200800, 0x00200002,
  409. 0x04200000, 0x00000800, 0x00000802, 0x04200802,
  410. 0x00200800, 0x00000002, 0x04000000, 0x00200800,
  411. 0x04000000, 0x00200800, 0x00200000, 0x04000802,
  412. 0x04000802, 0x04200002, 0x04200002, 0x00000002,
  413. 0x00200002, 0x04000000, 0x04000800, 0x00200000,
  414. 0x04200800, 0x00000802, 0x00200802, 0x04200800,
  415. 0x00000802, 0x04000002, 0x04200802, 0x04200000,
  416. 0x00200800, 0x00000000, 0x00000002, 0x04200802,
  417. 0x00000000, 0x00200802, 0x04200000, 0x00000800,
  418. 0x04000002, 0x04000800, 0x00000800, 0x00200002
  419. };
  420. private static int[] SP8 = {
  421. 0x10001040, 0x00001000, 0x00040000, 0x10041040,
  422. 0x10000000, 0x10001040, 0x00000040, 0x10000000,
  423. 0x00040040, 0x10040000, 0x10041040, 0x00041000,
  424. 0x10041000, 0x00041040, 0x00001000, 0x00000040,
  425. 0x10040000, 0x10000040, 0x10001000, 0x00001040,
  426. 0x00041000, 0x00040040, 0x10040040, 0x10041000,
  427. 0x00001040, 0x00000000, 0x00000000, 0x10040040,
  428. 0x10000040, 0x10001000, 0x00041040, 0x00040000,
  429. 0x00041040, 0x00040000, 0x10041000, 0x00001000,
  430. 0x00000040, 0x10040040, 0x00001000, 0x00041040,
  431. 0x10001000, 0x00000040, 0x10000040, 0x10040000,
  432. 0x10040040, 0x10000000, 0x00040000, 0x10001040,
  433. 0x00000000, 0x10041040, 0x00040040, 0x10000040,
  434. 0x10040000, 0x10001000, 0x10001040, 0x00000000,
  435. 0x10041040, 0x00041000, 0x00041000, 0x00001040,
  436. 0x00001040, 0x00040040, 0x10000000, 0x10041000
  437. };
  438. // Routines taken from other parts of the Acme utilities.
  439. /// Squash bytes down to ints.
  440. public static void squashBytesToInts( byte[] inBytes, int inOff, int[] outInts, int outOff, int intLen )
  441. {
  442. for ( int i = 0; i < intLen; ++i )
  443. outInts[outOff + i] =
  444. ( ( inBytes[inOff + i * 4 ] & 0xff ) << 24 ) |
  445. ( ( inBytes[inOff + i * 4 + 1] & 0xff ) << 16 ) |
  446. ( ( inBytes[inOff + i * 4 + 2] & 0xff ) << 8 ) |
  447. ( inBytes[inOff + i * 4 + 3] & 0xff );
  448. }
  449. /// Spread ints into bytes.
  450. public static void spreadIntsToBytes( int[] inInts, int inOff, byte[] outBytes, int outOff, int intLen )
  451. {
  452. for ( int i = 0; i < intLen; ++i )
  453. {
  454. outBytes[outOff + i * 4 ] = (byte) ( inInts[inOff + i] >>> 24 );
  455. outBytes[outOff + i * 4 + 1] = (byte) ( inInts[inOff + i] >>> 16 );
  456. outBytes[outOff + i * 4 + 2] = (byte) ( inInts[inOff + i] >>> 8 );
  457. outBytes[outOff + i * 4 + 3] = (byte) inInts[inOff + i];
  458. }
  459. }
  460. }