BlowfishEngine.java 29 KB

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