Location: PHPKode > scripts > OTP_TS > otp_ts/class.OTP_TS.php
<?php

class OTP_TS {

/*========================*\

 * OTP_TS
 * Written by: AS
 * Mialto: hide@address.com
 * Date: 2007-06-19
 * Algorithm: one-time-pad ts (encryption and decryption) 
 * Version: 1 
 * Licencia: Lesser General Public License (LGPL)   
 *
 * Copyright (C) 2007 Jacek Wloka
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.  

\*========================*/

/* cipher_OTP_TS (string $pass, string $data) */

 function cipher_OTP_TS ($pass="", $data="") {

    $tmp_a = '';
    for ($a=0; $a<strlen($pass); $a+=3) {
         $tmp_a .= md5(md5(md5(substr($pass,$a,3))));                
    }
    $tmpstrlen = strlen($tmp_a);
    for ($tmp_x=$tmpstrlen, $a=0; $tmp_x<strlen($data); $a+=3) {
         $tmp_a .= md5(md5(md5(substr($tmp_a,$a,3))));
         $tmp_x = strlen($tmp_a);                
    }
    $data_pass = $tmp_a;
    $pwd = md5(md5(md5($pass)));
    $key[] = "";
    $box[] = "";
    $temp_swap = "";
    $pwd_length = 0;
    $pwd_length = strlen($pwd);
    for ($i = 0; $i < 255; $i++) {
         $key[$i] = ord(substr($pwd, ($i % $pwd_length)+1, 1));
         $box[$i] = $i;
    }
    $x = 0;
    for ($i = 0; $i < 255; $i++) {
         $x = ($x + $box[$i] + $key[$i]) % 256;
         $temp_swap = $box[$i];
         $box[$i] = $box[$x];
         $box[$x] = $temp_swap;
    }
    $temp = "";
    $k = "";
    $cipherby = "";
    $cipher = "";
    $a = 0;
    $j = 0;
    for ($i = 0; $i < strlen($data_pass); $i++) {
         $a = ($a + 1) % 256;
         $j = ($j + $box[$a]) % 256;
         $temp = $box[$a];
         $box[$a] = $box[$j];
         $box[$j] = $temp;
         $k = $box[(($box[$a] + $box[$j]) % 256)];
         $cipherby = ord(substr($data_pass, $i, 1)) ^ $k;
         $cipher .= chr($cipherby);
    }
    
    return ($data ^ $cipher);
 
 }

}

?>
Return current item: OTP_TS