Witam, mam problem potrzebuję zrobić kodowanie base64
Zrobiłem, w php
Kod:
$fp = fopen('slowa.txt', "a");
$noweDane = str_replace('=', '', base64_encode($_POST['haslo']).'');
fwrite($fp, $noweDane);
fclose($fp);
I w delphi:
Kod:
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, Base64, StdCtrls;
type
TForm1 = class(TForm)
Memo1: TMemo;
Memo2: TMemo;
Button1: TButton;
Button2: TButton;
Label1: TLabel;
Label2: TLabel;
Label3: TLabel;
procedure Button2Click(Sender: TObject);
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
procedure TForm1.Button1Click(Sender: TObject);
begin
memo1.Text := DecodeBase64(memo2.Text);
end;
end.
I biblioteka base64 delphi,
Kod:
unit base64;
interface
function EncodeBase64(const inStr: string): string;
function DecodeBase64(const CinLine: string): string;
implementation
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;
end.
Skrypt php zapisuje ciąg słów
np za pierwszym razem wprowadzamy
test następnie asd następnie pies następnie kot
i otrzymujmy zakodowane
testasdpieskot
przez php: dGVzdAYXNkcGllcwa290
Co po zdekodowaniu przez delphi daje:
test6G–W0kot
czyli jakieś krzaki,
A gdy w programie delphi zakoduję testasdpieskot
to mam: dGVzdGFzZHBpZXNrb3Q=
i gdy zdekoduje wychodzi prawidłowo:
testasdpieskot
Próbowałem kilku dekoderów base64 do delphi tylko ten w ogóle działa, ale problem chyba leży po stronie php,Zanim dodałem str_replace('=', '',
To program php niby robił to dobrze ale co wpisane słowo dawał == i program w delphi kończył pracę na pierwszym słowie.
Kod:
$noweDane=base64_encode($_POST['slowo']).'';
To zapisane słowa mają formę testasdpieskot
php: dGVzdA==YXNkcGllcw==a290
I to delphi tłumaczy tylko jako test a nie cały ciąg,
a gdy skasują = to wychodzą krzaki,
A gdy zakoduję delphim testasdpieskot
to mam dGVzdGFzZHBpZXNrb3Q=
I to dobrze sie dekoduje w delphi,
I dobrze sie dekoduje w php
http://webnet77.com/cgi-bin/helpers/base-64.pl
Wiec ewidentnie coś jest nie tak z tym zapisywaniem w php,
Słowo wpisywane jest na stronie do formularza i następnie jest kodowane i dopisywane do notatnika i sęk w tym że słowo jest kodowane i kolejne słowo jest kodowane nie zależnie i tylko doklejane do ciągu słów, a gdy program na komputerze dekoduje, to dekoduje jednym ciągiem.Teraz zrozumiałeś?
forma zapisu po średniku nie działa(krzaki po zdekodowaniu), forma zapisu kolumną też się nie sprawdza gdyż dekodowanie kończy się na pierwszym haśle.
Proszę o szybka pomoc, z góry, dzięki!