Advertisement
Deltik

MaText.php

Aug 5th, 2011
289
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 14.74 KB | None | 0 0
  1. <?php
  2. /**
  3.  * Encryptext
  4.  *  Classes
  5.  *   Ported
  6.  *    "MaText v2.1.0"
  7.  *     From: TI-BASIC
  8.  *      By: Nick Liu <webmaster@deltik.org>
  9.  *       Source: <http://www.ticalc.org/archives/files/fileinfo/420/42081.html>
  10.  */
  11.  
  12. #### CONTENTS ####
  13. # Class Variables   - "MaText Variable Declarations"
  14. # Class Constructor - "MaText Class Constructor"
  15. # Low-Level Access  - "MaText Binary Functions"
  16. # Basic Operations  - "MaText Data Manipulation Functions"
  17. # Purpose Functions - "MaText Encryption Functions"
  18. # Storage Functions - "MaText File Operations"
  19. # Miscellaneous     - "MaText Original Source Code"
  20. ##################
  21.  
  22. class MaText extends Matrix
  23.   {
  24.   /**
  25.    * ################################
  26.    * # MaText Variable Declarations #
  27.    * ################################
  28.    */
  29.   // System
  30.   public $charset = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";
  31.   // Process
  32.   public $text;
  33.   public $list;
  34.  
  35.   /**
  36.    * ############################
  37.    * # MaText Class Constructor #
  38.    * ############################
  39.    */
  40.   public function MaText()
  41.     {
  42.     $argv = func_get_args();
  43.     if (is_string($argv[0]))
  44.       return setText($argv[0]);
  45.     }
  46.  
  47.   /**
  48.    * ###########################
  49.    * # MaText Binary Functions #
  50.    * ###########################
  51.    */
  52.  
  53.   /**
  54.    * Hexadecimal to String
  55.    * @param string $x Hexadecimal safe binary string to ASCII string
  56.    */
  57.   public function hextostr($x)
  58.     {
  59.     $s = '';
  60.     foreach (explode("\n", trim(chunk_split($x, 2))) as $h)
  61.       $s .= chr(hexdec($h));
  62.     return $s;
  63.     }
  64.  
  65.   /**
  66.    * String to Hexadecimal
  67.    * @param string $x ASCII string to hexadecimal safe binary string
  68.    */
  69.   public function strtohex($x)
  70.     {
  71.     $s = '';
  72.     foreach (str_split($x) as $c)
  73.       $s .= sprintf("%02X", ord($c));
  74.     return $s;
  75.     }
  76.  
  77.   /**
  78.    * ######################################
  79.    * # MaText Data Manipulation Functions #
  80.    * ######################################
  81.    */
  82.  
  83.   /**
  84.    * Load Text Input
  85.    * @param string $string Inputted text
  86.    * @returns bool TRUE - Guaranteed success
  87.    */
  88.   public function setText($string)
  89.     {
  90.     $this->text = $string;
  91.     return true;
  92.     }
  93.  
  94.   /**
  95.    * Make Numeric Representation of Text
  96.    * @param string $string (optional) Inputted text
  97.    * @returns array The converted list
  98.    */
  99.   public function makeList($string = null)
  100.     {
  101.     // Fall back on pre-set text
  102.     if ($string == null)
  103.       $string = $this->text;
  104.     // Return failure if the string isn't set
  105.     if (!is_string($string) || !strlen($string))
  106.       return false;
  107.     // Break break break break break some strings
  108.     $string_parts = str_split($string);
  109.     $charset_parts = array_flip(str_split($this->charset));
  110.    
  111.     // Do numeric encode
  112.     foreach ($string_parts as $string_char)
  113.       {
  114.       $final[] = $charset_parts[$string_char];
  115.       }
  116.    
  117.     // Store the created list in the object.
  118.     $this->list = $final;
  119.    
  120.     // Return the created list, just to be nice.
  121.     return $final;
  122.     }
  123.  
  124.   /**
  125.    * Make Textual Representation of Numbers
  126.    * @param array $list (optional) Inputted list
  127.    * @returns string The converted text
  128.    */
  129.   public function makeText($list = null)
  130.     {
  131.     // Fall back on pre-set list
  132.     if ($list == null)
  133.       $list = $this->list;
  134.     // Return failure if the list isn't set
  135.     if (!is_array($list) || !count($list))
  136.       return false;
  137.     // Break break break break break some strings
  138.     $charset_parts = str_split($this->charset);
  139.    
  140.     // Do numeric decode
  141.     foreach ($list as $list_char)
  142.       {
  143.       $final .= $charset_parts[$list_char];
  144.       }
  145.    
  146.     // Store the created list in the object.
  147.     $this->list = $final;
  148.    
  149.     // Return the created list, just to be nice.
  150.     return $final;
  151.     }
  152.  
  153.  
  154.   /**
  155.    * ######################################
  156.    * # MaText Data Manipulation Functions #
  157.    * ######################################
  158.    */
  159.  
  160.   /**
  161.    * Encrypt
  162.    * @param array $password The password square matrix
  163.    * @param array $list Numeric representation to encrypt
  164.    * @returns array The encrypted list
  165.    */
  166.   public function encrypt($password = array(1), $list = null)
  167.     {
  168.     // Fall back on pre-set list
  169.     if ($list == null)
  170.       $list = $this->list;
  171.     // Fall back on pre-set text
  172.     if ((!is_array($list) || !count($list)) && $string == null)
  173.       {
  174.       $string = $this->text;
  175.       $list = $this->makeList($string);
  176.       }
  177.     // Return failure if the list isn't set
  178.     if (!is_array($list) || !count($list))
  179.       return false;
  180.    
  181.     // Build password matrix
  182.     $password_matrix = new Matrix($password);
  183.    
  184.     // Check the password
  185.     if (!$password_matrix->checkSquare())
  186.       return false;
  187.    
  188.     // Pad the list to password-compatible size
  189.     $part_size = $password_matrix->getRows();
  190.     $pad_size = count($list) + ($part_size - (count($list) % $part_size));
  191.     $list = array_pad($list, $pad_size, -1);
  192.    
  193.     // OMG, encrypt! :D
  194.     $final = array();
  195.     for ($i = 0; $i < count($list); $i += $part_size)
  196.       {
  197.       $encrypt_proc_part = array(array_slice($list, $i, $part_size));
  198.       // This is the actual encryption right here:
  199.       $encrypted_part = $password_matrix->multiply($encrypt_proc_part, $password);
  200.       // Add the encrypted part to the final array.
  201.       $final = array_merge($final, $encrypted_part[0]);
  202.       }
  203.    
  204.     // Store the created list in the object.
  205.     $this->list = $final;
  206.    
  207.     // Return the created list, just to be nice.
  208.     return $final;
  209.     }
  210.  
  211.   /**
  212.    * Decrypt
  213.    * @param array $password The password square matrix
  214.    * @param array $list Numeric representation to decrypt
  215.    * @returns array The decryupted list
  216.    */
  217.   public function decrypt($password = array(1), $list = null)
  218.     {
  219.     // Fall back on pre-set list
  220.     if ($list == null)
  221.       $list = $this->list;
  222.     // Return failure if the list isn't set
  223.     if (!is_array($list) || !count($list))
  224.       return false;
  225.    
  226.     // Build password matrix
  227.     $password_matrix = new Matrix($password);
  228.    
  229.     // Check the password
  230.     if (!$password_matrix->checkSquare())
  231.       return false;
  232.    
  233.     // Set the encryption part size.
  234.     $part_size = $password_matrix->getRows();
  235.    
  236.     // OMG, decrypt! :D
  237.     $final = array();
  238.     for ($i = 0; $i < count($list); $i += $part_size)
  239.       {
  240.       $decrypt_proc_part = array(array_slice($list, $i, $part_size));
  241.       // This is the actual decryption right here:
  242.       $decrypted_part = $password_matrix->multiply($decrypt_proc_part, $password_matrix->inverse($password));
  243.       // Add the encrypted part to the final array.
  244.       $final = array_merge($final, $decrypted_part[0]);
  245.       }
  246.    
  247.     // Store the created list in the object.
  248.     $this->list = $final;
  249.    
  250.     // Return the created list, just to be nice.
  251.     return $final;
  252.     }
  253.  
  254.   /**
  255.    * ##########################
  256.    * # MaText File Operations #
  257.    * ##########################
  258.    */
  259.  
  260.   /**
  261.    * Generate File
  262.    * @param array $data List to store in the file
  263.    * @param string $accuracy (optional) Format code for the numeric data
  264.    * @returns string Binary string containing the NDF file contents
  265.    */
  266.   public function makeFile($data, $accuracy = "f")
  267.     {
  268.     # MaText stores data in NDF files.
  269.    #  NDF stands for Numeric Data File.
  270.    #   It's just a file format that Deltik made up.
  271.    // Header (identifying characters)
  272.     $final_hex = "FFDEAD";
  273.    
  274.     // Check for valid format code
  275.     if (strlen($accuracy) != 1 || !@pack($accuracy, "10101.10101"))
  276.       return false;
  277.    
  278.     // Add and store format code information
  279.     $final_hex .= $this->strtohex($accuracy);
  280.    
  281.     // Convert the hexadecimal header to binary
  282.     $final = $this->hextostr($final_hex);
  283.    
  284.     // Generate hexadecimal for the data
  285.     foreach ($data as $datum)
  286.       {
  287.       $final .= pack($accuracy, $datum);
  288.       }
  289.    
  290.     // Return the generated file
  291.     return $final;
  292.     }
  293.  
  294.   /**
  295.    * Read File
  296.    * @param string $data NDF File contents
  297.    * @returns array List contents from file
  298.    */
  299.   public function readFile($data)
  300.     {
  301.     # MaText stores data in NDF files.
  302.    #  NDF stands for Numeric Data File.
  303.    #   It's just a file format that Deltik made up.
  304.    // Convert the file contents to hexadecimal
  305.     $data_hex = $this->strtohex($data);
  306.    
  307.     // Check header (identifying characters)
  308.     if (substr($data_hex, 0, 6) != "FFDEAD")
  309.       {
  310.       return false;
  311.       }
  312.     else
  313.       {
  314.       $data_hex_proc = substr($data_hex, 6);
  315.       }
  316.     // Break hexadecimal data process into bytes.
  317.     $data_hex_proc = str_split($data_hex_proc, 2);
  318.     // Save the unpack format code.
  319.     $accuracy_hex = array_shift($data_hex_proc);
  320.     $accuracy = $this->hextostr($accuracy_hex);
  321.     // Reassemble the hexadecimal data process.
  322.     $data_hex_proc = implode("", $data_hex_proc);
  323.     // Convert the hexadecimal data to binary for unpack()ing.
  324.     $data_proc = $this->hextostr($data_hex_proc);
  325.    
  326.     // Determine byte size for this accuracy
  327.     $data_size = strlen(pack($accuracy, "10101.10101"));
  328.    
  329.     // Break binary data process into each individual number.
  330.     $data_proc = str_split($data_proc, $data_size);
  331.    
  332.     // Unpack.
  333.     $final = array();
  334.     foreach ($data_proc as $datum)
  335.       {
  336.       $final = array_merge($final, unpack($accuracy, $datum));
  337.       }
  338.    
  339.     // Return the read file
  340.     return $final;
  341.     }
  342.   }
  343.  
  344. /**
  345.  * ###############################
  346.  * # MaText Original Source Code #
  347.  * ###############################
  348.  */
  349. <<<sourceCodePortedFrom
  350. ::"MaText v2.1.0
  351. :Lbl B
  352. :Full:ClrHome
  353. :Normal:Float
  354. :" ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyzθ0123456789,.?!+-*/^()23-1{}[]eπiorT=≠>≥<≤n□┼∙‾':[xhat][yhat]>F[phat]→Str5
  355. :Menu("MaText v2.1.0   ","Encrypt",E,"Decrypt",D,"About",A,"Character Map",C,"σxit",X
  356. :Lbl A
  357. :Output(1,6,"MaText
  358. :Output(2,6,"v2.1.0
  359. :Output(3,1,"----------------
  360. :Output(4,3,"Row Matrices
  361. :Output(5,4,"Encryption
  362. :Output(6,1,"----------------
  363. :Output(7,2,"Programmed By:
  364. :Output(8,5,"Nick Liu
  365. :Pause
  366. :Goto B
  367. :Lbl C
  368. :Output(1,1,Str5
  369. :Pause :Goto B
  370. :Lbl E
  371. :DelVar TMenu("  Encrypt from  ","text input.",E1,"list input.",E2,"L1.",E3,"...never mind.",B
  372. :Lbl E1:ClrHome
  373. :Input ">",Str0
  374. :If 998<length(Str0:Then
  375. :¦ sub(Str0,1,995)+"...→Str0
  376. :¦ Disp "","WARNING! The","string is too","long. It has
  377. :¦ Pause "been cropped.
  378. :End
  379. :ClrHome
  380. :Goto E0:Lbl E3
  381. :L1→L2:1→T
  382. :Goto E0:Lbl E2
  383. :ClrHome
  384. :Input "List>",L2:1→T
  385. :Lbl E0:ClrHome
  386. :Disp "Enter a 4-number","password","separated by","spaces or commas
  387. :Input ">",Str1
  388. :DelVar L1
  389. :1→θ:1→Y
  390. :Str1+" 0 0 0 0→Str1
  391. :While Y<length(Str1) and θ≤4
  392. :¦ " →Str2
  393. :¦ Repeat sub(Str1,Y,1)=" " or sub(Str1,Y,1)=",
  394. :¦ ¦ Str2+sub(Str1,Y,1→Str2
  395. :¦ ¦ Y+1→Y:End
  396. :¦ Repeat sub(Str1,Y,1)≠" " and sub(Str1,Y,1)≠",
  397. :¦ ¦ Y+1→Y:End
  398. :¦ expr(sub(Str2,2,length(Str2)-1→L1(θ
  399. :¦ ">→Str9
  400. :¦ For(X,1,Y-2
  401. :¦ ¦ Str9+"*→Str9
  402. :¦ End
  403. :¦ Output(5,1,Str9
  404. :¦ θ+1→θ
  405. :End
  406. :DelVar Str1DelVar Str2DelVar Str9
  407. :[[L1(1),L1(2)][L1(3),L1(4)]]→[B]:ClrHome
  408. :If T=1:Goto E9
  409. :If length(Str0)/2≠iPart(length(Str0)/2
  410. :Str0+" →Str0
  411. :DelVar L1length(Str0→dim(L1
  412. :Output(1,1,"Status:
  413. :Output(2,1,"Text►List
  414. :Output(4,4,"
  415. :Output(7,1,"---
  416. :Output(8,1,length(Str0
  417. :length(Str0→dim(L1
  418. :1→θ
  419. :While θ≤length(Str0
  420. :¦ inString(Str5,sub(Str0,θ,1→Y
  421. :¦ Y-1→L1(θ
  422. :¦ If Y=0:66→L1(θ
  423. :¦ θ+1→θ
  424. :¦ Output(6,1,θ-1
  425. :¦ Output(4,1,round((θ-1)/length(Str0)*100,0
  426. :End
  427. :Goto E8
  428. :Lbl E9:L2→L1
  429. :Lbl E8
  430. :ClrHome
  431. :Output(1,1,"Encrypting...  
  432. :Output(4,1,"---
  433. :Output(5,1,dim(L1)/2
  434. :For(θ,1,dim(L1),2
  435. :¦ Output(3,1,(θ-1)/2
  436. :¦ [[L1(θ),L1(θ+1)]]*[B]→[A]
  437. :¦ [A](1,1→L1(θ
  438. :¦ [A](1,2→L1(θ+1
  439. :End
  440. :DelVar [A]DelVar [B]
  441. :Disp " Text Encrypted","----------------"," Encrypted data
  442. :Output(5,1,"<<  [SCROLL]  >>
  443. :Output(7,1,"Press [ENTER] to    continue
  444. :Pause L1:Goto B:Lbl D
  445. :Menu("  Decrypt from  ","L1.",D1,"data entry.",D2,"...never mind.",B
  446. :Lbl D2
  447. :Disp "Enter crypted","data in list","syntax.
  448. :Input ">",Str0
  449. :expr(Str0→L1
  450. :ClrHome
  451. :Lbl D1
  452. :L1→L3
  453. :Disp "Enter 4-number","password for the","data separated","by spaces or ","commas
  454. :Input ">",Str1
  455. :1→θ:1→Y
  456. :Str1+" 0 0 0 0→Str1
  457. :While Y<length(Str1) and θ≤4
  458. :¦ " →Str2
  459. :¦ Repeat sub(Str1,Y,1)=" " or sub(Str1,Y,1)=",
  460. :¦ ¦ Str2+sub(Str1,Y,1→Str2
  461. :¦ ¦ Y+1→Y:End
  462. :¦ Repeat sub(Str1,Y,1)≠" " and sub(Str1,Y,1)≠",
  463. :¦ ¦ Y+1→Y:End
  464. :¦ expr(sub(Str2,2,length(Str2)-1→L2(θ
  465. :¦ ">→Str9
  466. :¦ For(X,1,Y-2
  467. :¦ ¦ Str9+"*→Str9
  468. :¦ End
  469. :¦ Output(6,1,Str9
  470. :¦ θ+1→θ:End
  471. :DelVar Str1DelVar Str2DelVar Str9
  472. :[[L2(1),L2(2)][L2(3),L2(4)]]→[B]:DelVar L2ClrHome
  473. :If dim(L1)/2≠iPart(dim(L1)/2
  474. :0→L1(dim(L1)+1
  475. :Output(1,1,"Decrypting...
  476. :Output(4,1,"---
  477. :Output(5,1,dim(L1)/2
  478. :For(θ,1,dim(L1),2
  479. :¦ Output(3,1,(θ-1)/2
  480. :¦ [[L1(θ),L1(θ+1)]]*[B]-1→[A]
  481. :¦ [A](1,1)+1→L1(θ
  482. :¦ [A](1,2)+1→L1(θ+1
  483. :End
  484. :If max(L1)>length(Str5) or min(L1)<1
  485. :Then:Lbl P
  486. :¦ ClrHome
  487. :¦ Output(3,1,"The password youentered is not  correct.
  488. :¦ Output(7,1,"Press [ENTER] to
  489. :¦ Output(8,5,"continue
  490. :¦ Pause "ERR:PASSWORD
  491. :¦ L3→L1:DelVar L3Goto B:End
  492. :ClrHome
  493. :Output(1,1,"Status:
  494. :Output(2,1,"List►Text
  495. :Output(4,4,"
  496. :Output(7,1,"---
  497. :Output(8,1,dim(L1
  498. :Output(6,6,"+----------
  499. :Output(7,6,"_Processing
  500. :Output(8,6,"_
  501. :">→Str0
  502. :For(θ,1,dim(L1
  503. :¦ Output(4,1,round(θ/dim(L1)*100,0
  504. :¦ Output(6,1,θ
  505. :¦ round(L1(θ),0→L1(θ
  506. :¦ If L1(θ)<1 or L1(θ)>length(Str5
  507. :¦ Goto P
  508. :¦ Str0+sub(Str5,L1(θ),1→Str0
  509. :¦ Output(8,11,sub(Str5,L1(θ),1
  510. :End
  511. :ClrHome
  512. :Disp " Text Decrypted","----------------"," Decrypted Data
  513. :Output(5,1,"<<  [SCROLL]  >>
  514. :Output(7,1,"Press [ENTER] to
  515. :Output(8,5,"continue
  516. :Output(4,4,"LOADING...
  517. :For(θ,1,dim(L1
  518. :¦ L1(θ)-1→L1(θ
  519. :End
  520. :sub(Str0,2,length(Str0)-1→Str0
  521. :Output(4,4,"          
  522. :Pause L1
  523. :Lbl F
  524. :ClrHome
  525. :Menu("","View",V,"Unsave",U,"Return",B,"σxit",X
  526. :Lbl U
  527. :DelVar Str0L3→L1
  528. :Output(1,1,"The decrypted   data has been   deleted and re- placed with the encrypted list.
  529. :Output(7,1,"Press [ENTER] to
  530. :Output(8,5,"continue
  531. :Pause :Goto B
  532. :Lbl V
  533. :"----------------Press [CLEAR] toexit text viewer----------------"+Str0→Str1
  534. :1→θ
  535. :length(Str1→Y
  536. :DelVar XWhile X≠45
  537. :¦ getKey→X
  538. :¦ Output(1,1,sub(Str1,θ,Y-θ+1)+"                
  539. :¦ If X=25:θ-16→θ
  540. :¦ If X=34:θ+16→θ
  541. :¦ If θ>Y
  542. :¦ θ-16→θ
  543. :¦ If θ<1
  544. :¦ 1→θ
  545. :End:Goto F
  546. :Lbl X
  547. :DelVar TDelVar XDelVar YDelVar θDelVar L3DelVar Str0DelVar Str1DelVar Str5DelVar [A]DelVar [B]
  548. :ClrHome
  549. :Output(1,1,"
  550. sourceCodePortedFrom;
  551.  
  552. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement