BlowfishEngine.java 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308
  1. /*
  2. * This program is free software: you can redistribute it and/or modify it under
  3. * the terms of the GNU General Public License as published by the Free Software
  4. * Foundation, either version 3 of the License, or (at your option) any later
  5. * version.
  6. *
  7. * This program is distributed in the hope that it will be useful, but WITHOUT
  8. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  9. * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
  10. * details.
  11. *
  12. * You should have received a copy of the GNU General Public License along with
  13. * this program. If not, see <http://www.gnu.org/licenses/>.
  14. */
  15. package com.l2jserver.util.crypt;
  16. import java.io.IOException;
  17. /**
  18. * This file is based on the Blowfish Engine that is part of the BouncyCastle JCE.<br>
  19. * Copyright (c) 2000 The Legion Of The Bouncy Castle (http://www.bouncycastle.org)<br>
  20. * Permission is hereby granted, free of charge, to any person obtaining a copy of this software<br>
  21. * and associated documentation files (the "Software"), to deal in the Software without restriction,<br>
  22. * including without limitation the rights to use, copy, modify, merge, publish, distribute,<br>
  23. * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is<br>
  24. * furnished to do so, subject to the following conditions:<br>
  25. * <br>
  26. * The above copyright notice and this permission notice shall be included in all copies<br>
  27. * or substantial portions of the Software.<br>
  28. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,<br>
  29. * INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR<br>
  30. * PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE<br>
  31. * FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,<br>
  32. * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  33. */
  34. public class BlowfishEngine
  35. {
  36. private static final int[] KP =
  37. {
  38. 0x243F6A88,
  39. 0x85A308D3,
  40. 0x13198A2E,
  41. 0x03707344,
  42. 0xA4093822,
  43. 0x299F31D0,
  44. 0x082EFA98,
  45. 0xEC4E6C89,
  46. 0x452821E6,
  47. 0x38D01377,
  48. 0xBE5466CF,
  49. 0x34E90C6C,
  50. 0xC0AC29B7,
  51. 0xC97C50DD,
  52. 0x3F84D5B5,
  53. 0xB5470917,
  54. 0x9216D5D9,
  55. 0x8979FB1B
  56. };
  57. private static final int[] KS0 =
  58. {
  59. 0xD1310BA6,
  60. 0x98DFB5AC,
  61. 0x2FFD72DB,
  62. 0xD01ADFB7,
  63. 0xB8E1AFED,
  64. 0x6A267E96,
  65. 0xBA7C9045,
  66. 0xF12C7F99,
  67. 0x24A19947,
  68. 0xB3916CF7,
  69. 0x0801F2E2,
  70. 0x858EFC16,
  71. 0x636920D8,
  72. 0x71574E69,
  73. 0xA458FEA3,
  74. 0xF4933D7E,
  75. 0x0D95748F,
  76. 0x728EB658,
  77. 0x718BCD58,
  78. 0x82154AEE,
  79. 0x7B54A41D,
  80. 0xC25A59B5,
  81. 0x9C30D539,
  82. 0x2AF26013,
  83. 0xC5D1B023,
  84. 0x286085F0,
  85. 0xCA417918,
  86. 0xB8DB38EF,
  87. 0x8E79DCB0,
  88. 0x603A180E,
  89. 0x6C9E0E8B,
  90. 0xB01E8A3E,
  91. 0xD71577C1,
  92. 0xBD314B27,
  93. 0x78AF2FDA,
  94. 0x55605C60,
  95. 0xE65525F3,
  96. 0xAA55AB94,
  97. 0x57489862,
  98. 0x63E81440,
  99. 0x55CA396A,
  100. 0x2AAB10B6,
  101. 0xB4CC5C34,
  102. 0x1141E8CE,
  103. 0xA15486AF,
  104. 0x7C72E993,
  105. 0xB3EE1411,
  106. 0x636FBC2A,
  107. 0x2BA9C55D,
  108. 0x741831F6,
  109. 0xCE5C3E16,
  110. 0x9B87931E,
  111. 0xAFD6BA33,
  112. 0x6C24CF5C,
  113. 0x7A325381,
  114. 0x28958677,
  115. 0x3B8F4898,
  116. 0x6B4BB9AF,
  117. 0xC4BFE81B,
  118. 0x66282193,
  119. 0x61D809CC,
  120. 0xFB21A991,
  121. 0x487CAC60,
  122. 0x5DEC8032,
  123. 0xEF845D5D,
  124. 0xE98575B1,
  125. 0xDC262302,
  126. 0xEB651B88,
  127. 0x23893E81,
  128. 0xD396ACC5,
  129. 0x0F6D6FF3,
  130. 0x83F44239,
  131. 0x2E0B4482,
  132. 0xA4842004,
  133. 0x69C8F04A,
  134. 0x9E1F9B5E,
  135. 0x21C66842,
  136. 0xF6E96C9A,
  137. 0x670C9C61,
  138. 0xABD388F0,
  139. 0x6A51A0D2,
  140. 0xD8542F68,
  141. 0x960FA728,
  142. 0xAB5133A3,
  143. 0x6EEF0B6C,
  144. 0x137A3BE4,
  145. 0xBA3BF050,
  146. 0x7EFB2A98,
  147. 0xA1F1651D,
  148. 0x39AF0176,
  149. 0x66CA593E,
  150. 0x82430E88,
  151. 0x8CEE8619,
  152. 0x456F9FB4,
  153. 0x7D84A5C3,
  154. 0x3B8B5EBE,
  155. 0xE06F75D8,
  156. 0x85C12073,
  157. 0x401A449F,
  158. 0x56C16AA6,
  159. 0x4ED3AA62,
  160. 0x363F7706,
  161. 0x1BFEDF72,
  162. 0x429B023D,
  163. 0x37D0D724,
  164. 0xD00A1248,
  165. 0xDB0FEAD3,
  166. 0x49F1C09B,
  167. 0x075372C9,
  168. 0x80991B7B,
  169. 0x25D479D8,
  170. 0xF6E8DEF7,
  171. 0xE3FE501A,
  172. 0xB6794C3B,
  173. 0x976CE0BD,
  174. 0x04C006BA,
  175. 0xC1A94FB6,
  176. 0x409F60C4,
  177. 0x5E5C9EC2,
  178. 0x196A2463,
  179. 0x68FB6FAF,
  180. 0x3E6C53B5,
  181. 0x1339B2EB,
  182. 0x3B52EC6F,
  183. 0x6DFC511F,
  184. 0x9B30952C,
  185. 0xCC814544,
  186. 0xAF5EBD09,
  187. 0xBEE3D004,
  188. 0xDE334AFD,
  189. 0x660F2807,
  190. 0x192E4BB3,
  191. 0xC0CBA857,
  192. 0x45C8740F,
  193. 0xD20B5F39,
  194. 0xB9D3FBDB,
  195. 0x5579C0BD,
  196. 0x1A60320A,
  197. 0xD6A100C6,
  198. 0x402C7279,
  199. 0x679F25FE,
  200. 0xFB1FA3CC,
  201. 0x8EA5E9F8,
  202. 0xDB3222F8,
  203. 0x3C7516DF,
  204. 0xFD616B15,
  205. 0x2F501EC8,
  206. 0xAD0552AB,
  207. 0x323DB5FA,
  208. 0xFD238760,
  209. 0x53317B48,
  210. 0x3E00DF82,
  211. 0x9E5C57BB,
  212. 0xCA6F8CA0,
  213. 0x1A87562E,
  214. 0xDF1769DB,
  215. 0xD542A8F6,
  216. 0x287EFFC3,
  217. 0xAC6732C6,
  218. 0x8C4F5573,
  219. 0x695B27B0,
  220. 0xBBCA58C8,
  221. 0xE1FFA35D,
  222. 0xB8F011A0,
  223. 0x10FA3D98,
  224. 0xFD2183B8,
  225. 0x4AFCB56C,
  226. 0x2DD1D35B,
  227. 0x9A53E479,
  228. 0xB6F84565,
  229. 0xD28E49BC,
  230. 0x4BFB9790,
  231. 0xE1DDF2DA,
  232. 0xA4CB7E33,
  233. 0x62FB1341,
  234. 0xCEE4C6E8,
  235. 0xEF20CADA,
  236. 0x36774C01,
  237. 0xD07E9EFE,
  238. 0x2BF11FB4,
  239. 0x95DBDA4D,
  240. 0xAE909198,
  241. 0xEAAD8E71,
  242. 0x6B93D5A0,
  243. 0xD08ED1D0,
  244. 0xAFC725E0,
  245. 0x8E3C5B2F,
  246. 0x8E7594B7,
  247. 0x8FF6E2FB,
  248. 0xF2122B64,
  249. 0x8888B812,
  250. 0x900DF01C,
  251. 0x4FAD5EA0,
  252. 0x688FC31C,
  253. 0xD1CFF191,
  254. 0xB3A8C1AD,
  255. 0x2F2F2218,
  256. 0xBE0E1777,
  257. 0xEA752DFE,
  258. 0x8B021FA1,
  259. 0xE5A0CC0F,
  260. 0xB56F74E8,
  261. 0x18ACF3D6,
  262. 0xCE89E299,
  263. 0xB4A84FE0,
  264. 0xFD13E0B7,
  265. 0x7CC43B81,
  266. 0xD2ADA8D9,
  267. 0x165FA266,
  268. 0x80957705,
  269. 0x93CC7314,
  270. 0x211A1477,
  271. 0xE6AD2065,
  272. 0x77B5FA86,
  273. 0xC75442F5,
  274. 0xFB9D35CF,
  275. 0xEBCDAF0C,
  276. 0x7B3E89A0,
  277. 0xD6411BD3,
  278. 0xAE1E7E49,
  279. 0x00250E2D,
  280. 0x2071B35E,
  281. 0x226800BB,
  282. 0x57B8E0AF,
  283. 0x2464369B,
  284. 0xF009B91E,
  285. 0x5563911D,
  286. 0x59DFA6AA,
  287. 0x78C14389,
  288. 0xD95A537F,
  289. 0x207D5BA2,
  290. 0x02E5B9C5,
  291. 0x83260376,
  292. 0x6295CFA9,
  293. 0x11C81968,
  294. 0x4E734A41,
  295. 0xB3472DCA,
  296. 0x7B14A94A,
  297. 0x1B510052,
  298. 0x9A532915,
  299. 0xD60F573F,
  300. 0xBC9BC6E4,
  301. 0x2B60A476,
  302. 0x81E67400,
  303. 0x08BA6FB5,
  304. 0x571BE91F,
  305. 0xF296EC6B,
  306. 0x2A0DD915,
  307. 0xB6636521,
  308. 0xE7B9F9B6,
  309. 0xFF34052E,
  310. 0xC5855664,
  311. 0x53B02D5D,
  312. 0xA99F8FA1,
  313. 0x08BA4799,
  314. 0x6E85076A
  315. };
  316. private static final int[] KS1 =
  317. {
  318. 0x4B7A70E9,
  319. 0xB5B32944,
  320. 0xDB75092E,
  321. 0xC4192623,
  322. 0xAD6EA6B0,
  323. 0x49A7DF7D,
  324. 0x9CEE60B8,
  325. 0x8FEDB266,
  326. 0xECAA8C71,
  327. 0x699A17FF,
  328. 0x5664526C,
  329. 0xC2B19EE1,
  330. 0x193602A5,
  331. 0x75094C29,
  332. 0xA0591340,
  333. 0xE4183A3E,
  334. 0x3F54989A,
  335. 0x5B429D65,
  336. 0x6B8FE4D6,
  337. 0x99F73FD6,
  338. 0xA1D29C07,
  339. 0xEFE830F5,
  340. 0x4D2D38E6,
  341. 0xF0255DC1,
  342. 0x4CDD2086,
  343. 0x8470EB26,
  344. 0x6382E9C6,
  345. 0x021ECC5E,
  346. 0x09686B3F,
  347. 0x3EBAEFC9,
  348. 0x3C971814,
  349. 0x6B6A70A1,
  350. 0x687F3584,
  351. 0x52A0E286,
  352. 0xB79C5305,
  353. 0xAA500737,
  354. 0x3E07841C,
  355. 0x7FDEAE5C,
  356. 0x8E7D44EC,
  357. 0x5716F2B8,
  358. 0xB03ADA37,
  359. 0xF0500C0D,
  360. 0xF01C1F04,
  361. 0x0200B3FF,
  362. 0xAE0CF51A,
  363. 0x3CB574B2,
  364. 0x25837A58,
  365. 0xDC0921BD,
  366. 0xD19113F9,
  367. 0x7CA92FF6,
  368. 0x94324773,
  369. 0x22F54701,
  370. 0x3AE5E581,
  371. 0x37C2DADC,
  372. 0xC8B57634,
  373. 0x9AF3DDA7,
  374. 0xA9446146,
  375. 0x0FD0030E,
  376. 0xECC8C73E,
  377. 0xA4751E41,
  378. 0xE238CD99,
  379. 0x3BEA0E2F,
  380. 0x3280BBA1,
  381. 0x183EB331,
  382. 0x4E548B38,
  383. 0x4F6DB908,
  384. 0x6F420D03,
  385. 0xF60A04BF,
  386. 0x2CB81290,
  387. 0x24977C79,
  388. 0x5679B072,
  389. 0xBCAF89AF,
  390. 0xDE9A771F,
  391. 0xD9930810,
  392. 0xB38BAE12,
  393. 0xDCCF3F2E,
  394. 0x5512721F,
  395. 0x2E6B7124,
  396. 0x501ADDE6,
  397. 0x9F84CD87,
  398. 0x7A584718,
  399. 0x7408DA17,
  400. 0xBC9F9ABC,
  401. 0xE94B7D8C,
  402. 0xEC7AEC3A,
  403. 0xDB851DFA,
  404. 0x63094366,
  405. 0xC464C3D2,
  406. 0xEF1C1847,
  407. 0x3215D908,
  408. 0xDD433B37,
  409. 0x24C2BA16,
  410. 0x12A14D43,
  411. 0x2A65C451,
  412. 0x50940002,
  413. 0x133AE4DD,
  414. 0x71DFF89E,
  415. 0x10314E55,
  416. 0x81AC77D6,
  417. 0x5F11199B,
  418. 0x043556F1,
  419. 0xD7A3C76B,
  420. 0x3C11183B,
  421. 0x5924A509,
  422. 0xF28FE6ED,
  423. 0x97F1FBFA,
  424. 0x9EBABF2C,
  425. 0x1E153C6E,
  426. 0x86E34570,
  427. 0xEAE96FB1,
  428. 0x860E5E0A,
  429. 0x5A3E2AB3,
  430. 0x771FE71C,
  431. 0x4E3D06FA,
  432. 0x2965DCB9,
  433. 0x99E71D0F,
  434. 0x803E89D6,
  435. 0x5266C825,
  436. 0x2E4CC978,
  437. 0x9C10B36A,
  438. 0xC6150EBA,
  439. 0x94E2EA78,
  440. 0xA5FC3C53,
  441. 0x1E0A2DF4,
  442. 0xF2F74EA7,
  443. 0x361D2B3D,
  444. 0x1939260F,
  445. 0x19C27960,
  446. 0x5223A708,
  447. 0xF71312B6,
  448. 0xEBADFE6E,
  449. 0xEAC31F66,
  450. 0xE3BC4595,
  451. 0xA67BC883,
  452. 0xB17F37D1,
  453. 0x018CFF28,
  454. 0xC332DDEF,
  455. 0xBE6C5AA5,
  456. 0x65582185,
  457. 0x68AB9802,
  458. 0xEECEA50F,
  459. 0xDB2F953B,
  460. 0x2AEF7DAD,
  461. 0x5B6E2F84,
  462. 0x1521B628,
  463. 0x29076170,
  464. 0xECDD4775,
  465. 0x619F1510,
  466. 0x13CCA830,
  467. 0xEB61BD96,
  468. 0x0334FE1E,
  469. 0xAA0363CF,
  470. 0xB5735C90,
  471. 0x4C70A239,
  472. 0xD59E9E0B,
  473. 0xCBAADE14,
  474. 0xEECC86BC,
  475. 0x60622CA7,
  476. 0x9CAB5CAB,
  477. 0xB2F3846E,
  478. 0x648B1EAF,
  479. 0x19BDF0CA,
  480. 0xA02369B9,
  481. 0x655ABB50,
  482. 0x40685A32,
  483. 0x3C2AB4B3,
  484. 0x319EE9D5,
  485. 0xC021B8F7,
  486. 0x9B540B19,
  487. 0x875FA099,
  488. 0x95F7997E,
  489. 0x623D7DA8,
  490. 0xF837889A,
  491. 0x97E32D77,
  492. 0x11ED935F,
  493. 0x16681281,
  494. 0x0E358829,
  495. 0xC7E61FD6,
  496. 0x96DEDFA1,
  497. 0x7858BA99,
  498. 0x57F584A5,
  499. 0x1B227263,
  500. 0x9B83C3FF,
  501. 0x1AC24696,
  502. 0xCDB30AEB,
  503. 0x532E3054,
  504. 0x8FD948E4,
  505. 0x6DBC3128,
  506. 0x58EBF2EF,
  507. 0x34C6FFEA,
  508. 0xFE28ED61,
  509. 0xEE7C3C73,
  510. 0x5D4A14D9,
  511. 0xE864B7E3,
  512. 0x42105D14,
  513. 0x203E13E0,
  514. 0x45EEE2B6,
  515. 0xA3AAABEA,
  516. 0xDB6C4F15,
  517. 0xFACB4FD0,
  518. 0xC742F442,
  519. 0xEF6ABBB5,
  520. 0x654F3B1D,
  521. 0x41CD2105,
  522. 0xD81E799E,
  523. 0x86854DC7,
  524. 0xE44B476A,
  525. 0x3D816250,
  526. 0xCF62A1F2,
  527. 0x5B8D2646,
  528. 0xFC8883A0,
  529. 0xC1C7B6A3,
  530. 0x7F1524C3,
  531. 0x69CB7492,
  532. 0x47848A0B,
  533. 0x5692B285,
  534. 0x095BBF00,
  535. 0xAD19489D,
  536. 0x1462B174,
  537. 0x23820E00,
  538. 0x58428D2A,
  539. 0x0C55F5EA,
  540. 0x1DADF43E,
  541. 0x233F7061,
  542. 0x3372F092,
  543. 0x8D937E41,
  544. 0xD65FECF1,
  545. 0x6C223BDB,
  546. 0x7CDE3759,
  547. 0xCBEE7460,
  548. 0x4085F2A7,
  549. 0xCE77326E,
  550. 0xA6078084,
  551. 0x19F8509E,
  552. 0xE8EFD855,
  553. 0x61D99735,
  554. 0xA969A7AA,
  555. 0xC50C06C2,
  556. 0x5A04ABFC,
  557. 0x800BCADC,
  558. 0x9E447A2E,
  559. 0xC3453484,
  560. 0xFDD56705,
  561. 0x0E1E9EC9,
  562. 0xDB73DBD3,
  563. 0x105588CD,
  564. 0x675FDA79,
  565. 0xE3674340,
  566. 0xC5C43465,
  567. 0x713E38D8,
  568. 0x3D28F89E,
  569. 0xF16DFF20,
  570. 0x153E21E7,
  571. 0x8FB03D4A,
  572. 0xE6E39F2B,
  573. 0xDB83ADF7
  574. };
  575. private static final int[] KS2 =
  576. {
  577. 0xE93D5A68,
  578. 0x948140F7,
  579. 0xF64C261C,
  580. 0x94692934,
  581. 0x411520F7,
  582. 0x7602D4F7,
  583. 0xBCF46B2E,
  584. 0xD4A20068,
  585. 0xD4082471,
  586. 0x3320F46A,
  587. 0x43B7D4B7,
  588. 0x500061AF,
  589. 0x1E39F62E,
  590. 0x97244546,
  591. 0x14214F74,
  592. 0xBF8B8840,
  593. 0x4D95FC1D,
  594. 0x96B591AF,
  595. 0x70F4DDD3,
  596. 0x66A02F45,
  597. 0xBFBC09EC,
  598. 0x03BD9785,
  599. 0x7FAC6DD0,
  600. 0x31CB8504,
  601. 0x96EB27B3,
  602. 0x55FD3941,
  603. 0xDA2547E6,
  604. 0xABCA0A9A,
  605. 0x28507825,
  606. 0x530429F4,
  607. 0x0A2C86DA,
  608. 0xE9B66DFB,
  609. 0x68DC1462,
  610. 0xD7486900,
  611. 0x680EC0A4,
  612. 0x27A18DEE,
  613. 0x4F3FFEA2,
  614. 0xE887AD8C,
  615. 0xB58CE006,
  616. 0x7AF4D6B6,
  617. 0xAACE1E7C,
  618. 0xD3375FEC,
  619. 0xCE78A399,
  620. 0x406B2A42,
  621. 0x20FE9E35,
  622. 0xD9F385B9,
  623. 0xEE39D7AB,
  624. 0x3B124E8B,
  625. 0x1DC9FAF7,
  626. 0x4B6D1856,
  627. 0x26A36631,
  628. 0xEAE397B2,
  629. 0x3A6EFA74,
  630. 0xDD5B4332,
  631. 0x6841E7F7,
  632. 0xCA7820FB,
  633. 0xFB0AF54E,
  634. 0xD8FEB397,
  635. 0x454056AC,
  636. 0xBA489527,
  637. 0x55533A3A,
  638. 0x20838D87,
  639. 0xFE6BA9B7,
  640. 0xD096954B,
  641. 0x55A867BC,
  642. 0xA1159A58,
  643. 0xCCA92963,
  644. 0x99E1DB33,
  645. 0xA62A4A56,
  646. 0x3F3125F9,
  647. 0x5EF47E1C,
  648. 0x9029317C,
  649. 0xFDF8E802,
  650. 0x04272F70,
  651. 0x80BB155C,
  652. 0x05282CE3,
  653. 0x95C11548,
  654. 0xE4C66D22,
  655. 0x48C1133F,
  656. 0xC70F86DC,
  657. 0x07F9C9EE,
  658. 0x41041F0F,
  659. 0x404779A4,
  660. 0x5D886E17,
  661. 0x325F51EB,
  662. 0xD59BC0D1,
  663. 0xF2BCC18F,
  664. 0x41113564,
  665. 0x257B7834,
  666. 0x602A9C60,
  667. 0xDFF8E8A3,
  668. 0x1F636C1B,
  669. 0x0E12B4C2,
  670. 0x02E1329E,
  671. 0xAF664FD1,
  672. 0xCAD18115,
  673. 0x6B2395E0,
  674. 0x333E92E1,
  675. 0x3B240B62,
  676. 0xEEBEB922,
  677. 0x85B2A20E,
  678. 0xE6BA0D99,
  679. 0xDE720C8C,
  680. 0x2DA2F728,
  681. 0xD0127845,
  682. 0x95B794FD,
  683. 0x647D0862,
  684. 0xE7CCF5F0,
  685. 0x5449A36F,
  686. 0x877D48FA,
  687. 0xC39DFD27,
  688. 0xF33E8D1E,
  689. 0x0A476341,
  690. 0x992EFF74,
  691. 0x3A6F6EAB,
  692. 0xF4F8FD37,
  693. 0xA812DC60,
  694. 0xA1EBDDF8,
  695. 0x991BE14C,
  696. 0xDB6E6B0D,
  697. 0xC67B5510,
  698. 0x6D672C37,
  699. 0x2765D43B,
  700. 0xDCD0E804,
  701. 0xF1290DC7,
  702. 0xCC00FFA3,
  703. 0xB5390F92,
  704. 0x690FED0B,
  705. 0x667B9FFB,
  706. 0xCEDB7D9C,
  707. 0xA091CF0B,
  708. 0xD9155EA3,
  709. 0xBB132F88,
  710. 0x515BAD24,
  711. 0x7B9479BF,
  712. 0x763BD6EB,
  713. 0x37392EB3,
  714. 0xCC115979,
  715. 0x8026E297,
  716. 0xF42E312D,
  717. 0x6842ADA7,
  718. 0xC66A2B3B,
  719. 0x12754CCC,
  720. 0x782EF11C,
  721. 0x6A124237,
  722. 0xB79251E7,
  723. 0x06A1BBE6,
  724. 0x4BFB6350,
  725. 0x1A6B1018,
  726. 0x11CAEDFA,
  727. 0x3D25BDD8,
  728. 0xE2E1C3C9,
  729. 0x44421659,
  730. 0x0A121386,
  731. 0xD90CEC6E,
  732. 0xD5ABEA2A,
  733. 0x64AF674E,
  734. 0xDA86A85F,
  735. 0xBEBFE988,
  736. 0x64E4C3FE,
  737. 0x9DBC8057,
  738. 0xF0F7C086,
  739. 0x60787BF8,
  740. 0x6003604D,
  741. 0xD1FD8346,
  742. 0xF6381FB0,
  743. 0x7745AE04,
  744. 0xD736FCCC,
  745. 0x83426B33,
  746. 0xF01EAB71,
  747. 0xB0804187,
  748. 0x3C005E5F,
  749. 0x77A057BE,
  750. 0xBDE8AE24,
  751. 0x55464299,
  752. 0xBF582E61,
  753. 0x4E58F48F,
  754. 0xF2DDFDA2,
  755. 0xF474EF38,
  756. 0x8789BDC2,
  757. 0x5366F9C3,
  758. 0xC8B38E74,
  759. 0xB475F255,
  760. 0x46FCD9B9,
  761. 0x7AEB2661,
  762. 0x8B1DDF84,
  763. 0x846A0E79,
  764. 0x915F95E2,
  765. 0x466E598E,
  766. 0x20B45770,
  767. 0x8CD55591,
  768. 0xC902DE4C,
  769. 0xB90BACE1,
  770. 0xBB8205D0,
  771. 0x11A86248,
  772. 0x7574A99E,
  773. 0xB77F19B6,
  774. 0xE0A9DC09,
  775. 0x662D09A1,
  776. 0xC4324633,
  777. 0xE85A1F02,
  778. 0x09F0BE8C,
  779. 0x4A99A025,
  780. 0x1D6EFE10,
  781. 0x1AB93D1D,
  782. 0x0BA5A4DF,
  783. 0xA186F20F,
  784. 0x2868F169,
  785. 0xDCB7DA83,
  786. 0x573906FE,
  787. 0xA1E2CE9B,
  788. 0x4FCD7F52,
  789. 0x50115E01,
  790. 0xA70683FA,
  791. 0xA002B5C4,
  792. 0x0DE6D027,
  793. 0x9AF88C27,
  794. 0x773F8641,
  795. 0xC3604C06,
  796. 0x61A806B5,
  797. 0xF0177A28,
  798. 0xC0F586E0,
  799. 0x006058AA,
  800. 0x30DC7D62,
  801. 0x11E69ED7,
  802. 0x2338EA63,
  803. 0x53C2DD94,
  804. 0xC2C21634,
  805. 0xBBCBEE56,
  806. 0x90BCB6DE,
  807. 0xEBFC7DA1,
  808. 0xCE591D76,
  809. 0x6F05E409,
  810. 0x4B7C0188,
  811. 0x39720A3D,
  812. 0x7C927C24,
  813. 0x86E3725F,
  814. 0x724D9DB9,
  815. 0x1AC15BB4,
  816. 0xD39EB8FC,
  817. 0xED545578,
  818. 0x08FCA5B5,
  819. 0xD83D7CD3,
  820. 0x4DAD0FC4,
  821. 0x1E50EF5E,
  822. 0xB161E6F8,
  823. 0xA28514D9,
  824. 0x6C51133C,
  825. 0x6FD5C7E7,
  826. 0x56E14EC4,
  827. 0x362ABFCE,
  828. 0xDDC6C837,
  829. 0xD79A3234,
  830. 0x92638212,
  831. 0x670EFA8E,
  832. 0x406000E0
  833. };
  834. private static final int[] KS3 =
  835. {
  836. 0x3A39CE37,
  837. 0xD3FAF5CF,
  838. 0xABC27737,
  839. 0x5AC52D1B,
  840. 0x5CB0679E,
  841. 0x4FA33742,
  842. 0xD3822740,
  843. 0x99BC9BBE,
  844. 0xD5118E9D,
  845. 0xBF0F7315,
  846. 0xD62D1C7E,
  847. 0xC700C47B,
  848. 0xB78C1B6B,
  849. 0x21A19045,
  850. 0xB26EB1BE,
  851. 0x6A366EB4,
  852. 0x5748AB2F,
  853. 0xBC946E79,
  854. 0xC6A376D2,
  855. 0x6549C2C8,
  856. 0x530FF8EE,
  857. 0x468DDE7D,
  858. 0xD5730A1D,
  859. 0x4CD04DC6,
  860. 0x2939BBDB,
  861. 0xA9BA4650,
  862. 0xAC9526E8,
  863. 0xBE5EE304,
  864. 0xA1FAD5F0,
  865. 0x6A2D519A,
  866. 0x63EF8CE2,
  867. 0x9A86EE22,
  868. 0xC089C2B8,
  869. 0x43242EF6,
  870. 0xA51E03AA,
  871. 0x9CF2D0A4,
  872. 0x83C061BA,
  873. 0x9BE96A4D,
  874. 0x8FE51550,
  875. 0xBA645BD6,
  876. 0x2826A2F9,
  877. 0xA73A3AE1,
  878. 0x4BA99586,
  879. 0xEF5562E9,
  880. 0xC72FEFD3,
  881. 0xF752F7DA,
  882. 0x3F046F69,
  883. 0x77FA0A59,
  884. 0x80E4A915,
  885. 0x87B08601,
  886. 0x9B09E6AD,
  887. 0x3B3EE593,
  888. 0xE990FD5A,
  889. 0x9E34D797,
  890. 0x2CF0B7D9,
  891. 0x022B8B51,
  892. 0x96D5AC3A,
  893. 0x017DA67D,
  894. 0xD1CF3ED6,
  895. 0x7C7D2D28,
  896. 0x1F9F25CF,
  897. 0xADF2B89B,
  898. 0x5AD6B472,
  899. 0x5A88F54C,
  900. 0xE029AC71,
  901. 0xE019A5E6,
  902. 0x47B0ACFD,
  903. 0xED93FA9B,
  904. 0xE8D3C48D,
  905. 0x283B57CC,
  906. 0xF8D56629,
  907. 0x79132E28,
  908. 0x785F0191,
  909. 0xED756055,
  910. 0xF7960E44,
  911. 0xE3D35E8C,
  912. 0x15056DD4,
  913. 0x88F46DBA,
  914. 0x03A16125,
  915. 0x0564F0BD,
  916. 0xC3EB9E15,
  917. 0x3C9057A2,
  918. 0x97271AEC,
  919. 0xA93A072A,
  920. 0x1B3F6D9B,
  921. 0x1E6321F5,
  922. 0xF59C66FB,
  923. 0x26DCF319,
  924. 0x7533D928,
  925. 0xB155FDF5,
  926. 0x03563482,
  927. 0x8ABA3CBB,
  928. 0x28517711,
  929. 0xC20AD9F8,
  930. 0xABCC5167,
  931. 0xCCAD925F,
  932. 0x4DE81751,
  933. 0x3830DC8E,
  934. 0x379D5862,
  935. 0x9320F991,
  936. 0xEA7A90C2,
  937. 0xFB3E7BCE,
  938. 0x5121CE64,
  939. 0x774FBE32,
  940. 0xA8B6E37E,
  941. 0xC3293D46,
  942. 0x48DE5369,
  943. 0x6413E680,
  944. 0xA2AE0810,
  945. 0xDD6DB224,
  946. 0x69852DFD,
  947. 0x09072166,
  948. 0xB39A460A,
  949. 0x6445C0DD,
  950. 0x586CDECF,
  951. 0x1C20C8AE,
  952. 0x5BBEF7DD,
  953. 0x1B588D40,
  954. 0xCCD2017F,
  955. 0x6BB4E3BB,
  956. 0xDDA26A7E,
  957. 0x3A59FF45,
  958. 0x3E350A44,
  959. 0xBCB4CDD5,
  960. 0x72EACEA8,
  961. 0xFA6484BB,
  962. 0x8D6612AE,
  963. 0xBF3C6F47,
  964. 0xD29BE463,
  965. 0x542F5D9E,
  966. 0xAEC2771B,
  967. 0xF64E6370,
  968. 0x740E0D8D,
  969. 0xE75B1357,
  970. 0xF8721671,
  971. 0xAF537D5D,
  972. 0x4040CB08,
  973. 0x4EB4E2CC,
  974. 0x34D2466A,
  975. 0x0115AF84,
  976. 0xE1B00428,
  977. 0x95983A1D,
  978. 0x06B89FB4,
  979. 0xCE6EA048,
  980. 0x6F3F3B82,
  981. 0x3520AB82,
  982. 0x011A1D4B,
  983. 0x277227F8,
  984. 0x611560B1,
  985. 0xE7933FDC,
  986. 0xBB3A792B,
  987. 0x344525BD,
  988. 0xA08839E1,
  989. 0x51CE794B,
  990. 0x2F32C9B7,
  991. 0xA01FBAC9,
  992. 0xE01CC87E,
  993. 0xBCC7D1F6,
  994. 0xCF0111C3,
  995. 0xA1E8AAC7,
  996. 0x1A908749,
  997. 0xD44FBD9A,
  998. 0xD0DADECB,
  999. 0xD50ADA38,
  1000. 0x0339C32A,
  1001. 0xC6913667,
  1002. 0x8DF9317C,
  1003. 0xE0B12B4F,
  1004. 0xF79E59B7,
  1005. 0x43F5BB3A,
  1006. 0xF2D519FF,
  1007. 0x27D9459C,
  1008. 0xBF97222C,
  1009. 0x15E6FC2A,
  1010. 0x0F91FC71,
  1011. 0x9B941525,
  1012. 0xFAE59361,
  1013. 0xCEB69CEB,
  1014. 0xC2A86459,
  1015. 0x12BAA8D1,
  1016. 0xB6C1075E,
  1017. 0xE3056A0C,
  1018. 0x10D25065,
  1019. 0xCB03A442,
  1020. 0xE0EC6E0E,
  1021. 0x1698DB3B,
  1022. 0x4C98A0BE,
  1023. 0x3278E964,
  1024. 0x9F1F9532,
  1025. 0xE0D392DF,
  1026. 0xD3A0342B,
  1027. 0x8971F21E,
  1028. 0x1B0A7441,
  1029. 0x4BA3348C,
  1030. 0xC5BE7120,
  1031. 0xC37632D8,
  1032. 0xDF359F8D,
  1033. 0x9B992F2E,
  1034. 0xE60B6F47,
  1035. 0x0FE3F11D,
  1036. 0xE54CDA54,
  1037. 0x1EDAD891,
  1038. 0xCE6279CF,
  1039. 0xCD3E7E6F,
  1040. 0x1618B166,
  1041. 0xFD2C1D05,
  1042. 0x848FD2C5,
  1043. 0xF6FB2299,
  1044. 0xF523F357,
  1045. 0xA6327623,
  1046. 0x93A83531,
  1047. 0x56CCCD02,
  1048. 0xACF08162,
  1049. 0x5A75EBB5,
  1050. 0x6E163697,
  1051. 0x88D273CC,
  1052. 0xDE966292,
  1053. 0x81B949D0,
  1054. 0x4C50901B,
  1055. 0x71C65614,
  1056. 0xE6C6C7BD,
  1057. 0x327A140A,
  1058. 0x45E1D006,
  1059. 0xC3F27B9A,
  1060. 0xC9AA53FD,
  1061. 0x62A80F00,
  1062. 0xBB25BFE2,
  1063. 0x35BDD2F6,
  1064. 0x71126905,
  1065. 0xB2040222,
  1066. 0xB6CBCF7C,
  1067. 0xCD769C2B,
  1068. 0x53113EC0,
  1069. 0x1640E3D3,
  1070. 0x38ABBD60,
  1071. 0x2547ADF0,
  1072. 0xBA38209C,
  1073. 0xF746CE76,
  1074. 0x77AFA1C5,
  1075. 0x20756060,
  1076. 0x85CBFE4E,
  1077. 0x8AE88DD8,
  1078. 0x7AAAF9B0,
  1079. 0x4CF9AA7E,
  1080. 0x1948C25C,
  1081. 0x02FB8A8C,
  1082. 0x01C36AE4,
  1083. 0xD6EBE1F9,
  1084. 0x90D4F869,
  1085. 0xA65CDEA0,
  1086. 0x3F09252D,
  1087. 0xC208E69F,
  1088. 0xB74E6132,
  1089. 0xCE77E25B,
  1090. 0x578FDFE3,
  1091. 0x3AC372E6
  1092. };
  1093. // ====================================
  1094. // Useful constants
  1095. // ====================================
  1096. private static final int ROUNDS = 16;
  1097. private static final int BLOCK_SIZE = 8; // bytes = 64 bits
  1098. private static final int SBOX_SK = 256;
  1099. private static final int P_SZ = ROUNDS + 2;
  1100. private final int[] S0, S1, S2, S3; // the s-boxes
  1101. private final int[] P; // the p-array
  1102. private boolean encrypting = false;
  1103. private byte[] workingKey = null;
  1104. public BlowfishEngine()
  1105. {
  1106. S0 = new int[SBOX_SK];
  1107. S1 = new int[SBOX_SK];
  1108. S2 = new int[SBOX_SK];
  1109. S3 = new int[SBOX_SK];
  1110. P = new int[P_SZ];
  1111. }
  1112. /**
  1113. * Initialize a Blowfish cipher.
  1114. * @param pEncrypting whether or not we are for encryption.
  1115. * @param key the key used to set up the cipher.
  1116. * @exception IllegalArgumentException if the params argument is inappropriate.
  1117. */
  1118. public void init(boolean pEncrypting, byte[] key)
  1119. {
  1120. encrypting = pEncrypting;
  1121. workingKey = key;
  1122. setKey(workingKey);
  1123. return;
  1124. }
  1125. public String getAlgorithmName()
  1126. {
  1127. return "Blowfish";
  1128. }
  1129. public final int processBlock(byte[] in, int inOff, byte[] out, int outOff) throws IOException
  1130. {
  1131. if (workingKey == null)
  1132. {
  1133. throw new IllegalStateException("Blowfish not initialised");
  1134. }
  1135. if ((inOff + BLOCK_SIZE) > in.length)
  1136. {
  1137. throw new IOException("input buffer too short");
  1138. }
  1139. if ((outOff + BLOCK_SIZE) > out.length)
  1140. {
  1141. throw new IOException("output buffer too short");
  1142. }
  1143. if (encrypting)
  1144. {
  1145. encryptBlock(in, inOff, out, outOff);
  1146. }
  1147. else
  1148. {
  1149. decryptBlock(in, inOff, out, outOff);
  1150. }
  1151. return BLOCK_SIZE;
  1152. }
  1153. public void reset()
  1154. {
  1155. }
  1156. public int getBlockSize()
  1157. {
  1158. return BLOCK_SIZE;
  1159. }
  1160. // ==================================
  1161. // Private Implementation
  1162. // ==================================
  1163. private int func(int x)
  1164. {
  1165. return (((S0[(x >>> 24)] + S1[(x >>> 16) & 0xff]) ^ S2[(x >>> 8) & 0xff]) + S3[x & 0xff]);
  1166. }
  1167. /**
  1168. * apply the encryption cycle to each value pair in the table.
  1169. * @param xl
  1170. * @param xr
  1171. * @param table
  1172. */
  1173. private void processTable(int xl, int xr, int[] table)
  1174. {
  1175. int size = table.length;
  1176. for (int s = 0; s < size; s += 2)
  1177. {
  1178. xl ^= P[0];
  1179. for (int i = 1; i < ROUNDS; i += 2)
  1180. {
  1181. xr ^= func(xl) ^ P[i];
  1182. xl ^= func(xr) ^ P[i + 1];
  1183. }
  1184. xr ^= P[ROUNDS + 1];
  1185. table[s] = xr;
  1186. table[s + 1] = xl;
  1187. xr = xl; // end of cycle swap
  1188. xl = table[s];
  1189. }
  1190. }
  1191. private void setKey(byte[] key)
  1192. {
  1193. /*
  1194. * - comments are from _Applied Crypto_, Schneier, p338.<br>
  1195. * Please be careful comparing the two, AC numbers the arrays from 1, the enclosed code from 0.<br>
  1196. * (1) Initialize the S-boxes and the P-array, with a fixed string This string contains the hexadecimal digits of pi (3.141...)
  1197. */
  1198. System.arraycopy(KS0, 0, S0, 0, SBOX_SK);
  1199. System.arraycopy(KS1, 0, S1, 0, SBOX_SK);
  1200. System.arraycopy(KS2, 0, S2, 0, SBOX_SK);
  1201. System.arraycopy(KS3, 0, S3, 0, SBOX_SK);
  1202. System.arraycopy(KP, 0, P, 0, P_SZ);
  1203. /*
  1204. * (2) Now, XOR P[0] with the first 32 bits of the key, XOR P[1] with the second 32-bits of the key, and so on for all bits of the key (up to P[17]).<br>
  1205. * Repeatedly cycle through the key bits until the entire P-array has been XOR-ed with the key bits
  1206. */
  1207. int keyLength = key.length;
  1208. int keyIndex = 0;
  1209. for (int i = 0; i < P_SZ; i++)
  1210. {
  1211. // get the 32 bits of the key, in 4 * 8 bit chunks
  1212. int data = 0x0000000;
  1213. for (int j = 0; j < 4; j++)
  1214. {
  1215. // create a 32 bit block
  1216. data = (data << 8) | (key[keyIndex++] & 0xff);
  1217. // wrap when we get to the end of the key
  1218. if (keyIndex >= keyLength)
  1219. {
  1220. keyIndex = 0;
  1221. }
  1222. }
  1223. // XOR the newly created 32 bit chunk onto the P-array
  1224. P[i] ^= data;
  1225. }
  1226. /*
  1227. * (3) Encrypt the all-zero string with the Blowfish algorithm, using the subkeys described in (1) and (2)<br>
  1228. * (4) Replace P1 and P2 with the output of step (3)<br>
  1229. * (5) Encrypt the output of step(3) using the Blowfish algorithm, with the modified subkeys.<br>
  1230. * (6) Replace P3 and P4 with the output of step (5)<br>
  1231. * (7) Continue the process, replacing all elements of the P-array and then all four S-boxes in order, with the output of the continuously changing Blowfish algorithm
  1232. */
  1233. processTable(0, 0, P);
  1234. processTable(P[P_SZ - 2], P[P_SZ - 1], S0);
  1235. processTable(S0[SBOX_SK - 2], S0[SBOX_SK - 1], S1);
  1236. processTable(S1[SBOX_SK - 2], S1[SBOX_SK - 1], S2);
  1237. processTable(S2[SBOX_SK - 2], S2[SBOX_SK - 1], S3);
  1238. }
  1239. /**
  1240. * Encrypt the given input starting at the given offset and place the result in the provided buffer starting at the given offset. The input will be an exact multiple of our blocksize.
  1241. * @param src
  1242. * @param srcIndex
  1243. * @param dst
  1244. * @param dstIndex
  1245. */
  1246. private void encryptBlock(byte[] src, int srcIndex, byte[] dst, int dstIndex)
  1247. {
  1248. int xl = bytesTo32bits(src, srcIndex);
  1249. int xr = bytesTo32bits(src, srcIndex + 4);
  1250. xl ^= P[0];
  1251. for (int i = 1; i < ROUNDS; i += 2)
  1252. {
  1253. xr ^= func(xl) ^ P[i];
  1254. xl ^= func(xr) ^ P[i + 1];
  1255. }
  1256. xr ^= P[ROUNDS + 1];
  1257. bits32ToBytes(xr, dst, dstIndex);
  1258. bits32ToBytes(xl, dst, dstIndex + 4);
  1259. }
  1260. /**
  1261. * Decrypt the given input starting at the given offset and place the result in the provided buffer starting at the given offset. The input will be an exact multiple of our blocksize.
  1262. * @param src
  1263. * @param srcIndex
  1264. * @param dst
  1265. * @param dstIndex
  1266. */
  1267. private void decryptBlock(byte[] src, int srcIndex, byte[] dst, int dstIndex)
  1268. {
  1269. int xl = bytesTo32bits(src, srcIndex);
  1270. int xr = bytesTo32bits(src, srcIndex + 4);
  1271. xl ^= P[ROUNDS + 1];
  1272. for (int i = ROUNDS; i > 0; i -= 2)
  1273. {
  1274. xr ^= func(xl) ^ P[i];
  1275. xl ^= func(xr) ^ P[i - 1];
  1276. }
  1277. xr ^= P[0];
  1278. bits32ToBytes(xr, dst, dstIndex);
  1279. bits32ToBytes(xl, dst, dstIndex + 4);
  1280. }
  1281. private int bytesTo32bits(byte[] b, int i)
  1282. {
  1283. return ((b[i + 3] & 0xff) << 24) | ((b[i + 2] & 0xff) << 16) | ((b[i + 1] & 0xff) << 8) | ((b[i] & 0xff));
  1284. }
  1285. private void bits32ToBytes(int in, byte[] b, int offset)
  1286. {
  1287. b[offset] = (byte) in;
  1288. b[offset + 1] = (byte) (in >> 8);
  1289. b[offset + 2] = (byte) (in >> 16);
  1290. b[offset + 3] = (byte) (in >> 24);
  1291. }
  1292. }