스트링과 3개의 키값을 매개변수로 받아 암호화, 복호화 하는 함수.
PHP의 Integer 가 단위가 작아서 키값을 작게 설정하려다가 Key2 를 곱하기 연산을 하는 부분을 xor 로 대체했음.
Delphi 에서만 사용하려면 'xor Key2' 부분을 곱하기로 바꿔도 좋다.
PHP와 Delphi 간에 암호화<->복호화를 위해선 키값을 동일하게 선언해야 한다.
아니면 키값 자체를 통신으로 주고 받거나.
처음엔 상수값이 한자리 수로 되어 있지만 수만자리씩 되어야 암호화가 된다...
Delphi
const
C_KEY1 = 1;
C_KEY2 = 2;
C_KEY3 = 3;
// 암호화
// ________________________________________________________________________________________________
function Encrypt(const S: String; Key1, Key2, Key3: DWORD): String;
var
i: Integer;
sEncrypt, rEncrypt: String;
rChar: Char;
begin
if Length(S)<1 then begin
Result:='';
System.Exit;
end;
// 암호화
sEncrypt:='';
for i:=1 to Length(S) do begin
sEncrypt:=sEncrypt+Char(Byte(S[i]) xor (Key1 shr 8));
Key1:=(Byte(sEncrypt[i])+Key1) xor Key2+Key3;
end;
// 3자리 ASCII 숫자로 바꿈
rEncrypt:='';
for i:=1 to Length(sEncrypt) do begin
rChar:=sEncrypt[i];
rEncrypt:=rEncrypt+Format('%.3d', [Ord(rChar)]); // 한 문자당 3자리씩
end;
Result:=rEncrypt;
end;
// 복호화
// ________________________________________________________________________________________________
function Decrypt(const S: String; Key1, Key2, Key3: DWORD): String;
var
i: Integer;
sDecrypt, rDecrypt, Temp: String;
begin
if Length(S)<1 then begin
Result:='';
System.Exit;
end;
// 3자리로 끊어진 ASCII 숫자에서 문자 추출
rDecrypt:='';
i:=1;
repeat
Temp:=Copy(S, i, 3); // 한 문자당 3자리 숫자
rDecrypt:=rDecrypt+Chr(StrToIntDef(Temp, 0)); // ASCII 값
Inc(i, 3);
until i>Length(S);
// 추출된 문자의 복호화
sDecrypt:='';
for i:=1 to Length(rDecrypt) do begin
sDecrypt:=sDecrypt+Char(Byte(rDecrypt[i]) xor (Key1 shr 8));
Key1:=(Byte(rDecrypt[i])+Key1) xor Key2+Key3;
end;
Result:=sDecrypt;
end;
C_KEY1 = 1;
C_KEY2 = 2;
C_KEY3 = 3;
// 암호화
// ________________________________________________________________________________________________
function Encrypt(const S: String; Key1, Key2, Key3: DWORD): String;
var
i: Integer;
sEncrypt, rEncrypt: String;
rChar: Char;
begin
if Length(S)<1 then begin
Result:='';
System.Exit;
end;
// 암호화
sEncrypt:='';
for i:=1 to Length(S) do begin
sEncrypt:=sEncrypt+Char(Byte(S[i]) xor (Key1 shr 8));
Key1:=(Byte(sEncrypt[i])+Key1) xor Key2+Key3;
end;
// 3자리 ASCII 숫자로 바꿈
rEncrypt:='';
for i:=1 to Length(sEncrypt) do begin
rChar:=sEncrypt[i];
rEncrypt:=rEncrypt+Format('%.3d', [Ord(rChar)]); // 한 문자당 3자리씩
end;
Result:=rEncrypt;
end;
// 복호화
// ________________________________________________________________________________________________
function Decrypt(const S: String; Key1, Key2, Key3: DWORD): String;
var
i: Integer;
sDecrypt, rDecrypt, Temp: String;
begin
if Length(S)<1 then begin
Result:='';
System.Exit;
end;
// 3자리로 끊어진 ASCII 숫자에서 문자 추출
rDecrypt:='';
i:=1;
repeat
Temp:=Copy(S, i, 3); // 한 문자당 3자리 숫자
rDecrypt:=rDecrypt+Chr(StrToIntDef(Temp, 0)); // ASCII 값
Inc(i, 3);
until i>Length(S);
// 추출된 문자의 복호화
sDecrypt:='';
for i:=1 to Length(rDecrypt) do begin
sDecrypt:=sDecrypt+Char(Byte(rDecrypt[i]) xor (Key1 shr 8));
Key1:=(Byte(rDecrypt[i])+Key1) xor Key2+Key3;
end;
Result:=sDecrypt;
end;
PHP
$C_KEY1 = 7;
$C_KEY2 = 6;
$C_KEY3 = 1;
// 암호화
// _________________________________________________________________________________________________
function Encrypt($s, $Key1, $Key2, $Key3)
{
if (strlen(trim($s))<1) return '';
$sEncrypt = '';
for ($i=0; $i<strlen($s); $i++) {
$sEncrypt .= chr(ord($s[$i]) ^ ($Key1 >> 8));
$Key1 = (int)((ord($sEncrypt[$i])+$key1)^$Key2)+$Key3;
}
$rEncrypt = '';
for ($i=0; $i<strlen($sEncrypt); $i++) {
$rChar = $sEncrypt[$i];
$rEncrypt .= sprintf('%03d', ord($rChar));
}
return $rEncrypt;
}
// 복호화
// _________________________________________________________________________________________________
function Decrypt($s, $Key1, $Key2, $Key3)
{
if (strlen(trim($s))<1) return '';
$rDecrypt = '';
$i=0;
do {
$Temp = $s[$i].$s[$i+1].$s[$i+2];
$rDecrypt .= chr(sprintf('%03d', $Temp));
$i+=3;
} while ($i<strlen($s));
$sDecrypt = '';
for ($i=0; $i<strlen($rDecrypt); $i++) {
$sDecrypt .= chr(ord($rDecrypt[$i]) ^ ($Key1 >> 8));
$Key1 = (int)((ord($rDecrypt[$i])+$Key1) ^ $Key2)+$Key3;
}
return $sDecrypt;
}
$C_KEY2 = 6;
$C_KEY3 = 1;
// 암호화
// _________________________________________________________________________________________________
function Encrypt($s, $Key1, $Key2, $Key3)
{
if (strlen(trim($s))<1) return '';
$sEncrypt = '';
for ($i=0; $i<strlen($s); $i++) {
$sEncrypt .= chr(ord($s[$i]) ^ ($Key1 >> 8));
$Key1 = (int)((ord($sEncrypt[$i])+$key1)^$Key2)+$Key3;
}
$rEncrypt = '';
for ($i=0; $i<strlen($sEncrypt); $i++) {
$rChar = $sEncrypt[$i];
$rEncrypt .= sprintf('%03d', ord($rChar));
}
return $rEncrypt;
}
// 복호화
// _________________________________________________________________________________________________
function Decrypt($s, $Key1, $Key2, $Key3)
{
if (strlen(trim($s))<1) return '';
$rDecrypt = '';
$i=0;
do {
$Temp = $s[$i].$s[$i+1].$s[$i+2];
$rDecrypt .= chr(sprintf('%03d', $Temp));
$i+=3;
} while ($i<strlen($s));
$sDecrypt = '';
for ($i=0; $i<strlen($rDecrypt); $i++) {
$sDecrypt .= chr(ord($rDecrypt[$i]) ^ ($Key1 >> 8));
$Key1 = (int)((ord($rDecrypt[$i])+$Key1) ^ $Key2)+$Key3;
}
return $sDecrypt;
}
'Delphi' 카테고리의 다른 글
[Delphi] 클립보드 HTML Format 제어 (0) | 2009.06.20 |
---|---|
Delphi2009 + IdHTTP (Indy10) 에서 UTF-8 웹페이지 한글 깨짐 (0) | 2009.05.20 |
캡션없는 윈도우 마우스 드래그로 움직이기 (0) | 2009.04.30 |
Delphi2009 Indy10 에서 한글 깨짐 문제 (0) | 2009.04.28 |
TList 형에서 Delete 를 이용해 item 삭제시 루프문 (0) | 2009.04.20 |