Форум: "Основная";
Текущий архив: 2004.07.04;
Скачать: [xml.tar.bz2];
ВнизКодирование .. Найти похожие ветки
← →
Alek © (2004-06-18 13:56) [0]Можно ли из этих двух функций:
function EncodeBase64(const inStr: string): string;
function Encode_Byte(b: Byte): char;
const
Base64Code: string[64] =
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
begin
Result := Base64Code[(b and $3F)+1];
end;
var
i: Integer;
begin
i := 1;
Result := "";
while i < =Length(InStr) do
begin
Result := Result + Encode_Byte(Byte(inStr[i]) shr 2);
Result := Result + Encode_Byte((Byte(inStr[i]) shl 4) or (Byte(inStr[i+1]) shr 4));
if i+1 < =Length(inStr) then
Result := Result + Encode_Byte((Byte(inStr[i+1]) shl 2) or (Byte(inStr[i+2]) shr 6))
else
Result := Result + "=";
if i+2 < =Length(inStr) then
Result := Result + Encode_Byte(Byte(inStr[i+2]))
else
Result := Result + "=";
Inc(i, 3);
end;
end;
// Base64 decoding
function DecodeBase64(const CinLine: string): string;
const
RESULT_ERROR = -2;
var
inLineIndex: Integer;
c: Char;
x: SmallInt;
c4: Word;
StoredC4: array[0..3] of SmallInt;
InLineLength: Integer;
begin
Result := "";
inLineIndex := 1;
c4 := 0;
InLineLength := Length(CinLine);
while inLineIndex < =InLineLength do
begin
while (inLineIndex < =InLineLength) and (c4 < 4) do
begin
c := CinLine[inLineIndex];
case c of
"+" : x := 62;
"/" : x := 63;
"0".."9": x := Ord(c) - (Ord("0")-52);
"=" : x := -1;
"A".."Z": x := Ord(c) - Ord("A");
"a".."z": x := Ord(c) - (Ord("a")-26);
else
x := RESULT_ERROR;
end;
if x < > RESULT_ERROR then
begin
StoredC4[c4] := x;
Inc(c4);
end;
Inc(inLineIndex);
end;
if c4 = 4 then
begin
c4 := 0;
Result := Result + Char((StoredC4[0] shl 2) or (StoredC4[1] shr 4));
if StoredC4[2] = -1 then Exit;
Result := Result + Char((StoredC4[1] shl 4) or (StoredC4[2] shr 2));
if StoredC4[3] = -1 then Exit;
Result := Result + Char((StoredC4[2] shl 6) or (StoredC4[3]));
end;
end;
end;
сделать так, чтобы при кодировании не использовались символы + и /.
Уменя что-то не выходит.
← →
Anatoly Podgoretsky © (2004-06-18 14:21) [1]Замени здесь два последних на нужные тебе символы
ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/
← →
ПсихЪ © (2004-06-18 14:23) [2]Думается, что это будет выглядеть примерно так:
function EncodeBase64(const inStr: string): string;
function Encode_Byte(b: Byte): char;
const
Base64Code: string[64] =
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-/";
begin
Result := Base64Code[(b and $3F)+1];
end;
var
i: Integer;
bb: char;
begin
i := 1;
Result := "";
while i < =Length(InStr) do
begin
bb := chr( inStr[i+1]) shr 4 );
if bb = "+" then bb := "-";
Result := Result + Encode_Byte(Byte(inStr[i]) shr 2);
Result := Result + Encode_Byte((Byte(inStr[i]) shl 4) or (Byte(ord(bb))); // возможно придётся заменить на ord(bb)+1
if i+1 < =Length(inStr) then
begin
bb := chr( inStr[i+2]) shr 6 );
if bb = "+" then bb := "-";
Result := Result + Encode_Byte((Byte(inStr[i+1]) shl 2) or (Byte(ord(bb))) // возможно придётся заменить на ord(bb)+1
end
else
Result := Result + "=";
if i+2 < =Length(inStr) then
Result := Result + Encode_Byte(Byte(inStr[i+2]))
else
Result := Result + "=";
Inc(i, 3);
end;
end;
// Base64 decoding
function DecodeBase64(const CinLine: string): string;
const
RESULT_ERROR = -2;
var
inLineIndex: Integer;
c: Char;
x: SmallInt;
c4: Word;
StoredC4: array[0..3] of SmallInt;
InLineLength: Integer;
begin
Result := "";
inLineIndex := 1;
c4 := 0;
InLineLength := Length(CinLine);
while inLineIndex < =InLineLength do
begin
while (inLineIndex < =InLineLength) and (c4 < 4) do
begin
c := CinLine[inLineIndex];
case c of
"-" : x := 62; // возможно придётся заменить на 61
"/" : x := 63;
"0".."9": x := Ord(c) - (Ord("0")-52);
"=" : x := -1;
"A".."Z": x := Ord(c) - Ord("A");
"a".."z": x := Ord(c) - (Ord("a")-26);
else
x := RESULT_ERROR;
end;
if x < > RESULT_ERROR then
begin
StoredC4[c4] := x;
Inc(c4);
end;
Inc(inLineIndex);
end;
// вот тут нужно подумать... потестить...
if c4 = 4 then
begin
c4 := 0;
Result := Result + Char((StoredC4[0] shl 2) or (StoredC4[1] shr 4));
if StoredC4[2] = -1 then Exit;
Result := Result + Char((StoredC4[1] shl 4) or (StoredC4[2] shr 2));
if StoredC4[3] = -1 then Exit;
Result := Result + Char((StoredC4[2] shl 6) or (StoredC4[3]));
end;
end;
end;
А вообще, можно сделать так:
// Функции не трогаешь, а пишешь свои, которые будут их юзать и обрабатывать результат
function ReplaceSub( str, sub1, sub2 : string ); // реализацию смотри в DelphiWorld
// EncodeBase64
function EncB64( const inStr: string ) : string;
begin
Result := ReplaceSub( EncodeBase64( inStr ), "+", "-" );
end;
// DecodeBase64
function DecB64( const inStr: string ) : string;
begin
inStr := ReplaceSub( inStr, "-", "+" );
Result := DecodeBase64( inStr );
end;
← →
VMcL © (2004-06-18 14:31) [3]>>Alek © (18.06.04 13:56)
>Можно ли
Можно, но это уже будет BASE62.
← →
Alek © (2004-06-18 15:02) [4]а как закодировать в базе62??
есть ли уже готовые функции?
а то мне нужно коджировать строки и передавать их по хттп, и так чтобы символы не менялись на другие например пробел на %20
← →
Fay © (2004-06-18 15:22) [5]Nut 8)
← →
ПсихЪ © (2004-06-18 15:25) [6]Так тебе нужен алгоритм шифрования строк или именно бинарных данных? Чем тебе не нравится предложенный мной вариант?
← →
Alek © (2004-06-18 17:13) [7]
> ПсихЪ
спасибо за помощь, пока твоим способом и сделал, но приходится менять + и / на другие значения например + я меняю на %F, а / на %B и из-за этого увеличивается строка :(.
Желательно сделать вообще чтобы кодировалось только вот этим вот - ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789
← →
Anatoly Podgoretsky © (2004-06-18 17:29) [8]Это простые математические операции целочисленного деления и получения остатка, вроде первый класс школы, если не ошибаюсь.
Страницы: 1 вся ветка
Форум: "Основная";
Текущий архив: 2004.07.04;
Скачать: [xml.tar.bz2];
Память: 0.48 MB
Время: 0.032 c