Vamos a ver que podemos hacer
Para realizar la codificación utilizaremos las librerías criptográficas que vienen con windows y que nos ahorraran mucho trabajo. Incluye la siguiente unit en tu proyecto:
base64.pas
Código Delphi
[-]
unit base64;
interface
uses Windows, SysUtils, Classes;
function BinToStr(Binary: PByte; Len: Cardinal): String;
procedure StrToStream(Str: String; Stream: TStream);
implementation
const
CRYPT_STRING_BASE64 = 1;
function CryptBinaryToString(pbBinary: PByte; cbBinary: DWORD; dwFlags: DWORD;
pszString: PChar; var pcchString: DWORD): BOOL; stdcall;
external 'Crypt32.dll' name 'CryptBinaryToStringA';
function CryptStringToBinary(pszString: PChar; cchString: DWORD; dwFlags: DWORD;
pbBinary: PByte; var pcbBinary: DWORD; pdwSkip: PDWORD;
pdwFlags: PDWORD): BOOL; stdcall;
external 'Crypt32.dll' name 'CryptStringToBinaryA';
function BinToStr(Binary: PByte; Len: Cardinal): String;
var
Count: DWORD;
begin
Count:= 0;
if CryptBinaryToString(Binary,Len,CRYPT_STRING_BASE64,nil,Count) then
begin
SetLength(Result,Count);
if not CryptBinaryToString(Binary,Len,CRYPT_STRING_BASE64,PChar(Result),
Count) then
Result:= EmptyStr;
end;
end;
procedure StrToStream(Str: String; Stream: TStream);
var
Buffer: PByte;
Count: DWORD;
begin
Count:= 0;
if CryptStringToBinary(PChar(Str),Length(Str),CRYPT_STRING_BASE64,nil,Count,
nil,nil) then
begin
GetMem(Buffer,Count);
try
if CryptStringToBinary(PChar(Str),Length(Str),CRYPT_STRING_BASE64,Buffer,
Count,nil,nil) then
Stream.WriteBuffer(Buffer^,Count);
finally
FreeMem(Buffer);
end;
end;
end;
end.
Ahora para codificar un archivo en una cadena de texto:
Código Delphi
[-]
var
Stream: TMemoryStream;
Texto: String;
begin
Stream:= TMemoryStream.Create;
try
Stream.LoadFromFile('imagen.jpg');
Texto:= BinToStr(Stream.Memory,Stream.Size);
finally
Stream.Free;
end;
end.
Para descifrar un texto y guardarlo en un archivo:
Código Delphi
[-]
var
Stream: TFileStream;
begin
Stream:= TFileStream.Create('imagen.jpg',fmCreate);
try
StrToStream(Texto,Stream);
finally
Stream.Free;
end;
end.
Y a partir de aquí lo que quieras, jugando con los stream puedes evitar tener que guardar nada en el disco, y hacer todas las operaciones en memoria, pero eso ya depende de lo que quieras hacer.