home *** CD-ROM | disk | FTP | other *** search
/ PC Professionell 2004 December / PCpro_2004_12.ISO / files / webserver / tsw / TSW_3.4.0.exe / Apache2 / admin / encryption.inc < prev    next >
Encoding:
Text File  |  2003-07-06  |  2.8 KB  |  114 lines

  1. <?php
  2. /////////////////////////////////////////////////////////
  3. //    
  4. //    include/encryption.inc
  5. //
  6. //    (C)Copyright 2000-2002 Ryo Chijiiwa <Ryo@IlohaMail.org>
  7. //
  8. //    This file is part of IlohaMail. IlohaMail is free software released 
  9. //    under the GPL license.  See enclosed file COPYING for details, or 
  10. //    see http://www.fsf.org/copyleft/gpl.html
  11. //
  12. /////////////////////////////////////////////////////////
  13.  
  14. /********************************************************
  15.  
  16.     PURPOSE:
  17.         Provide basic encryption related functionality.
  18.     COMMENTS:
  19.         This library commits the worst crime in cryptography: 
  20.         Never write your own crypto algos.
  21.  
  22. ********************************************************/
  23.  
  24. function GenerateRandomString($messLen, $seed){
  25.     srand ((double) microtime() * 1000000);
  26.     if (empty($seed)) $seed="abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890";
  27.     $seedLen=strlen($seed);
  28.     if ($messLen==0) $messLen = rand(10, 20);
  29.     for ($i=0;$i<$messLen;$i++){
  30.         $point=rand(0, $seedLen-1);
  31.         $message.=$seed[$point];
  32.     }
  33.     return $message;
  34. }
  35.  
  36. function GenerateMessage($messLen){
  37.     $seed="abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890";
  38.     return GenerateRandomString($messLen, $seed);
  39. }
  40.  
  41. function EncryptMessage($key,$message){
  42.     $messLen=strlen($message);
  43.     $keylen=strlen($key);
  44.     $enc_message="";
  45.     
  46.     for ($i=0;$i<$messLen;$i++){
  47.         $j=$i % $keylen;
  48.         $code=chr((ord($message[$i]) + ord($key[$j])) % 128);
  49.         $enc_message.=$code;
  50.     }
  51.  
  52.     return base64_encode($enc_message);
  53. }
  54.  
  55. function DecodeMessage($pass, $message){
  56.     $message=base64_decode($message);
  57.     $messLen=strlen($message);
  58.     $passLen=strlen($pass);
  59.     
  60.     $decMessage="";
  61.     for ($i=0;$i<$messLen;$i++){
  62.         $j=$i % $passLen;
  63.         $num=ord($message[$i]);
  64.         $decNum=(($num + 128) - ord($pass[$j])) % 128;
  65.         $decMessage.=chr($decNum);
  66.     }
  67.     
  68.     return $decMessage;
  69. }
  70.  
  71.  
  72. function GenerateKeyFromIP(){
  73.     $ip = $_SERVER["REMOTE_ADDR"];
  74.     $ipkey="";
  75.     $ip_a = explode(".", $ip);
  76.     for ($i=3; $i>=0; $i--) $ipkey.=$ip_a[$i];
  77.     return $ipkey;
  78. }
  79.  
  80.  
  81. function GetSessionEncKey($sid){
  82.     global $MAX_SESSION_TIME, $STAY_LOGGED_IN;
  83.     $cookie_name = "IMAIL_SESS_KEY_".$sid;
  84.     if (empty($_COOKIE[$cookie_name])){
  85.         // No cookies, turn IP into encryption key
  86.         $ipkey = GenerateKeyFromIP();        
  87.     }else{
  88.         // use cookie
  89.         $ipkey = $_COOKIE[$cookie_name];
  90.         if ($STAY_LOGGED_IN){
  91.             setcookie ($cookie_name, $ipkey, time()+$MAX_SESSION_TIME, "/", $_SERVER[SERVER_NAME]);
  92.         }
  93.     }
  94.     return $ipkey;
  95. }
  96.  
  97.  
  98. function InitSessionEncKey($sid){
  99.     global $MAX_SESSION_TIME;
  100.     
  101.     if (empty($_COOKIE['IMAIL_TEST_COOKIE'])){
  102.         //cookies disabled
  103.         $key = GenerateKeyFromIP();
  104.     }else{
  105.         //cookies enabled
  106.         $cookie_name = "IMAIL_SESS_KEY_".$sid;
  107.         $key = GenerateRandomString(16, "");
  108.         $_COOKIE[$cookie_name] = $key;
  109.         setcookie ($cookie_name, $key, time()+$MAX_SESSION_TIME, "/", $_SERVER[SERVER_NAME]);
  110.     }
  111.     return $key;
  112. }
  113.  
  114. ?>