델파이/PHP 암호화/복호화 소스

from My Library 2008/02/27 14:15 / hits (801)

아래는 델마당(http://delmadang.com) 강좌, 팁 정보 게시판에 박후선 님이 올린 델파이를 이용한 암호화/복호화 소스이다.

unit UEncrypt;

interface

function Encrypt(const S: String; Key: Word): String;
function Decrypt(const S: String; Key: Word): String;

implementation

uses SysUtils;

const
C1 = 74102;
C2 = 12337;
HexaChar : array [0..15] of Char = 
( '0', '1', '2', '3', '4', '5', '6', '7', '8', 
'9','A', 'B', 'C', 'D', 'E', 'F' );
// Byte로 구성된 데이터를 Hexadecimal 문자열로 변환
function ValueToHex(const S : String): String;
var
  I : Integer;
begin
  SetLength(Result, Length(S)*2); // 문자열 크기를 설정
  for I := 0 to Length(S)-1 do
  begin
    Result[(I*2)+1] := HexaChar[Integer(S[I+1]) shr 4];
    Result[(I*2)+2] := HexaChar[Integer(S[I+1]) and $0f];
  end;
end;
// Hexadecimal로 구성된 문자열을 Byte 데이터로 변환
function HexToValue(const S : String) : String;
var
  I : Integer;
begin
  SetLength(Result, Length(S) div 2);
  for I := 0 to (Length(S) div 2) - 1 do
  begin
    Result[I+1] := Char(StrToInt('$'+Copy(S,(I*2)+1, 2)));
  end;
end;
// 암호걸기
function Encrypt(const S: String; Key: Word): String;
var
  I: byte;
  FirstResult : String;
begin
  SetLength(FirstResult, Length(S)); // 문자열의 크기를 설정
  for I := 1 to Length(S) do begin
    FirstResult[I] := char(byte(S[I]) xor (Key shr 8));
    Key := (byte(FirstResult[I]) + Key) * C1 + C2;
  end;
  Result := ValueToHex(FirstResult);
end;
// 암호풀기
function Decrypt(const S: String; Key: Word): String;
var
  I: byte;
  FirstResult : String;
begin
  FirstResult := HexToValue(S);
  SetLength( Result, Length(FirstResult) );
  for I := 1 to Length(FirstResult) do
  begin
    Result[I] := char(byte(FirstResult[I]) xor (Key shr 8));
    Key := (byte(FirstResult[I]) + Key) * C1 + C2;
  end;
end;
end.


위의 소스를 PHP로 변환해 보았다.
Integer Overflow까지 감안해서 암호화/복호화가 이루어졌는데, PHP의 경우 INT형 범위를 초과하게 되면 자동으로 Float 형으로 변환이 되어 버리기 때문에 강제 타입 캐스팅 (int)을 통해 원하는 결과를 얻을 수 있었다.

아래는 그 소스.

<?PHP
/*
   Date   : 2008년 2월 27일
   Author : 김광철 (k2club@hanmail.net)
   사용 예]
    $pid = new ENCDEC();
    $pid->Encrypt(4936);
    => 231F3051D40B78DB37FB0000004936
    $pid->Decrypt($fid);
    => 0000004936
*/ 
  class ENCDEC {
    FUNCTION ENCDEC() {
      // Class Initialization
    }
    FUNCTION ASCII2HEX($ASCII) {
      $len = strlen($ASCII);
      for ($i=0; $i<$len; $i++) 
        $result.=sprintf("%02X",ord(substr($ASCII,$i,1)));
      return $result;
    }
    FUNCTION HEX2ASCII($HEX) {
      $len = strlen($HEX);
      for ($i=0; $i<$len; $i+=2) 
        $result.=sprintf("%s", chr(hexdec(substr($HEX,$i,2))));
      return $result;
    }
    FUNCTION Make_HexaString($prg_id) {
      $FirstResult = '0';
      $Key         = $prg_id;
      $S           = sprintf("%010d", $prg_id);
      for($i = 0; $i <= strlen($S)-1; $i++) {
        $FirstResult[$i] = CHR(ORD($S[$i]) ^ ($Key>>8));
        $Key = (int)ORD($FirstResult[$i]) + (int)$Key;
        $Key = (int)($Key * 74102);
        $Key = (int)($Key + 12337);  
      }
      return $FirstResult;
    }
  
    FUNCTION Encrypt($prg_id) {
      if($prg_id == 0) return '';
      return $this->ASCII2HEX($this->Make_HexaString($prg_id)).sprintf("%010d", $prg_id);
    }
    FUNCTION Make_AsciiString($fid_id) {
      $Result = '0';
      $str = substr($fid_id, 0, strlen($fid_id) - 10);
      $Key = (int)substr($fid_id, strlen($fid_id) - 10);
      $FirstResult = $this->HEX2ASCII($str);
      for($i=0; $i <= strlen($FirstResult)-1; $i++) {
        $Result[$i] = CHR(ORD($FirstResult[$i]) ^ ($Key>>8));
        
        $Key = (int)ORD($FirstResult[$i]) + (int)$Key;
        $Key = (int)($Key * 74102);
        $Key = (int)($Key + 12337);  
      } 
      return $Result;
    }
    FUNCTION Decrypt($fid_id) {
      if($fid_id == '') return '';
      return $this->Make_AsciiString($fid_id);    
    }
  }
  // http://BASE_URL?prg_id=Value
  $pid = new ENCDEC();
  $fid = $pid->Encrypt($_GET[prg_id]);
  echo $fid."<BR><BR>";
  echo $pid->Decrypt($fid);
?>



2008/02/27 14:15 2008/02/27 14:15
포스팅이 유익 하셨다면 RSS 구독을 신청하세요

Trackback Address >> http://dolba.net/tt/k2club/trackback/1724