Guest User

Untitled

a guest
Dec 7th, 2015
170
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 43.41 KB | None | 0 0
  1. <?php
  2. /*
  3. *
  4. *  Sky Fortress V1.0 by Cher Ami
  5. *
  6. *  Credits:
  7. *    Me, for writing the code (obviously)
  8. *    The good people who developed PHP
  9. *    The phpSecLib dev team, for their AES and Rjindael libraries ( http://phpseclib.sourceforge.net/ )
  10. *
  11. *  Legal:
  12. *    I don't care what you do with this file. If you have the source, all of the
  13. *    work I've put in this may as well be yours. You can take my name off of it,
  14. *    change the name of the software, you can do *ANYTHING* you want with it.
  15. *    'Mi casa es su casa' as the Spanish would say. All I ask is that, if you do
  16. *    use this source elsewhere, that you not use it for malicious purposes and
  17. *    that you not try to view what users upload without their knowledge.
  18. *
  19. *  Live demo:
  20. *    http://shxdhomhggy3bjrn.onion/skyfortress/
  21. *
  22. *  Installation:
  23. *    1.) Drop this file wherever you want with whatever name you want.
  24. *    2.) Chmod this file to 777.
  25. *    3.) Create a folder named "crypt" in the same directory.
  26. *    4.) Chmod that folder to 777.
  27. *    5.) Spread the link around. :)
  28. *
  29. *  Side note: The AES/Rjindael libraries begin at line 266. Everything after line
  30. *  266 was written by the phpseclib devs.
  31. */
  32.  
  33. $fileself = $_SERVER['PHP_SELF'];
  34.  
  35. function table1($str) {
  36.     echo "
  37.     <center>
  38.     <table border=10 cellpadding=5>
  39.     <tr height=50%>
  40.         <td colspan=2><h1><center>Welcome to the Sky Fortress<br /><small>$str</small></center></h1></td>
  41.     </tr>    
  42.     <tr height=50%>
  43.         <td width=100%>
  44.     ";
  45. }
  46.  
  47. function table2() {
  48.     echo "</td>
  49.     </tr>
  50.     </table>
  51.     </center>";
  52. }
  53. ?>
  54.  
  55. <html>
  56. <head>
  57. <title>Sky Fortress</title>
  58. <style>
  59. body {
  60.     background: #222;
  61. }
  62.  
  63. table {
  64.     width: 80%;
  65.     height: 100%;
  66.     background: #999;
  67. }
  68. </style>
  69. </head>
  70. <body>
  71.  
  72. <?php
  73. if(isset($_FILES['file'])) {
  74.     table1("File upload pt. 2");
  75.     $deletionkey = md5(rand(0,0x7FFFFF));
  76.     $rand = hash("crc32", $deletionkey) . ".txt";
  77.     if($_FILES['file']['size'] > 10485760) {
  78.         echo "File too large. Uploaded files can be no more than 10MB.";
  79.     } else {
  80.         if(move_uploaded_file($_FILES['file']['tmp_name'], "crypt/$rand")) {
  81.             $contents = file_get_contents("crypt/$rand");
  82.             $crypt = new CRYPT_AES(CRYPT_AES_MODE_ECB);
  83.             $crypt->setKey($_POST['password']);
  84.             $contents = $crypt->encrypt($contents);
  85.  
  86.             $f = fopen("crypt/$rand", "w");
  87.             fwrite($f, $contents);
  88.             fclose($f);
  89.  
  90.             echo "The file " . $_FILES['file']['name'] . " has been uploaded as crypt/$rand. <br />
  91.             To download your file in its encrypted form, <a href='crypt/$rand'>click here</a>.<br />
  92.             To decrypt your file for a usable download, <a href='$fileself?mode=decrypt&file=$rand'>click here</a>.<br />
  93.             To delete your file, <a href='$fileself?mode=delete&file=$rand&key=$deletionkey'>click here</a>.<br />
  94.             ";
  95.         } else {
  96.             echo "There was an error uploading the file, please try again.";
  97.         }
  98.     }
  99.     table2();
  100. }
  101. elseif($_GET['mode'] == "delete") {
  102.     table1("File deletion");
  103.  
  104.     if(hash("crc32", $_GET['key']) == substr($_GET['file'], 0, -4) && file_exists("crypt/" . $_GET['file'])) {
  105.         if(unlink("crypt/" . $_GET['file'])) {
  106.             echo "File deleted successfully.";
  107.         } else {
  108.             echo "Error deleting file.";
  109.         }
  110.     } else {
  111.         echo "Error deleting file. (Invalid deletion key or file name.)";
  112.     }
  113.  
  114.     table2();
  115. } elseif($_GET['mode'] == "encrypt") {
  116.     echo "<center>
  117.     <table border=10 cellpadding=5>
  118.     <tr height=50%>
  119.         <td colspan=2><h1><center>Welcome to the Sky Fortress<br /><small>File upload</small></center></h1></td>
  120.     </tr>    
  121.     <tr height=50%>
  122.         <td width=50%>
  123.             File size limit is 10MB.<br />
  124.             We cannot help you recover a file's password if you lose it.<br />
  125.             There are no requirements on password length.
  126.         </td>
  127.         <td width=50%>
  128.             <form enctype='multipart/form-data' action='$fileself' method='post'>
  129.             <input type='password' name='password' style='width: 50%' placeholder='Password'/><br />
  130.             <input name='file' type='file' /><br />
  131.             <input type='submit' value='Upload File' />
  132.             </form>
  133.         </td>
  134.     </tr>
  135.     </table>
  136.     </center>";
  137. } elseif($_GET['mode'] == "decrypt") {
  138.     table1("File decryption");
  139.  
  140.     if(!isset($_GET['file'])) {
  141.         echo "<center><h2>No file selected</h2>
  142.         <small>(Maybe you should <a href='$fileself?mode=browse'>look for it</a>?)</small>
  143.         <form action='$fileself?mode=decrypt' method='get'><br />
  144.         <input type='text' placeholder='File name' name='file'><br />
  145.         <input type='submit' value='Decrypt'><br />
  146.         </form></center>";
  147.     } else {
  148.         if(file_exists("crypt/" . $_GET['file'])) {
  149.             if(!isset($_POST['password'])) {
  150.                 echo "<center><h2>Decrypting " . htmlentities($_GET['file']) . "</h2>
  151.                 <form action='$fileself?file=".$_GET['file']."&mode=decrypt' method='post'><br />
  152.                 <input type='password' name='password' placeholder='Password'><br />
  153.                 <input type='submit' value='Decrypt'><br />
  154.                 </form></center>";
  155.             } else {
  156.                 $crypt = new CRYPT_AES(CRYPT_AES_MODE_ECB);
  157.                 $crypt->setKey($_POST['password']);
  158.                 $decrypted = $crypt->decrypt(file_get_contents("crypt/" . $_GET['file']));
  159.                 if($decrypted == null || $decrypted == "") {
  160.                     echo "Incorrect password.";
  161.                 } else {
  162.                     $deletionkey = md5(rand(0,0x7FFFFF));
  163.                     $rand = hash("crc32", $deletionkey);
  164.                     $rand = $rand . ".txt";
  165.  
  166.                     $f = fopen("crypt/" . $rand, "w+");
  167.                     fwrite($f, $decrypted);
  168.                     fclose($f);
  169.  
  170.                     echo "File successfully decrypted. Download it <a href='crypt/$rand'>here</a>.<br /><br />
  171.                     <strong><em>VERY IMPORTANT!</em></strong><br />
  172.                     Though your decrypted file may blend in with the other files in the crypt folder, it is decrypted and anybody who stumbles upon it and realizes it isn't encrypted can download it. We will do our best to delete any decrypted files we find but the fact of the matter is we simply won't find all of them because they do blend in.<br />
  173.                     As such, it is your responsibility to use <a href='$fileself?mode=delete&key=$deletionkey&file=$rand'>this link</a> to delete the decrypted file once you've downloaded it.";
  174.                 }
  175.             }
  176.         }
  177.     }
  178.     table2();
  179. } elseif($_GET['mode'] == "browse") {
  180.     echo "
  181.     <center>
  182.     <table border=10 cellpadding=5>
  183.     <tr height=50%>
  184.         <td colspan=2><h1><center>Welcome to the Sky Fortress<br /><small>File browser</small></center></h1></td>
  185.     </tr>    
  186.     <tr height=50%>
  187.         <td width=100%><pre>
  188. ";
  189.  
  190.     foreach(scandir("crypt") as $cryptofile) {
  191.         if($cryptofile != "." && $cryptofile != "..") {
  192.             $size = round(filesize("crypt/" . $cryptofile)/1024, 2, PHP_ROUND_HALF_UP);
  193.             $lastmod = date("F d Y H:i:s.", filemtime($filename));
  194.             echo "<a href='$fileself?mode=decrypt&file=$cryptofile'>$cryptofile</a> - $size KB - Last modified $lastmod\n";
  195.         }
  196.     }
  197.  
  198.     echo "</pre></td>
  199.     </tr>
  200.     </table>
  201.     </center>";
  202. } elseif($_GET['mode'] == "quine") {
  203.     echo "
  204.     <center>
  205.     <table border=10 cellpadding=5 width=80%>
  206.     <tr height=100%>
  207.         <td width=100%><pre>
  208. ";
  209.     echo htmlentities(file_get_contents(substr($_SERVER['PHP_SELF'], 1)));
  210.     //echo $self;
  211.  
  212.     echo "</pre></td>
  213.     </tr>
  214.     </table>
  215.     </center>";
  216. }
  217. else {
  218.     echo "
  219. <center>
  220. <table border=10 cellpadding=5>
  221. <tr height=50%>
  222.     <td colspan=3><h1><center>Welcome to the Sky Fortress<br /><small>Home</small></center></h1></td>
  223. </tr>    
  224. <tr height=50%>
  225.     <td width=33%>
  226.         <center><h2>About</h2></center><br />
  227.         Sky Fortress is an <a href='$fileself?mode=quine'>open source</a> standalone PHP file for encrypted file upload and download. All files are stored encrypted without human-readable names or extensions so that nobody knows what the files are except the people who uploaded them.
  228.     </td>
  229.     <td width=34%>
  230.         <center><h2>Actions</h2></center><br />
  231.         <ul>
  232.             <li><a href='$fileself?mode=encrypt'>Encrypt a file</a></li>
  233.             <li><a href='$fileself?mode=decrypt'>Decrypt a file</a></li>
  234.             <li><a href='$fileself?mode=browse'>Browse files</a></li>
  235.             <li><a href='$fileself?mode=quine'>View Sky Fortress source</a></li>
  236.         </ul>
  237.     </td>
  238.     <td width=33%>
  239.         <center><h2>Misc</h2></center><br />
  240.         <strong>Version</strong>: <em>1.0</em><br />
  241.         <strong>Developer</strong>: <em>Cher Ami</em><br />
  242.         <strong>Contact</strong>: <em>[email protected]</em>
  243.     </td>
  244. </tr>
  245. </table>
  246. </center>";
  247. }
  248.  
  249. ?>
  250.  
  251. </body>
  252. </html>
  253.  
  254. <?php
  255. // And now, the AES and Rjindael libraries from phpseclib. Note that I took out
  256. // all the comments and most of the line breaks and a lot of the formatting; I
  257. // didn't do it to steal credit or anything, I did it to conserve space.
  258.  
  259.  
  260.  
  261.  
  262.  
  263.  
  264.  
  265.  
  266.  
  267.  
  268. define('CRYPT_RIJNDAEL_MODE_CTR', -1);
  269. define('CRYPT_RIJNDAEL_MODE_ECB', 1);
  270. define('CRYPT_RIJNDAEL_MODE_CBC', 2);
  271. define('CRYPT_RIJNDAEL_MODE_CFB', 3);
  272. define('CRYPT_RIJNDAEL_MODE_OFB', 4);
  273. define('CRYPT_RIJNDAEL_MODE_INTERNAL', 1);
  274.  
  275. define('CRYPT_RIJNDAEL_MODE_MCRYPT', 2);
  276.  
  277. class Crypt_Rijndael {
  278. var $mode;
  279.  
  280. var $key = "\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0";
  281.  
  282. var $iv = '';
  283.  
  284. var $encryptIV = '';
  285.  
  286. var $decryptIV = '';
  287.  
  288. var $continuousBuffer = false;
  289.  
  290. var $padding = true;
  291.  
  292. var $changed = true;
  293.  
  294. var $explicit_key_length = false;
  295.  
  296. var $w;
  297.  
  298. var $dw;
  299.  
  300. var $block_size = 16;
  301.  
  302. var $Nb = 4;
  303.  
  304. var $key_size = 16;
  305.  
  306. var $Nk = 4;
  307.  
  308. var $Nr;
  309.  
  310. var $c;
  311.  
  312. var $t0;
  313.  
  314. var $t1;
  315. var $t2;
  316. var $t3;
  317. var $dt0;
  318. var $dt1;
  319. var $dt2;
  320. var $dt3;
  321. var $paddable = false;
  322. var $enbuffer = array('encrypted' => '', 'xor' => '');
  323. var $debuffer = array('ciphertext' => '');
  324. function Crypt_Rijndael($mode = CRYPT_RIJNDAEL_MODE_CBC)
  325. {
  326. switch ($mode) {
  327. case CRYPT_RIJNDAEL_MODE_ECB:
  328. case CRYPT_RIJNDAEL_MODE_CBC:
  329. $this->paddable = true;
  330. $this->mode = $mode;
  331. break;
  332. case CRYPT_RIJNDAEL_MODE_CTR:
  333. case CRYPT_RIJNDAEL_MODE_CFB:
  334. case CRYPT_RIJNDAEL_MODE_OFB:
  335. $this->mode = $mode;
  336. break;
  337. default:
  338. $this->paddable = true;
  339. $this->mode = CRYPT_RIJNDAEL_MODE_CBC;
  340. }
  341.  
  342. $t3 = &$this->t3;
  343. $t2 = &$this->t2;
  344. $t1 = &$this->t1;
  345. $t0 = &$this->t0;
  346.  
  347. $dt3 = &$this->dt3;
  348. $dt2 = &$this->dt2;
  349. $dt1 = &$this->dt1;
  350. $dt0 = &$this->dt0;
  351. $t3 = array(
  352. 0x6363A5C6, 0x7C7C84F8, 0x777799EE, 0x7B7B8DF6, 0xF2F20DFF, 0x6B6BBDD6, 0x6F6FB1DE, 0xC5C55491,
  353. 0x30305060, 0x01010302, 0x6767A9CE, 0x2B2B7D56, 0xFEFE19E7, 0xD7D762B5, 0xABABE64D, 0x76769AEC,
  354. 0xCACA458F, 0x82829D1F, 0xC9C94089, 0x7D7D87FA, 0xFAFA15EF, 0x5959EBB2, 0x4747C98E, 0xF0F00BFB,
  355. 0xADADEC41, 0xD4D467B3, 0xA2A2FD5F, 0xAFAFEA45, 0x9C9CBF23, 0xA4A4F753, 0x727296E4, 0xC0C05B9B,
  356. 0xB7B7C275, 0xFDFD1CE1, 0x9393AE3D, 0x26266A4C, 0x36365A6C, 0x3F3F417E, 0xF7F702F5, 0xCCCC4F83,
  357. 0x34345C68, 0xA5A5F451, 0xE5E534D1, 0xF1F108F9, 0x717193E2, 0xD8D873AB, 0x31315362, 0x15153F2A,
  358. 0x04040C08, 0xC7C75295, 0x23236546, 0xC3C35E9D, 0x18182830, 0x9696A137, 0x05050F0A, 0x9A9AB52F,
  359. 0x0707090E, 0x12123624, 0x80809B1B, 0xE2E23DDF, 0xEBEB26CD, 0x2727694E, 0xB2B2CD7F, 0x75759FEA,
  360. 0x09091B12, 0x83839E1D, 0x2C2C7458, 0x1A1A2E34, 0x1B1B2D36, 0x6E6EB2DC, 0x5A5AEEB4, 0xA0A0FB5B,
  361. 0x5252F6A4, 0x3B3B4D76, 0xD6D661B7, 0xB3B3CE7D, 0x29297B52, 0xE3E33EDD, 0x2F2F715E, 0x84849713,
  362. 0x5353F5A6, 0xD1D168B9, 0x00000000, 0xEDED2CC1, 0x20206040, 0xFCFC1FE3, 0xB1B1C879, 0x5B5BEDB6,
  363. 0x6A6ABED4, 0xCBCB468D, 0xBEBED967, 0x39394B72, 0x4A4ADE94, 0x4C4CD498, 0x5858E8B0, 0xCFCF4A85,
  364. 0xD0D06BBB, 0xEFEF2AC5, 0xAAAAE54F, 0xFBFB16ED, 0x4343C586, 0x4D4DD79A, 0x33335566, 0x85859411,
  365. 0x4545CF8A, 0xF9F910E9, 0x02020604, 0x7F7F81FE, 0x5050F0A0, 0x3C3C4478, 0x9F9FBA25, 0xA8A8E34B,
  366. 0x5151F3A2, 0xA3A3FE5D, 0x4040C080, 0x8F8F8A05, 0x9292AD3F, 0x9D9DBC21, 0x38384870, 0xF5F504F1,
  367. 0xBCBCDF63, 0xB6B6C177, 0xDADA75AF, 0x21216342, 0x10103020, 0xFFFF1AE5, 0xF3F30EFD, 0xD2D26DBF,
  368. 0xCDCD4C81, 0x0C0C1418, 0x13133526, 0xECEC2FC3, 0x5F5FE1BE, 0x9797A235, 0x4444CC88, 0x1717392E,
  369. 0xC4C45793, 0xA7A7F255, 0x7E7E82FC, 0x3D3D477A, 0x6464ACC8, 0x5D5DE7BA, 0x19192B32, 0x737395E6,
  370. 0x6060A0C0, 0x81819819, 0x4F4FD19E, 0xDCDC7FA3, 0x22226644, 0x2A2A7E54, 0x9090AB3B, 0x8888830B,
  371. 0x4646CA8C, 0xEEEE29C7, 0xB8B8D36B, 0x14143C28, 0xDEDE79A7, 0x5E5EE2BC, 0x0B0B1D16, 0xDBDB76AD,
  372. 0xE0E03BDB, 0x32325664, 0x3A3A4E74, 0x0A0A1E14, 0x4949DB92, 0x06060A0C, 0x24246C48, 0x5C5CE4B8,
  373. 0xC2C25D9F, 0xD3D36EBD, 0xACACEF43, 0x6262A6C4, 0x9191A839, 0x9595A431, 0xE4E437D3, 0x79798BF2,
  374. 0xE7E732D5, 0xC8C8438B, 0x3737596E, 0x6D6DB7DA, 0x8D8D8C01, 0xD5D564B1, 0x4E4ED29C, 0xA9A9E049,
  375. 0x6C6CB4D8, 0x5656FAAC, 0xF4F407F3, 0xEAEA25CF, 0x6565AFCA, 0x7A7A8EF4, 0xAEAEE947, 0x08081810,
  376. 0xBABAD56F, 0x787888F0, 0x25256F4A, 0x2E2E725C, 0x1C1C2438, 0xA6A6F157, 0xB4B4C773, 0xC6C65197,
  377. 0xE8E823CB, 0xDDDD7CA1, 0x74749CE8, 0x1F1F213E, 0x4B4BDD96, 0xBDBDDC61, 0x8B8B860D, 0x8A8A850F,
  378. 0x707090E0, 0x3E3E427C, 0xB5B5C471, 0x6666AACC, 0x4848D890, 0x03030506, 0xF6F601F7, 0x0E0E121C,
  379. 0x6161A3C2, 0x35355F6A, 0x5757F9AE, 0xB9B9D069, 0x86869117, 0xC1C15899, 0x1D1D273A, 0x9E9EB927,
  380. 0xE1E138D9, 0xF8F813EB, 0x9898B32B, 0x11113322, 0x6969BBD2, 0xD9D970A9, 0x8E8E8907, 0x9494A733,
  381. 0x9B9BB62D, 0x1E1E223C, 0x87879215, 0xE9E920C9, 0xCECE4987, 0x5555FFAA, 0x28287850, 0xDFDF7AA5,
  382. 0x8C8C8F03, 0xA1A1F859, 0x89898009, 0x0D0D171A, 0xBFBFDA65, 0xE6E631D7, 0x4242C684, 0x6868B8D0,
  383. 0x4141C382, 0x9999B029, 0x2D2D775A, 0x0F0F111E, 0xB0B0CB7B, 0x5454FCA8, 0xBBBBD66D, 0x16163A2C
  384. );
  385.  
  386. $dt3 = array(
  387. 0xF4A75051, 0x4165537E, 0x17A4C31A, 0x275E963A, 0xAB6BCB3B, 0x9D45F11F, 0xFA58ABAC, 0xE303934B,
  388. 0x30FA5520, 0x766DF6AD, 0xCC769188, 0x024C25F5, 0xE5D7FC4F, 0x2ACBD7C5, 0x35448026, 0x62A38FB5,
  389. 0xB15A49DE, 0xBA1B6725, 0xEA0E9845, 0xFEC0E15D, 0x2F7502C3, 0x4CF01281, 0x4697A38D, 0xD3F9C66B,
  390. 0x8F5FE703, 0x929C9515, 0x6D7AEBBF, 0x5259DA95, 0xBE832DD4, 0x7421D358, 0xE0692949, 0xC9C8448E,
  391. 0xC2896A75, 0x8E7978F4, 0x583E6B99, 0xB971DD27, 0xE14FB6BE, 0x88AD17F0, 0x20AC66C9, 0xCE3AB47D,
  392. 0xDF4A1863, 0x1A3182E5, 0x51336097, 0x537F4562, 0x6477E0B1, 0x6BAE84BB, 0x81A01CFE, 0x082B94F9,
  393. 0x48685870, 0x45FD198F, 0xDE6C8794, 0x7BF8B752, 0x73D323AB, 0x4B02E272, 0x1F8F57E3, 0x55AB2A66,
  394. 0xEB2807B2, 0xB5C2032F, 0xC57B9A86, 0x3708A5D3, 0x2887F230, 0xBFA5B223, 0x036ABA02, 0x16825CED,
  395. 0xCF1C2B8A, 0x79B492A7, 0x07F2F0F3, 0x69E2A14E, 0xDAF4CD65, 0x05BED506, 0x34621FD1, 0xA6FE8AC4,
  396. 0x2E539D34, 0xF355A0A2, 0x8AE13205, 0xF6EB75A4, 0x83EC390B, 0x60EFAA40, 0x719F065E, 0x6E1051BD,
  397. 0x218AF93E, 0xDD063D96, 0x3E05AEDD, 0xE6BD464D, 0x548DB591, 0xC45D0571, 0x06D46F04, 0x5015FF60,
  398. 0x98FB2419, 0xBDE997D6, 0x4043CC89, 0xD99E7767, 0xE842BDB0, 0x898B8807, 0x195B38E7, 0xC8EEDB79,
  399. 0x7C0A47A1, 0x420FE97C, 0x841EC9F8, 0x00000000, 0x80868309, 0x2BED4832, 0x1170AC1E, 0x5A724E6C,
  400. 0x0EFFFBFD, 0x8538560F, 0xAED51E3D, 0x2D392736, 0x0FD9640A, 0x5CA62168, 0x5B54D19B, 0x362E3A24,
  401. 0x0A67B10C, 0x57E70F93, 0xEE96D2B4, 0x9B919E1B, 0xC0C54F80, 0xDC20A261, 0x774B695A, 0x121A161C,
  402. 0x93BA0AE2, 0xA02AE5C0, 0x22E0433C, 0x1B171D12, 0x090D0B0E, 0x8BC7ADF2, 0xB6A8B92D, 0x1EA9C814,
  403. 0xF1198557, 0x75074CAF, 0x99DDBBEE, 0x7F60FDA3, 0x01269FF7, 0x72F5BC5C, 0x663BC544, 0xFB7E345B,
  404. 0x4329768B, 0x23C6DCCB, 0xEDFC68B6, 0xE4F163B8, 0x31DCCAD7, 0x63851042, 0x97224013, 0xC6112084,
  405. 0x4A247D85, 0xBB3DF8D2, 0xF93211AE, 0x29A16DC7, 0x9E2F4B1D, 0xB230F3DC, 0x8652EC0D, 0xC1E3D077,
  406. 0xB3166C2B, 0x70B999A9, 0x9448FA11, 0xE9642247, 0xFC8CC4A8, 0xF03F1AA0, 0x7D2CD856, 0x3390EF22,
  407. 0x494EC787, 0x38D1C1D9, 0xCAA2FE8C, 0xD40B3698, 0xF581CFA6, 0x7ADE28A5, 0xB78E26DA, 0xADBFA43F,
  408. 0x3A9DE42C, 0x78920D50, 0x5FCC9B6A, 0x7E466254, 0x8D13C2F6, 0xD8B8E890, 0x39F75E2E, 0xC3AFF582,
  409. 0x5D80BE9F, 0xD0937C69, 0xD52DA96F, 0x2512B3CF, 0xAC993BC8, 0x187DA710, 0x9C636EE8, 0x3BBB7BDB,
  410. 0x267809CD, 0x5918F46E, 0x9AB701EC, 0x4F9AA883, 0x956E65E6, 0xFFE67EAA, 0xBCCF0821, 0x15E8E6EF,
  411. 0xE79BD9BA, 0x6F36CE4A, 0x9F09D4EA, 0xB07CD629, 0xA4B2AF31, 0x3F23312A, 0xA59430C6, 0xA266C035,
  412. 0x4EBC3774, 0x82CAA6FC, 0x90D0B0E0, 0xA7D81533, 0x04984AF1, 0xECDAF741, 0xCD500E7F, 0x91F62F17,
  413. 0x4DD68D76, 0xEFB04D43, 0xAA4D54CC, 0x9604DFE4, 0xD1B5E39E, 0x6A881B4C, 0x2C1FB8C1, 0x65517F46,
  414. 0x5EEA049D, 0x8C355D01, 0x877473FA, 0x0B412EFB, 0x671D5AB3, 0xDBD25292, 0x105633E9, 0xD647136D,
  415. 0xD7618C9A, 0xA10C7A37, 0xF8148E59, 0x133C89EB, 0xA927EECE, 0x61C935B7, 0x1CE5EDE1, 0x47B13C7A,
  416. 0xD2DF599C, 0xF2733F55, 0x14CE7918, 0xC737BF73, 0xF7CDEA53, 0xFDAA5B5F, 0x3D6F14DF, 0x44DB8678,
  417. 0xAFF381CA, 0x68C43EB9, 0x24342C38, 0xA3405FC2, 0x1DC37216, 0xE2250CBC, 0x3C498B28, 0x0D9541FF,
  418. 0xA8017139, 0x0CB3DE08, 0xB4E49CD8, 0x56C19064, 0xCB84617B, 0x32B670D5, 0x6C5C7448, 0xB85742D0
  419. );
  420.  
  421. for ($i = 0; $i < 256; $i++) {
  422. $t2[$i <<  8] = (($t3[$i] <<  8) & 0xFFFFFF00) | (($t3[$i] >> 24) & 0x000000FF);
  423. $t1[$i << 16] = (($t3[$i] << 16) & 0xFFFF0000) | (($t3[$i] >> 16) & 0x0000FFFF);
  424. $t0[$i << 24] = (($t3[$i] << 24) & 0xFF000000) | (($t3[$i] >>  8) & 0x00FFFFFF);
  425.  
  426. $dt2[$i <<  8] = (($this->dt3[$i] <<  8) & 0xFFFFFF00) | (($dt3[$i] >> 24) & 0x000000FF);
  427. $dt1[$i << 16] = (($this->dt3[$i] << 16) & 0xFFFF0000) | (($dt3[$i] >> 16) & 0x0000FFFF);
  428. $dt0[$i << 24] = (($this->dt3[$i] << 24) & 0xFF000000) | (($dt3[$i] >>  8) & 0x00FFFFFF);
  429. }
  430. }
  431. function setKey($key)
  432. {
  433. $this->key = $key;
  434. $this->changed = true;
  435. }
  436. function setIV($iv)
  437. {
  438. $this->encryptIV = $this->decryptIV = $this->iv = str_pad(substr($iv, 0, $this->block_size), $this->block_size, chr(0));
  439. }
  440. function setKeyLength($length)
  441. {
  442. $length >>= 5;
  443. if ($length > 8) {
  444. $length = 8;
  445. } else if ($length < 4) {
  446. $length = 4;
  447. }
  448. $this->Nk = $length;
  449. $this->key_size = $length << 2;
  450.  
  451. $this->explicit_key_length = true;
  452. $this->changed = true;
  453. }
  454. function setPassword($password, $method = 'pbkdf2')
  455. {
  456. $key = '';
  457.  
  458. switch ($method) {
  459. default:
  460. list(, , $hash, $salt, $count) = func_get_args();
  461. if (!isset($hash)) {
  462. $hash = 'sha1';
  463. }
  464. if (!isset($salt)) {
  465. $salt = 'phpseclib/salt';
  466. }
  467. if (!isset($count)) {
  468. $count = 1000;
  469. }
  470.  
  471. if (!class_exists('Crypt_Hash')) {
  472. require_once('Crypt/Hash.php');
  473. }
  474.  
  475. $i = 1;
  476. while (strlen($key) < $this->key_size) {
  477. $hmac = new Crypt_Hash();
  478. $hmac->setHash($hash);
  479. $hmac->setKey($password);
  480. $f = $u = $hmac->hash($salt . pack('N', $i++));
  481. for ($j = 2; $j <= $count; $j++) {
  482. $u = $hmac->hash($u);
  483. $f^= $u;
  484. }
  485. $key.= $f;
  486. }
  487. }
  488.  
  489. $this->setKey(substr($key, 0, $this->key_size));
  490. }
  491. function setBlockLength($length)
  492. {
  493. $length >>= 5;
  494. if ($length > 8) {
  495. $length = 8;
  496. } else if ($length < 4) {
  497. $length = 4;
  498. }
  499. $this->Nb = $length;
  500. $this->block_size = $length << 2;
  501. $this->changed = true;
  502. }
  503. function _generate_xor($length, &$iv)
  504. {
  505. $xor = '';
  506. $block_size = $this->block_size;
  507. $num_blocks = floor(($length + ($block_size - 1)) / $block_size);
  508. for ($i = 0; $i < $num_blocks; $i++) {
  509. $xor.= $iv;
  510. for ($j = 4; $j <= $block_size; $j+=4) {
  511. $temp = substr($iv, -$j, 4);
  512. switch ($temp) {
  513. case "\xFF\xFF\xFF\xFF":
  514. $iv = substr_replace($iv, "\x00\x00\x00\x00", -$j, 4);
  515. break;
  516. case "\x7F\xFF\xFF\xFF":
  517. $iv = substr_replace($iv, "\x80\x00\x00\x00", -$j, 4);
  518. break 2;
  519. default:
  520. extract(unpack('Ncount', $temp));
  521. $iv = substr_replace($iv, pack('N', $count + 1), -$j, 4);
  522. break 2;
  523. }
  524. }
  525. }
  526.  
  527. return $xor;
  528. }
  529. function encrypt($plaintext)
  530. {
  531. $this->_setup();
  532. if ($this->paddable) {
  533. $plaintext = $this->_pad($plaintext);
  534. }
  535.  
  536. $block_size = $this->block_size;
  537. $buffer = &$this->enbuffer;
  538. $continuousBuffer = $this->continuousBuffer;
  539. $ciphertext = '';
  540. switch ($this->mode) {
  541. case CRYPT_RIJNDAEL_MODE_ECB:
  542. for ($i = 0; $i < strlen($plaintext); $i+=$block_size) {
  543. $ciphertext.= $this->_encryptBlock(substr($plaintext, $i, $block_size));
  544. }
  545. break;
  546. case CRYPT_RIJNDAEL_MODE_CBC:
  547. $xor = $this->encryptIV;
  548. for ($i = 0; $i < strlen($plaintext); $i+=$block_size) {
  549. $block = substr($plaintext, $i, $block_size);
  550. $block = $this->_encryptBlock($block ^ $xor);
  551. $xor = $block;
  552. $ciphertext.= $block;
  553. }
  554. if ($this->continuousBuffer) {
  555. $this->encryptIV = $xor;
  556. }
  557. break;
  558. case CRYPT_RIJNDAEL_MODE_CTR:
  559. $xor = $this->encryptIV;
  560. if (!empty($buffer['encrypted'])) {
  561. for ($i = 0; $i < strlen($plaintext); $i+=$block_size) {
  562. $block = substr($plaintext, $i, $block_size);
  563. $buffer['encrypted'].= $this->_encryptBlock($this->_generate_xor($block_size, $xor));
  564. $key = $this->_string_shift($buffer['encrypted'], $block_size);
  565. $ciphertext.= $block ^ $key;
  566. }
  567. } else {
  568. for ($i = 0; $i < strlen($plaintext); $i+=$block_size) {
  569. $block = substr($plaintext, $i, $block_size);
  570. $key = $this->_encryptBlock($this->_generate_xor($block_size, $xor));
  571. $ciphertext.= $block ^ $key;
  572. }
  573. }
  574. if ($this->continuousBuffer) {
  575. $this->encryptIV = $xor;
  576. if ($start = strlen($plaintext) % $block_size) {
  577. $buffer['encrypted'] = substr($key, $start) . $buffer['encrypted'];
  578. }
  579. }
  580. break;
  581. case CRYPT_RIJNDAEL_MODE_CFB:
  582. if (!empty($buffer['xor'])) {
  583. $ciphertext = $plaintext ^ $buffer['xor'];
  584. $iv = $buffer['encrypted'] . $ciphertext;
  585. $start = strlen($ciphertext);
  586. $buffer['encrypted'].= $ciphertext;
  587. $buffer['xor'] = substr($buffer['xor'], strlen($ciphertext));
  588. } else {
  589. $ciphertext = '';
  590. $iv = $this->encryptIV;
  591. $start = 0;
  592. }
  593.  
  594. for ($i = $start; $i < strlen($plaintext); $i+=$block_size) {
  595. $block = substr($plaintext, $i, $block_size);
  596. $xor = $this->_encryptBlock($iv);
  597. $iv = $block ^ $xor;
  598. if ($continuousBuffer && strlen($iv) != $block_size) {
  599. $buffer = array(
  600. 'encrypted' => $iv,
  601. 'xor' => substr($xor, strlen($iv))
  602. );
  603. }
  604. $ciphertext.= $iv;
  605. }
  606.  
  607. if ($this->continuousBuffer) {
  608. $this->encryptIV = $iv;
  609. }
  610. break;
  611. case CRYPT_RIJNDAEL_MODE_OFB:
  612. $xor = $this->encryptIV;
  613. if (strlen($buffer)) {
  614. for ($i = 0; $i < strlen($plaintext); $i+=$block_size) {
  615. $xor = $this->_encryptBlock($xor);
  616. $buffer.= $xor;
  617. $key = $this->_string_shift($buffer, $block_size);
  618. $ciphertext.= substr($plaintext, $i, $block_size) ^ $key;
  619. }
  620. } else {
  621. for ($i = 0; $i < strlen($plaintext); $i+=$block_size) {
  622. $xor = $this->_encryptBlock($xor);
  623. $ciphertext.= substr($plaintext, $i, $block_size) ^ $xor;
  624. }
  625. $key = $xor;
  626. }
  627. if ($this->continuousBuffer) {
  628. $this->encryptIV = $xor;
  629. if ($start = strlen($plaintext) % $block_size) {
  630.  $buffer = substr($key, $start) . $buffer;
  631. }
  632. }
  633. }
  634.  
  635. return $ciphertext;
  636. }
  637. function decrypt($ciphertext)
  638. {
  639. $this->_setup();
  640.  
  641. if ($this->paddable) {
  642. // we pad with chr(0) since that's what mcrypt_generic does.  to quote from http://php.net/function.mcrypt-generic :
  643. // "The data is padded with "\0" to make sure the length of the data is n * blocksize."
  644. $ciphertext = str_pad($ciphertext, strlen($ciphertext) + ($this->block_size - strlen($ciphertext) % $this->block_size) % $this->block_size, chr(0));
  645. }
  646.  
  647. $block_size = $this->block_size;
  648. $buffer = &$this->debuffer;
  649. $continuousBuffer = $this->continuousBuffer;
  650. $plaintext = '';
  651. switch ($this->mode) {
  652. case CRYPT_RIJNDAEL_MODE_ECB:
  653. for ($i = 0; $i < strlen($ciphertext); $i+=$block_size) {
  654. $plaintext.= $this->_decryptBlock(substr($ciphertext, $i, $block_size));
  655. }
  656. break;
  657. case CRYPT_RIJNDAEL_MODE_CBC:
  658. $xor = $this->decryptIV;
  659. for ($i = 0; $i < strlen($ciphertext); $i+=$block_size) {
  660. $block = substr($ciphertext, $i, $block_size);
  661. $plaintext.= $this->_decryptBlock($block) ^ $xor;
  662. $xor = $block;
  663. }
  664. if ($this->continuousBuffer) {
  665. $this->decryptIV = $xor;
  666. }
  667. break;
  668. case CRYPT_RIJNDAEL_MODE_CTR:
  669. $xor = $this->decryptIV;
  670. if (!empty($buffer['ciphertext'])) {
  671. for ($i = 0; $i < strlen($ciphertext); $i+=$block_size) {
  672. $block = substr($ciphertext, $i, $block_size);
  673. $buffer['ciphertext'].= $this->_encryptBlock($this->_generate_xor($block_size, $xor));
  674. $key = $this->_string_shift($buffer['ciphertext'], $block_size);
  675. $plaintext.= $block ^ $key;
  676. }
  677. } else {
  678. for ($i = 0; $i < strlen($ciphertext); $i+=$block_size) {
  679. $block = substr($ciphertext, $i, $block_size);
  680. $key = $this->_encryptBlock($this->_generate_xor($block_size, $xor));
  681. $plaintext.= $block ^ $key;
  682. }
  683. }
  684. if ($this->continuousBuffer) {
  685. $this->decryptIV = $xor;
  686. if ($start = strlen($ciphertext) % $block_size) {
  687. $buffer['ciphertext'] = substr($key, $start) . $buffer['encrypted'];
  688. }
  689. }
  690. break;
  691. case CRYPT_RIJNDAEL_MODE_CFB:
  692. if (!empty($buffer['ciphertext'])) {
  693. $plaintext = $ciphertext ^ substr($this->decryptIV, strlen($buffer['ciphertext']));
  694. $buffer['ciphertext'].= substr($ciphertext, 0, strlen($plaintext));
  695. if (strlen($buffer['ciphertext']) == $block_size) {
  696. $xor = $this->_encryptBlock($buffer['ciphertext']);
  697. $buffer['ciphertext'] = '';
  698. }
  699. $start = strlen($plaintext);
  700. $block = $this->decryptIV;
  701. } else {
  702. $plaintext = '';
  703. $xor = $this->_encryptBlock($this->decryptIV);
  704. $start = 0;
  705. }
  706.  
  707. for ($i = $start; $i < strlen($ciphertext); $i+=$block_size) {
  708. $block = substr($ciphertext, $i, $block_size);
  709. $plaintext.= $block ^ $xor;
  710. if ($continuousBuffer && strlen($block) != $block_size) {
  711. $buffer['ciphertext'].= $block;
  712. $block = $xor;
  713. } else if (strlen($block) == $block_size) {
  714. $xor = $this->_encryptBlock($block);
  715. }
  716. }
  717. if ($this->continuousBuffer) {
  718. $this->decryptIV = $block;
  719. }
  720. break;
  721. case CRYPT_RIJNDAEL_MODE_OFB:
  722. $xor = $this->decryptIV;
  723. if (strlen($buffer)) {
  724. for ($i = 0; $i < strlen($ciphertext); $i+=$block_size) {
  725. $xor = $this->_encryptBlock($xor);
  726. $buffer.= $xor;
  727. $key = $this->_string_shift($buffer, $block_size);
  728. $plaintext.= substr($ciphertext, $i, $block_size) ^ $key;
  729. }
  730. } else {
  731. for ($i = 0; $i < strlen($ciphertext); $i+=$block_size) {
  732. $xor = $this->_encryptBlock($xor);
  733. $plaintext.= substr($ciphertext, $i, $block_size) ^ $xor;
  734. }
  735. $key = $xor;
  736. }
  737. if ($this->continuousBuffer) {
  738. $this->decryptIV = $xor;
  739. if ($start = strlen($ciphertext) % $block_size) {
  740.  $buffer = substr($key, $start) . $buffer;
  741. }
  742. }
  743. }
  744.  
  745. return $this->paddable ? $this->_unpad($plaintext) : $plaintext;
  746. }
  747. function _encryptBlock($in)
  748. {
  749. $state = array();
  750. $words = unpack('N*word', $in);
  751.  
  752. $w = $this->w;
  753. $t0 = $this->t0;
  754. $t1 = $this->t1;
  755. $t2 = $this->t2;
  756. $t3 = $this->t3;
  757. $Nb = $this->Nb;
  758. $Nr = $this->Nr;
  759. $c = $this->c;
  760.  
  761. $i = 0;
  762. foreach ($words as $word) {
  763. $state[] = $word ^ $w[0][$i++];
  764. }
  765. $temp = array();
  766. for ($round = 1; $round < $Nr; $round++) {
  767. $i = 0; // $c[0] == 0
  768. $j = $c[1];
  769. $k = $c[2];
  770. $l = $c[3];
  771.  
  772. while ($i < $this->Nb) {
  773. $temp[$i] = $t0[$state[$i] & 0xFF000000] ^
  774. $t1[$state[$j] & 0x00FF0000] ^
  775. $t2[$state[$k] & 0x0000FF00] ^
  776. $t3[$state[$l] & 0x000000FF] ^
  777. $w[$round][$i];
  778. $i++;
  779. $j = ($j + 1) % $Nb;
  780. $k = ($k + 1) % $Nb;
  781. $l = ($l + 1) % $Nb;
  782. }
  783.  
  784. for ($i = 0; $i < $Nb; $i++) {
  785. $state[$i] = $temp[$i];
  786. }
  787. }
  788.  
  789. for ($i = 0; $i < $Nb; $i++) {
  790. $state[$i] = $this->_subWord($state[$i]);
  791. }
  792.  
  793. $i = 0;
  794. $j = $c[1];
  795. $k = $c[2];
  796. $l = $c[3];
  797. while ($i < $this->Nb) {
  798. $temp[$i] = ($state[$i] & 0xFF000000) ^
  799. ($state[$j] & 0x00FF0000) ^
  800. ($state[$k] & 0x0000FF00) ^
  801. ($state[$l] & 0x000000FF) ^
  802.  $w[$Nr][$i];
  803. $i++;
  804. $j = ($j + 1) % $Nb;
  805. $k = ($k + 1) % $Nb;
  806. $l = ($l + 1) % $Nb;
  807. }
  808. $state = $temp;
  809.  
  810. array_unshift($state, 'N*');
  811.  
  812. return call_user_func_array('pack', $state);
  813. }
  814. function _decryptBlock($in)
  815. {
  816. $state = array();
  817. $words = unpack('N*word', $in);
  818.  
  819. $num_states = count($state);
  820. $dw = $this->dw;
  821. $dt0 = $this->dt0;
  822. $dt1 = $this->dt1;
  823. $dt2 = $this->dt2;
  824. $dt3 = $this->dt3;
  825. $Nb = $this->Nb;
  826. $Nr = $this->Nr;
  827. $c = $this->c;
  828. $i = 0;
  829. foreach ($words as $word) {
  830. $state[] = $word ^ $dw[$Nr][$i++];
  831. }
  832.  
  833. $temp = array();
  834. for ($round = $Nr - 1; $round > 0; $round--) {
  835. $i = 0;
  836. $j = $Nb - $c[1];
  837. $k = $Nb - $c[2];
  838. $l = $Nb - $c[3];
  839.  
  840. while ($i < $Nb) {
  841. $temp[$i] = $dt0[$state[$i] & 0xFF000000] ^
  842. $dt1[$state[$j] & 0x00FF0000] ^
  843. $dt2[$state[$k] & 0x0000FF00] ^
  844. $dt3[$state[$l] & 0x000000FF] ^
  845. $dw[$round][$i];
  846. $i++;
  847. $j = ($j + 1) % $Nb;
  848. $k = ($k + 1) % $Nb;
  849. $l = ($l + 1) % $Nb;
  850. }
  851.  
  852. for ($i = 0; $i < $Nb; $i++) {
  853. $state[$i] = $temp[$i];
  854. }
  855. }
  856. $i = 0;
  857. $j = $Nb - $c[1];
  858. $k = $Nb - $c[2];
  859. $l = $Nb - $c[3];
  860.  
  861. while ($i < $Nb) {
  862. $temp[$i] = $dw[0][$i] ^
  863. $this->_invSubWord(($state[$i] & 0xFF000000) |
  864.    ($state[$j] & 0x00FF0000) |
  865.    ($state[$k] & 0x0000FF00) |
  866.    ($state[$l] & 0x000000FF));
  867. $i++;
  868. $j = ($j + 1) % $Nb;
  869. $k = ($k + 1) % $Nb;
  870. $l = ($l + 1) % $Nb;
  871. }
  872.  
  873. $state = $temp;
  874.  
  875. array_unshift($state, 'N*');
  876.  
  877. return call_user_func_array('pack', $state);
  878. }
  879.  
  880. function _setup()
  881. {
  882. static $rcon = array(0,
  883. 0x01000000, 0x02000000, 0x04000000, 0x08000000, 0x10000000,
  884. 0x20000000, 0x40000000, 0x80000000, 0x1B000000, 0x36000000,
  885. 0x6C000000, 0xD8000000, 0xAB000000, 0x4D000000, 0x9A000000,
  886. 0x2F000000, 0x5E000000, 0xBC000000, 0x63000000, 0xC6000000,
  887. 0x97000000, 0x35000000, 0x6A000000, 0xD4000000, 0xB3000000,
  888. 0x7D000000, 0xFA000000, 0xEF000000, 0xC5000000, 0x91000000
  889. );
  890.  
  891. if (!$this->changed) {
  892. return;
  893. }
  894.  
  895. if (!$this->explicit_key_length) {
  896. $length = strlen($this->key) >> 2;
  897. if ($length > 8) {
  898. $length = 8;
  899. } else if ($length < 4) {
  900. $length = 4;
  901. }
  902. $this->Nk = $length;
  903. $this->key_size = $length << 2;
  904. }
  905.  
  906. $this->key = str_pad(substr($this->key, 0, $this->key_size), $this->key_size, chr(0));
  907. $this->encryptIV = $this->decryptIV = $this->iv = str_pad(substr($this->iv, 0, $this->block_size), $this->block_size, chr(0));
  908.  
  909. $this->Nr = max($this->Nk, $this->Nb) + 6;
  910.  
  911. switch ($this->Nb) {
  912. case 4:
  913. case 5:
  914. case 6:
  915. $this->c = array(0, 1, 2, 3);
  916. break;
  917. case 7:
  918. $this->c = array(0, 1, 2, 4);
  919. break;
  920. case 8:
  921. $this->c = array(0, 1, 3, 4);
  922. }
  923.  
  924. $key = $this->key;
  925.  
  926. $w = array_values(unpack('N*words', $key));
  927.  
  928. $length = $this->Nb * ($this->Nr + 1);
  929. for ($i = $this->Nk; $i < $length; $i++) {
  930. $temp = $w[$i - 1];
  931. if ($i % $this->Nk == 0) {
  932. $temp = (($temp << 8) & 0xFFFFFF00) | (($temp >> 24) & 0x000000FF);
  933. $temp = $this->_subWord($temp) ^ $rcon[$i / $this->Nk];
  934. } else if ($this->Nk > 6 && $i % $this->Nk == 4) {
  935. $temp = $this->_subWord($temp);
  936. }
  937. $w[$i] = $w[$i - $this->Nk] ^ $temp;
  938. }
  939. $temp = array();
  940. for ($i = $row = $col = 0; $i < $length; $i++, $col++) {
  941. if ($col == $this->Nb) {
  942. if ($row == 0) {
  943. $this->dw[0] = $this->w[0];
  944. } else {
  945. $j = 0;
  946. while ($j < $this->Nb) {
  947. $dw = $this->_subWord($this->w[$row][$j]);
  948. $temp[$j] = $this->dt0[$dw & 0xFF000000] ^
  949. $this->dt1[$dw & 0x00FF0000] ^
  950. $this->dt2[$dw & 0x0000FF00] ^
  951. $this->dt3[$dw & 0x000000FF];
  952. $j++;
  953. }
  954. $this->dw[$row] = $temp;
  955. }
  956.  
  957. $col = 0;
  958. $row++;
  959. }
  960. $this->w[$row][$col] = $w[$i];
  961. }
  962.  
  963. $this->dw[$row] = $this->w[$row];
  964.  
  965. $this->changed = false;
  966. }
  967. function _subWord($word)
  968. {
  969. static $sbox0, $sbox1, $sbox2, $sbox3;
  970.  
  971. if (empty($sbox0)) {
  972. $sbox0 = array(
  973. 0x63, 0x7C, 0x77, 0x7B, 0xF2, 0x6B, 0x6F, 0xC5, 0x30, 0x01, 0x67, 0x2B, 0xFE, 0xD7, 0xAB, 0x76,
  974. 0xCA, 0x82, 0xC9, 0x7D, 0xFA, 0x59, 0x47, 0xF0, 0xAD, 0xD4, 0xA2, 0xAF, 0x9C, 0xA4, 0x72, 0xC0,
  975. 0xB7, 0xFD, 0x93, 0x26, 0x36, 0x3F, 0xF7, 0xCC, 0x34, 0xA5, 0xE5, 0xF1, 0x71, 0xD8, 0x31, 0x15,
  976. 0x04, 0xC7, 0x23, 0xC3, 0x18, 0x96, 0x05, 0x9A, 0x07, 0x12, 0x80, 0xE2, 0xEB, 0x27, 0xB2, 0x75,
  977. 0x09, 0x83, 0x2C, 0x1A, 0x1B, 0x6E, 0x5A, 0xA0, 0x52, 0x3B, 0xD6, 0xB3, 0x29, 0xE3, 0x2F, 0x84,
  978. 0x53, 0xD1, 0x00, 0xED, 0x20, 0xFC, 0xB1, 0x5B, 0x6A, 0xCB, 0xBE, 0x39, 0x4A, 0x4C, 0x58, 0xCF,
  979. 0xD0, 0xEF, 0xAA, 0xFB, 0x43, 0x4D, 0x33, 0x85, 0x45, 0xF9, 0x02, 0x7F, 0x50, 0x3C, 0x9F, 0xA8,
  980. 0x51, 0xA3, 0x40, 0x8F, 0x92, 0x9D, 0x38, 0xF5, 0xBC, 0xB6, 0xDA, 0x21, 0x10, 0xFF, 0xF3, 0xD2,
  981. 0xCD, 0x0C, 0x13, 0xEC, 0x5F, 0x97, 0x44, 0x17, 0xC4, 0xA7, 0x7E, 0x3D, 0x64, 0x5D, 0x19, 0x73,
  982. 0x60, 0x81, 0x4F, 0xDC, 0x22, 0x2A, 0x90, 0x88, 0x46, 0xEE, 0xB8, 0x14, 0xDE, 0x5E, 0x0B, 0xDB,
  983. 0xE0, 0x32, 0x3A, 0x0A, 0x49, 0x06, 0x24, 0x5C, 0xC2, 0xD3, 0xAC, 0x62, 0x91, 0x95, 0xE4, 0x79,
  984. 0xE7, 0xC8, 0x37, 0x6D, 0x8D, 0xD5, 0x4E, 0xA9, 0x6C, 0x56, 0xF4, 0xEA, 0x65, 0x7A, 0xAE, 0x08,
  985. 0xBA, 0x78, 0x25, 0x2E, 0x1C, 0xA6, 0xB4, 0xC6, 0xE8, 0xDD, 0x74, 0x1F, 0x4B, 0xBD, 0x8B, 0x8A,
  986. 0x70, 0x3E, 0xB5, 0x66, 0x48, 0x03, 0xF6, 0x0E, 0x61, 0x35, 0x57, 0xB9, 0x86, 0xC1, 0x1D, 0x9E,
  987. 0xE1, 0xF8, 0x98, 0x11, 0x69, 0xD9, 0x8E, 0x94, 0x9B, 0x1E, 0x87, 0xE9, 0xCE, 0x55, 0x28, 0xDF,
  988. 0x8C, 0xA1, 0x89, 0x0D, 0xBF, 0xE6, 0x42, 0x68, 0x41, 0x99, 0x2D, 0x0F, 0xB0, 0x54, 0xBB, 0x16
  989. );
  990.  
  991. $sbox1 = array();
  992. $sbox2 = array();
  993. $sbox3 = array();
  994.  
  995. for ($i = 0; $i < 256; $i++) {
  996. $sbox1[$i <<  8] = $sbox0[$i] <<  8;
  997. $sbox2[$i << 16] = $sbox0[$i] << 16;
  998. $sbox3[$i << 24] = $sbox0[$i] << 24;
  999. }
  1000. }
  1001.  
  1002. return $sbox0[$word & 0x000000FF] |
  1003.    $sbox1[$word & 0x0000FF00] |
  1004.    $sbox2[$word & 0x00FF0000] |
  1005.    $sbox3[$word & 0xFF000000];
  1006. }
  1007. function _invSubWord($word)
  1008. {
  1009. static $sbox0, $sbox1, $sbox2, $sbox3;
  1010.  
  1011. if (empty($sbox0)) {
  1012. $sbox0 = array(
  1013. 0x52, 0x09, 0x6A, 0xD5, 0x30, 0x36, 0xA5, 0x38, 0xBF, 0x40, 0xA3, 0x9E, 0x81, 0xF3, 0xD7, 0xFB,
  1014. 0x7C, 0xE3, 0x39, 0x82, 0x9B, 0x2F, 0xFF, 0x87, 0x34, 0x8E, 0x43, 0x44, 0xC4, 0xDE, 0xE9, 0xCB,
  1015. 0x54, 0x7B, 0x94, 0x32, 0xA6, 0xC2, 0x23, 0x3D, 0xEE, 0x4C, 0x95, 0x0B, 0x42, 0xFA, 0xC3, 0x4E,
  1016. 0x08, 0x2E, 0xA1, 0x66, 0x28, 0xD9, 0x24, 0xB2, 0x76, 0x5B, 0xA2, 0x49, 0x6D, 0x8B, 0xD1, 0x25,
  1017. 0x72, 0xF8, 0xF6, 0x64, 0x86, 0x68, 0x98, 0x16, 0xD4, 0xA4, 0x5C, 0xCC, 0x5D, 0x65, 0xB6, 0x92,
  1018. 0x6C, 0x70, 0x48, 0x50, 0xFD, 0xED, 0xB9, 0xDA, 0x5E, 0x15, 0x46, 0x57, 0xA7, 0x8D, 0x9D, 0x84,
  1019. 0x90, 0xD8, 0xAB, 0x00, 0x8C, 0xBC, 0xD3, 0x0A, 0xF7, 0xE4, 0x58, 0x05, 0xB8, 0xB3, 0x45, 0x06,
  1020. 0xD0, 0x2C, 0x1E, 0x8F, 0xCA, 0x3F, 0x0F, 0x02, 0xC1, 0xAF, 0xBD, 0x03, 0x01, 0x13, 0x8A, 0x6B,
  1021. 0x3A, 0x91, 0x11, 0x41, 0x4F, 0x67, 0xDC, 0xEA, 0x97, 0xF2, 0xCF, 0xCE, 0xF0, 0xB4, 0xE6, 0x73,
  1022. 0x96, 0xAC, 0x74, 0x22, 0xE7, 0xAD, 0x35, 0x85, 0xE2, 0xF9, 0x37, 0xE8, 0x1C, 0x75, 0xDF, 0x6E,
  1023. 0x47, 0xF1, 0x1A, 0x71, 0x1D, 0x29, 0xC5, 0x89, 0x6F, 0xB7, 0x62, 0x0E, 0xAA, 0x18, 0xBE, 0x1B,
  1024. 0xFC, 0x56, 0x3E, 0x4B, 0xC6, 0xD2, 0x79, 0x20, 0x9A, 0xDB, 0xC0, 0xFE, 0x78, 0xCD, 0x5A, 0xF4,
  1025. 0x1F, 0xDD, 0xA8, 0x33, 0x88, 0x07, 0xC7, 0x31, 0xB1, 0x12, 0x10, 0x59, 0x27, 0x80, 0xEC, 0x5F,
  1026. 0x60, 0x51, 0x7F, 0xA9, 0x19, 0xB5, 0x4A, 0x0D, 0x2D, 0xE5, 0x7A, 0x9F, 0x93, 0xC9, 0x9C, 0xEF,
  1027. 0xA0, 0xE0, 0x3B, 0x4D, 0xAE, 0x2A, 0xF5, 0xB0, 0xC8, 0xEB, 0xBB, 0x3C, 0x83, 0x53, 0x99, 0x61,
  1028. 0x17, 0x2B, 0x04, 0x7E, 0xBA, 0x77, 0xD6, 0x26, 0xE1, 0x69, 0x14, 0x63, 0x55, 0x21, 0x0C, 0x7D
  1029. );
  1030.  
  1031. $sbox1 = array();
  1032. $sbox2 = array();
  1033. $sbox3 = array();
  1034.  
  1035. for ($i = 0; $i < 256; $i++) {
  1036. $sbox1[$i <<  8] = $sbox0[$i] <<  8;
  1037. $sbox2[$i << 16] = $sbox0[$i] << 16;
  1038. $sbox3[$i << 24] = $sbox0[$i] << 24;
  1039. }
  1040. }
  1041.  
  1042. return $sbox0[$word & 0x000000FF] |
  1043.    $sbox1[$word & 0x0000FF00] |
  1044.    $sbox2[$word & 0x00FF0000] |
  1045.    $sbox3[$word & 0xFF000000];
  1046. }
  1047. function enablePadding()
  1048. {
  1049. $this->padding = true;
  1050. }
  1051. function disablePadding()
  1052. {
  1053. $this->padding = false;
  1054. }
  1055. function _pad($text)
  1056. {
  1057. $length = strlen($text);
  1058.  
  1059. if (!$this->padding) {
  1060. if ($length % $this->block_size == 0) {
  1061. return $text;
  1062. } else {
  1063. user_error("The plaintext's length ($length) is not a multiple of the block size ({$this->block_size})", E_USER_NOTICE);
  1064. $this->padding = true;
  1065. }
  1066. }
  1067.  
  1068. $pad = $this->block_size - ($length % $this->block_size);
  1069.  
  1070. return str_pad($text, $length + $pad, chr($pad));
  1071. }
  1072. function _unpad($text)
  1073. {
  1074. if (!$this->padding) {
  1075. return $text;
  1076. }
  1077.  
  1078. $length = ord($text[strlen($text) - 1]);
  1079.  
  1080. if (!$length || $length > $this->block_size) {
  1081. return false;
  1082. }
  1083.  
  1084. return substr($text, 0, -$length);
  1085. }
  1086. function enableContinuousBuffer()
  1087. {
  1088. $this->continuousBuffer = true;
  1089. }
  1090. function disableContinuousBuffer()
  1091. {
  1092. $this->continuousBuffer = false;
  1093. $this->encryptIV = $this->iv;
  1094. $this->decryptIV = $this->iv;
  1095. }
  1096. function _string_shift(&$string, $index = 1)
  1097. {
  1098. $substr = substr($string, 0, $index);
  1099. $string = substr($string, $index);
  1100. return $substr;
  1101. }
  1102. }
  1103.  
  1104. define('CRYPT_AES_MODE_CTR', -1);
  1105. define('CRYPT_AES_MODE_ECB', 1);
  1106. define('CRYPT_AES_MODE_CBC', 2);
  1107. define('CRYPT_AES_MODE_CFB', 3);
  1108. define('CRYPT_AES_MODE_OFB', 4);
  1109. define('CRYPT_AES_MODE_INTERNAL', 1);
  1110. define('CRYPT_AES_MODE_MCRYPT', 2);
  1111. class Crypt_AES extends Crypt_Rijndael {
  1112. var $enmcrypt;
  1113. var $demcrypt;
  1114. var $ecb;
  1115. function Crypt_AES($mode = CRYPT_AES_MODE_CBC)
  1116. {
  1117. if ( !defined('CRYPT_AES_MODE') ) {
  1118. switch (true) {
  1119. case extension_loaded('mcrypt') && in_array('rijndael-128', mcrypt_list_algorithms()):
  1120. define('CRYPT_AES_MODE', CRYPT_AES_MODE_MCRYPT);
  1121. break;
  1122. default:
  1123. define('CRYPT_AES_MODE', CRYPT_AES_MODE_INTERNAL);
  1124. }
  1125. }
  1126.  
  1127. switch ( CRYPT_AES_MODE ) {
  1128. case CRYPT_AES_MODE_MCRYPT:
  1129. switch ($mode) {
  1130. case CRYPT_AES_MODE_ECB:
  1131. $this->paddable = true;
  1132. $this->mode = MCRYPT_MODE_ECB;
  1133. break;
  1134. case CRYPT_AES_MODE_CTR:
  1135. $this->mode = 'ctr';
  1136. break;
  1137. case CRYPT_AES_MODE_CFB:
  1138. $this->mode = 'ncfb';
  1139. break;
  1140. case CRYPT_AES_MODE_OFB:
  1141. $this->mode = MCRYPT_MODE_NOFB;
  1142. break;
  1143. case CRYPT_AES_MODE_CBC:
  1144. default:
  1145. $this->paddable = true;
  1146. $this->mode = MCRYPT_MODE_CBC;
  1147. }
  1148.  
  1149. $this->debuffer = $this->enbuffer = '';
  1150.  
  1151. break;
  1152. default:
  1153. switch ($mode) {
  1154. case CRYPT_AES_MODE_ECB:
  1155. $this->paddable = true;
  1156. $this->mode = CRYPT_RIJNDAEL_MODE_ECB;
  1157. break;
  1158. case CRYPT_AES_MODE_CTR:
  1159. $this->mode = CRYPT_RIJNDAEL_MODE_CTR;
  1160. break;
  1161. case CRYPT_AES_MODE_CFB:
  1162. $this->mode = CRYPT_RIJNDAEL_MODE_CFB;
  1163. break;
  1164. case CRYPT_AES_MODE_OFB:
  1165. $this->mode = CRYPT_RIJNDAEL_MODE_OFB;
  1166. break;
  1167. case CRYPT_AES_MODE_CBC:
  1168. default:
  1169. $this->paddable = true;
  1170. $this->mode = CRYPT_RIJNDAEL_MODE_CBC;
  1171. }
  1172. }
  1173.  
  1174. if (CRYPT_AES_MODE == CRYPT_AES_MODE_INTERNAL) {
  1175. parent::Crypt_Rijndael($this->mode);
  1176. }
  1177. }
  1178. function setBlockLength($length)
  1179. {
  1180. return;
  1181. }
  1182. function setIV($iv)
  1183. {
  1184. parent::setIV($iv);
  1185. if ( CRYPT_AES_MODE == CRYPT_AES_MODE_MCRYPT ) {
  1186. $this->changed = true;
  1187. }
  1188. }
  1189. function encrypt($plaintext)
  1190. {
  1191. if ( CRYPT_AES_MODE == CRYPT_AES_MODE_MCRYPT ) {
  1192. $changed = $this->changed;
  1193. $this->_mcryptSetup();
  1194. if ($this->mode == 'ncfb') {
  1195. if ($changed) {
  1196. $this->ecb = mcrypt_module_open(MCRYPT_RIJNDAEL_128, '', MCRYPT_MODE_ECB, '');
  1197. mcrypt_generic_init($this->ecb, $this->key, "\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0");
  1198. }
  1199.  
  1200. if (strlen($this->enbuffer)) {
  1201. $ciphertext = $plaintext ^ substr($this->encryptIV, strlen($this->enbuffer));
  1202. $this->enbuffer.= $ciphertext;
  1203. if (strlen($this->enbuffer) == 16) {
  1204. $this->encryptIV = $this->enbuffer;
  1205. $this->enbuffer = '';
  1206. mcrypt_generic_init($this->enmcrypt, $this->key, $this->encryptIV);
  1207. }
  1208. $plaintext = substr($plaintext, strlen($ciphertext));
  1209. } else {
  1210. $ciphertext = '';
  1211. }
  1212.  
  1213. $last_pos = strlen($plaintext) & 0xFFFFFFF0;
  1214. $ciphertext.= $last_pos ? mcrypt_generic($this->enmcrypt, substr($plaintext, 0, $last_pos)) : '';
  1215.  
  1216. if (strlen($plaintext) & 0xF) {
  1217. if (strlen($ciphertext)) {
  1218. $this->encryptIV = substr($ciphertext, -16);
  1219. }
  1220. $this->encryptIV = mcrypt_generic($this->ecb, $this->encryptIV);
  1221. $this->enbuffer = substr($plaintext, $last_pos) ^ $this->encryptIV;
  1222. $ciphertext.= $this->enbuffer;
  1223. }
  1224.  
  1225. return $ciphertext;
  1226. }
  1227.  
  1228. if ($this->paddable) {
  1229. $plaintext = $this->_pad($plaintext);
  1230. }
  1231.  
  1232. $ciphertext = mcrypt_generic($this->enmcrypt, $plaintext);
  1233.  
  1234. if (!$this->continuousBuffer) {
  1235. mcrypt_generic_init($this->enmcrypt, $this->key, $this->iv);
  1236. }
  1237.  
  1238. return $ciphertext;
  1239. }
  1240.  
  1241. return parent::encrypt($plaintext);
  1242. }
  1243. function decrypt($ciphertext)
  1244. {
  1245. if ( CRYPT_AES_MODE == CRYPT_AES_MODE_MCRYPT ) {
  1246. $changed = $this->changed;
  1247. $this->_mcryptSetup();
  1248. if ($this->mode == 'ncfb') {
  1249. if ($changed) {
  1250. $this->ecb = mcrypt_module_open(MCRYPT_RIJNDAEL_128, '', MCRYPT_MODE_ECB, '');
  1251. mcrypt_generic_init($this->ecb, $this->key, "\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0");
  1252. }
  1253.  
  1254. if (strlen($this->debuffer)) {
  1255. $plaintext = $ciphertext ^ substr($this->decryptIV, strlen($this->debuffer));
  1256.  
  1257. $this->debuffer.= substr($ciphertext, 0, strlen($plaintext));
  1258. if (strlen($this->debuffer) == 16) {
  1259. $this->decryptIV = $this->debuffer;
  1260. $this->debuffer = '';
  1261. mcrypt_generic_init($this->demcrypt, $this->key, $this->decryptIV);
  1262. }
  1263. $ciphertext = substr($ciphertext, strlen($plaintext));
  1264. } else {
  1265. $plaintext = '';
  1266. }
  1267.  
  1268. $last_pos = strlen($ciphertext) & 0xFFFFFFF0;
  1269. $plaintext.= $last_pos ? mdecrypt_generic($this->demcrypt, substr($ciphertext, 0, $last_pos)) : '';
  1270.  
  1271. if (strlen($ciphertext) & 0xF) {
  1272. if (strlen($plaintext)) {
  1273. $this->decryptIV = substr($ciphertext, $last_pos - 16, 16);
  1274. }
  1275. $this->decryptIV = mcrypt_generic($this->ecb, $this->decryptIV);
  1276. $this->debuffer = substr($ciphertext, $last_pos);
  1277. $plaintext.= $this->debuffer ^ $this->decryptIV;
  1278. }
  1279.  
  1280. return $plaintext;
  1281. }
  1282.  
  1283. if ($this->paddable) {
  1284. $ciphertext = str_pad($ciphertext, (strlen($ciphertext) + 15) & 0xFFFFFFF0, chr(0));
  1285. }
  1286.  
  1287. $plaintext = mdecrypt_generic($this->demcrypt, $ciphertext);
  1288.  
  1289. if (!$this->continuousBuffer) {
  1290. mcrypt_generic_init($this->demcrypt, $this->key, $this->iv);
  1291. }
  1292.  
  1293. return $this->paddable ? $this->_unpad($plaintext) : $plaintext;
  1294. }
  1295.  
  1296. return parent::decrypt($ciphertext);
  1297. }
  1298. function _mcryptSetup()
  1299. {
  1300. if (!$this->changed) {
  1301. return;
  1302. }
  1303.  
  1304. if (!$this->explicit_key_length) {
  1305. $length = strlen($this->key) >> 2;
  1306. if ($length > 8) {
  1307. $length = 8;
  1308. } else if ($length < 4) {
  1309. $length = 4;
  1310. }
  1311. $this->Nk = $length;
  1312. $this->key_size = $length << 2;
  1313. }
  1314.  
  1315. switch ($this->Nk) {
  1316. case 4:
  1317. $this->key_size = 16;
  1318. break;
  1319. case 5:
  1320. case 6:
  1321. $this->key_size = 24;
  1322. break;
  1323. case 7:
  1324. case 8:
  1325. $this->key_size = 32;
  1326. }
  1327.  
  1328. $this->key = str_pad(substr($this->key, 0, $this->key_size), $this->key_size, chr(0));
  1329. $this->encryptIV = $this->decryptIV = $this->iv = str_pad(substr($this->iv, 0, 16), 16, chr(0));
  1330.  
  1331. if (!isset($this->enmcrypt)) {
  1332. $mode = $this->mode;
  1333.  
  1334. $this->demcrypt = mcrypt_module_open(MCRYPT_RIJNDAEL_128, '', $mode, '');
  1335. $this->enmcrypt = mcrypt_module_open(MCRYPT_RIJNDAEL_128, '', $mode, '');
  1336. }
  1337.  
  1338. mcrypt_generic_init($this->demcrypt, $this->key, $this->iv);
  1339. mcrypt_generic_init($this->enmcrypt, $this->key, $this->iv);
  1340.  
  1341. $this->changed = false;
  1342. }
  1343. function _encryptBlock($in)
  1344. {
  1345. $state = unpack('N*word', $in);
  1346.  
  1347. $Nr = $this->Nr;
  1348. $w = $this->w;
  1349. $t0 = $this->t0;
  1350. $t1 = $this->t1;
  1351. $t2 = $this->t2;
  1352. $t3 = $this->t3;
  1353.  
  1354. $state = array(
  1355. $state['word1'] ^ $w[0][0],
  1356. $state['word2'] ^ $w[0][1],
  1357. $state['word3'] ^ $w[0][2],
  1358. $state['word4'] ^ $w[0][3]
  1359. );
  1360. for ($round = 1; $round < $this->Nr; $round++) {
  1361. $state = array(
  1362. $t0[$state[0] & 0xFF000000] ^ $t1[$state[1] & 0x00FF0000] ^ $t2[$state[2] & 0x0000FF00] ^ $t3[$state[3] & 0x000000FF] ^ $w[$round][0],
  1363. $t0[$state[1] & 0xFF000000] ^ $t1[$state[2] & 0x00FF0000] ^ $t2[$state[3] & 0x0000FF00] ^ $t3[$state[0] & 0x000000FF] ^ $w[$round][1],
  1364. $t0[$state[2] & 0xFF000000] ^ $t1[$state[3] & 0x00FF0000] ^ $t2[$state[0] & 0x0000FF00] ^ $t3[$state[1] & 0x000000FF] ^ $w[$round][2],
  1365. $t0[$state[3] & 0xFF000000] ^ $t1[$state[0] & 0x00FF0000] ^ $t2[$state[1] & 0x0000FF00] ^ $t3[$state[2] & 0x000000FF] ^ $w[$round][3]
  1366. );
  1367.  
  1368. }
  1369. $state = array(
  1370. $this->_subWord($state[0]),
  1371. $this->_subWord($state[1]),
  1372. $this->_subWord($state[2]),
  1373. $this->_subWord($state[3])
  1374. );
  1375.  
  1376. $state = array(
  1377. ($state[0] & 0xFF000000) ^ ($state[1] & 0x00FF0000) ^ ($state[2] & 0x0000FF00) ^ ($state[3] & 0x000000FF) ^ $this->w[$this->Nr][0],
  1378. ($state[1] & 0xFF000000) ^ ($state[2] & 0x00FF0000) ^ ($state[3] & 0x0000FF00) ^ ($state[0] & 0x000000FF) ^ $this->w[$this->Nr][1],
  1379. ($state[2] & 0xFF000000) ^ ($state[3] & 0x00FF0000) ^ ($state[0] & 0x0000FF00) ^ ($state[1] & 0x000000FF) ^ $this->w[$this->Nr][2],
  1380. ($state[3] & 0xFF000000) ^ ($state[0] & 0x00FF0000) ^ ($state[1] & 0x0000FF00) ^ ($state[2] & 0x000000FF) ^ $this->w[$this->Nr][3]
  1381. );
  1382.  
  1383. return pack('N*', $state[0], $state[1], $state[2], $state[3]);
  1384. }
  1385.  
  1386. function _decryptBlock($in)
  1387. {
  1388. $state = unpack('N*word', $in);
  1389.  
  1390. $Nr = $this->Nr;
  1391. $dw = $this->dw;
  1392. $dt0 = $this->dt0;
  1393. $dt1 = $this->dt1;
  1394. $dt2 = $this->dt2;
  1395. $dt3 = $this->dt3;
  1396.  
  1397. $state = array(
  1398. $state['word1'] ^ $dw[$this->Nr][0],
  1399. $state['word2'] ^ $dw[$this->Nr][1],
  1400. $state['word3'] ^ $dw[$this->Nr][2],
  1401. $state['word4'] ^ $dw[$this->Nr][3]
  1402. );
  1403.  
  1404. for ($round = $this->Nr - 1; $round > 0; $round--) {
  1405. $state = array(
  1406. $dt0[$state[0] & 0xFF000000] ^ $dt1[$state[3] & 0x00FF0000] ^ $dt2[$state[2] & 0x0000FF00] ^ $dt3[$state[1] & 0x000000FF] ^ $dw[$round][0],
  1407. $dt0[$state[1] & 0xFF000000] ^ $dt1[$state[0] & 0x00FF0000] ^ $dt2[$state[3] & 0x0000FF00] ^ $dt3[$state[2] & 0x000000FF] ^ $dw[$round][1],
  1408. $dt0[$state[2] & 0xFF000000] ^ $dt1[$state[1] & 0x00FF0000] ^ $dt2[$state[0] & 0x0000FF00] ^ $dt3[$state[3] & 0x000000FF] ^ $dw[$round][2],
  1409. $dt0[$state[3] & 0xFF000000] ^ $dt1[$state[2] & 0x00FF0000] ^ $dt2[$state[1] & 0x0000FF00] ^ $dt3[$state[0] & 0x000000FF] ^ $dw[$round][3]
  1410. );
  1411. }
  1412.  
  1413. $state = array(
  1414. $this->_invSubWord(($state[0] & 0xFF000000) ^ ($state[3] & 0x00FF0000) ^ ($state[2] & 0x0000FF00) ^ ($state[1] & 0x000000FF)) ^ $dw[0][0],
  1415. $this->_invSubWord(($state[1] & 0xFF000000) ^ ($state[0] & 0x00FF0000) ^ ($state[3] & 0x0000FF00) ^ ($state[2] & 0x000000FF)) ^ $dw[0][1],
  1416. $this->_invSubWord(($state[2] & 0xFF000000) ^ ($state[1] & 0x00FF0000) ^ ($state[0] & 0x0000FF00) ^ ($state[3] & 0x000000FF)) ^ $dw[0][2],
  1417. $this->_invSubWord(($state[3] & 0xFF000000) ^ ($state[2] & 0x00FF0000) ^ ($state[1] & 0x0000FF00) ^ ($state[0] & 0x000000FF)) ^ $dw[0][3]
  1418. );
  1419.  
  1420. return pack('N*', $state[0], $state[1], $state[2], $state[3]);
  1421. }
  1422. }
  1423. ?>
Advertisement
Add Comment
Please, Sign In to add comment