-1

作成したカスタム関数を使用してデータを暗号化しようとしています...(base64に基づく)コードは機能しますが、ある程度...ランダムな結果が得られます(機能する場合と機能しない場合があります)。

<?php
set_time_limit(0);
class encryptor{
 function encrypt($data){
  $all = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789!?@,.&*()$; ";
  $chars = str_split($all, 1); // Split $all to array of single characters
  $text_chars = str_split($data, 1); // Split $data to array of single characters

  // Create array of unique results based on the characters from $all
  foreach($chars as $char){
   $array[$char] = md5(uniqid(rand(), true));
  }
  // Replace the input text with the results from $array 
  foreach($text_chars as $text_char){
   $data = str_replace($text_char,$array[$text_char], $data);
  }
  // Encode and compress $array as $solution
  $solution = gzcompress(base64_encode(json_encode($array)),9);
  // Return the encoded solution + Breaker + encoded and compressed input text
  return $solution."BREAKHERE".gzcompress(base64_encode($data),9);
 }
 function decrypt($data){
  // Break the encrypted code to two parts
  $exploded = explode('BREAKHERE', $data);
  // Decoding the contents
  $contents = base64_decode(gzuncompress($exploded[1]));
  // Decoding solution ($array)
  $solves = json_decode(base64_decode(gzuncompress($exploded[0])),true);
  $fliped = array_flip($solves);
  // Replace the encrypted data with the solution
  foreach($solves as $solve){
   $contents = str_replace($solve,$fliped[$solve], $contents);
  }
  return($contents); // Return decoded data
  }
 }

$text = "11 1";
$enc = new encryptor();
$encrypted = $enc->encrypt($text);
$decrypted = $enc->decrypt($encrypted);
echo $decrypted;
?>
4

1 に答える 1

1

それはただの楽しみのためなので、この変更はそれを機能させるように見えます:

// Replace the input text with the results from $array
$encrypted = '';
foreach($text_chars as $text_char){
    //$data = str_replace($text_char,$array[$text_char], $data);
     $encrypted .= $array[$text_char];
}

ループ内で実行str_replaceすると、以前に置換されたデータが置換されます。

それとは別に、暗号化されたデータの転送を簡単にするために、私は変更します:

return $solution."BREAKHERE".gzcompress(base64_encode($data),9);

に:

return base64_encode($solution."BREAKHERE".gzcompress($data,9);

次に、復号化機能で適切な変更を加えます。圧縮データにはヌル文字と印刷不可能な文字を含めることができるため、結果全体をbase64エンコードすると、より簡単にデータを渡すことができます。

于 2012-09-19T18:22:33.197 に答える