'php'에 해당되는 글 4건
- 2008/08/13
델파이와 PHP 간의 암호화를 위해 작성하였음.
암호키를 XOR 연산하여 값을 구해줌
바이너리 값으로 출력하지 않고 텍스트 출력을 위해 16진수로 표시함
- PHP 소스 -
<?
function calcxor($str,$key)
{
$str_max=strlen($str);
$key_max=strlen($key);
$tmp='';
for($i=0;$i<$str_max;$i++)
$tmp.=$str{$i}^$key{$i%$key_max};
return $tmp;
}
function with_encode($str,$key)
{
$tmp=calcxor($str,$key);
$tmp_max=strlen($tmp);
$enc='';
for($i=0;$i<$tmp_max;$i++)
$enc.=sprintf('%02x',ord($tmp{$i}));
return $enc;
}
function with_decode($str,$key)
{
$tmp='';
for($i=0;$i<strlen($str);$i+=2)
$tmp.=chr(hexdec($str{$i}.$str{$i+1}));
return calcxor($tmp,$key);
}
// 사용예제
$message = "돼지코";
$key = "여기에 원하는 암호키를 입력하시와요";
$x=with_encode($message,$key);
$y=with_decode($x,$key);
echo " <br>원본 : ".$message;
echo " <br>암호키 : ".$key;
echo " <br>암호 : ".$x;
echo " <br>복구 : ".$y;
?>
- Delphi 소스 -
const
HexaChar : array [0..15] of Char =
( '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
'A', 'B', 'C', 'D', 'E', 'F' );
MyKey = '여기에 원하는 암호키를 입력하시와요';
function CalcXor(const str:string;const key:string):string;
var
str_max, key_max, Loop : integer;
tmp : string;
begin
str_max := length( str );
key_max := length( key );
tmp := '';
for Loop := 1 to str_max do
tmp := tmp + char(byte(str[Loop]) xor byte(key[Loop mod key_max]));
result := tmp;
end;
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;
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 With_Encode(const str:string;const key:string):string;
begin
result := ValueToHex( CalcXor(str,key) );
end;
function With_Decode(const str:string;const key:string):string;
begin
result := CalcXor(HexToValue( str ) , key);
end;