PDA

Просмотр полной версии : [Помогите!] base64


АнтиАмерика
08.10.2011, 20:32
Как декодить base 64 программно?

VeTaL_UA
08.10.2011, 20:57
Кодирование:
function EncodeBase64(const inStr:string):string;

function Encode_Byte(b:Byte):char;
const
Base64Code:string[64]='ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuv wxyz0123456789+/';
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
begin
Result:=Result+Encode_Byte((Byte(inStr[i+1]) shl 2) or (Byte(inStr[i+2]) shr 6));
end
else
begin
Result:=Result+'=';
end;
if i+2<=Length(inStr) then
begin
Result := Result + Encode_Byte(Byte(inStr[i+2]));
end
else
begin
Result:=Result+'=';
end;
Inc(i,3);
end;
end;
Декодирование:
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
begin
Exit;
Result:=Result+Char((StoredC4[1] shl 4) or (StoredC4[2] shr 2));
end;
if StoredC4[3]=-1 then
begin
Exit;
Result := Result + Char((StoredC4[2] shl 6) or (StoredC4[3]));
end;
end;
end;
end;

АнтиАмерика
08.10.2011, 21:08
Декодирование:

Кодирование:
Перепутал..


___________________________________________



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
begin
Exit;
Result:=Result+Char((StoredC4[1] shl 4) or (StoredC4[2] shr 2));
end;
if StoredC4[3]=-1 then
begin
Exit;
Result := Result + Char((StoredC4[2] shl 6) or (StoredC4[3]));
end;
end;
end;
end;

Плохо декодит :(

VeTaL_UA
08.10.2011, 21:14
Перепутал..
:)
Плохо декодит
Ну что же поделать...

Посмотри этот ([Ссылки могут видеть только зарегистрированные и активированные пользователи]) исходник. Вдруг это лучше будет... ;)

АнтиАмерика
08.10.2011, 21:20
Посмотри этот исходник. Вдруг это лучше будет...
Помогло, спасибо!

lolwut98
10.10.2011, 22:04
ну зачем же делать отдельные функции, если всё это уже есть?

добавьте в uses модуль EncdDecd. и для кодирования/декодирования используйте encodestring/decodestring

VeTaL_UA
11.10.2011, 18:53
ну зачем же делать отдельные функции, если всё это уже есть?

добавьте в uses модуль EncdDecd. и для кодирования/декодирования используйте encodestring/decodestring
Ну я не люблю использовать дополнительные модули, по этому предложил такой вариант... Оба варианта правильны. ;)