Advertisement
gitlez

YA: Simple Lowercase String Offsetter

May 24th, 2012
46
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.44 KB | None | 0 0
  1. <?php
  2. /*    Response to Yahoo Answers Question    */
  3.  
  4.  
  5. /*    Your's Cleaned Up and Working    */
  6. $alphaex = range('a','z');
  7.  
  8. $inputwordex = range('a','e');
  9. echo '<pre>';
  10. print_r($inputwordex);
  11. echo '</pre>';
  12.  
  13. $iword = 0;
  14. $decodeword = "";
  15. while ($iword < 5){
  16.     $ialpha = 0;
  17.     $found = "N";
  18.     while ($found == "N"){
  19.         if ($inputwordex[$iword] == $alphaex[$ialpha]) {
  20.             $i = $ialpha + 24;
  21.             $i = $i % count($alphaex); // Remainder of $i / count($alphaex) or $i / 26; Remeber that a is = 0 and z is 25; Array Indexes start at 0 not 1;
  22.             $decodeword .= $alphaex[$i];
  23.             echo "<br>" . $decodeword;
  24.             $found = "Y";
  25.         }else{
  26.             $ialpha += 1;
  27.         }
  28.     }
  29.     $iword += 1;
  30. }
  31. echo "<br>";
  32. echo $decodeword;
  33.  
  34.  
  35.  
  36. /*    Using Functions to offset a string. Only Does LowerCase    */
  37. function offset_encode($str, $offset=24){
  38.     $alphaex = range('a','z');
  39.     $r = '';
  40.     for($i=0; $i < strlen($str); ++$i){
  41.         $num = (array_search($str{$i}, $alphaex) + $offset) % count($alphaex);
  42.         $r .= $alphaex[$num];        
  43.     }
  44.     return $r;
  45. }
  46. function offset_decode($str, $offset=24){
  47.     $alphaex = range('a','z');
  48.     $r = '';
  49.     for($i=0; $i < strlen($str); ++$i){
  50.         $num = (array_search($str{$i}, $alphaex) - $offset);
  51.         if($num < 0){
  52.             $num += count($alphaex);
  53.         }
  54.         $r .= $alphaex[$num];        
  55.     }
  56.     return $r;
  57. }
  58. $offset = 24;
  59. $str = 'abcde';
  60. $encoded = offset_encode($str, $offset);
  61. $decoded = offset_decode($encoded, $offset);
  62.  
  63. echo 'String: ' . $str . '<br>' . 'Encoded: ' . $encoded . '<br>' . 'Decoded: ' . $decoded . '<br>';
  64.  
  65.  
  66. /*    String Encoding and Decoding    */
  67. echo '<h3>str_encode() and str_decode()</h3>';
  68. defined('STR_SALT')? null : define('STR_SALT','Dwnp=MduTHp0Bn4x>u0p:hf=*[smx5T+ieL9Zr-.Wc4Xc{r0rjjT[Qhv,v{$)sGl');
  69.  
  70. function str_encode($i){
  71.     return base64_encode(mcrypt_encrypt(MCRYPT_RIJNDAEL_256, md5(STR_SALT), ($i . sha1(STR_SALT) . STR_SALT) , MCRYPT_MODE_CBC, md5(md5(STR_SALT))));
  72. }
  73. function str_decode($i){
  74.     return str_replace(Array(sha1(STR_SALT),STR_SALT),'', rtrim(mcrypt_decrypt(MCRYPT_RIJNDAEL_256, md5(STR_SALT), base64_decode($i), MCRYPT_MODE_CBC, md5(md5(STR_SALT))), "\0"));
  75. }
  76. $str = 'abcde';
  77. $encoded = str_encode($str);
  78. $decoded = str_decode($encoded);
  79. echo 'String: ' . $str . '<br>' . 'Encoded: ' . $encoded . '<br>' . 'Decoded: ' . $decoded . '<br>';
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement