마음의 안정을 찾기 위하여 - 델파이/PHP 암호화/복호화 소스
2279391
1147
952
관리자새글쓰기
태그위치로그방명록
별일없다의 생각
dawnsea's me2day/2010
색상(RGB)코드 추출기(Color...
Connection Generator/2010
최승호PD, '4대강 거짓말 검...
Green Monkey**/2010
Syng의 생각
syng's me2DAY/2010
천재 작곡가 윤일상이 기획,...
엘븐킹's Digital Factory/2010
델파이/PHP 암호화/복호화 소스
My Library | 2008/02/27 14:15

아래는 델마당(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
Article tag list Go to top
View Comment 0
Trackback URL :: 이 글에는 트랙백을 보낼 수 없습니다
 
 
 
 
: [1] ... [623][624][625][626][627][628][629][630][631] ... [1317] :
«   2024/04   »
  1 2 3 4 5 6
7 8 9 10 11 12 13
14 15 16 17 18 19 20
21 22 23 24 25 26 27
28 29 30        
전체 (1317)
출판 준비 (0)
My-Pro... (41)
사는 ... (933)
블로그... (22)
My Lib... (32)
게임 ... (23)
개발관... (3)
Smart ... (1)
Delphi (93)
C Builder (0)
Object... (0)
VC, MF... (10)
Window... (1)
Open API (3)
Visual... (0)
Java, JSP (2)
ASP.NET (0)
PHP (5)
Database (12)
리눅스 (29)
Windows (25)
Device... (1)
Embedded (1)
게임 ... (0)
Web Se... (2)
Web, S... (21)
잡다한... (6)
프로젝트 (0)
Personal (0)
대통령... (13)
Link (2)