کد:
unit XPBase64;
// Author: snowy, dumb.
interface
uses Windows;
const
crypt32 = 'Crypt32.dll';
// Flags:
CRYPT_STRING_BASE64HEADER = 0;
// Base64, with certificate Beginning and ending headers
CRYPT_STRING_BASE64 = 1;
// Base64, without headers
CRYPT_STRING_BINARY = 2;
// pure binary copy
CRYPT_STRING_BASE64REQUESTHEADER = 3;
// Base64, with request Beginning and ending headers
CRYPT_STRING_HEX = 4;
// Hexadecimal Only
CRYPT_STRING_HEXASCII = 5;
// Hexadecimal, with ASCII character display
CRYPT_STRING_BASE64X509CRLHEADER = 9;
// Base64, with X.509 CRL Beginning and ending headers
CRYPT_STRING_HEXADDR = 10;
// Hexadecimal, with Address display
CRYPT_STRING_HEXASCIIADDR = 11;
// Hexadecimal, with ASCII character display and Address
CRYPT_STRING_HEXRAW = 12;
// A raw hex string.
function ToBase64(s: string; Flags: dword = CRYPT_STRING_BASE64REQUESTHEADER): string;
function FromBase64(s: string; Flags: dword = CRYPT_STRING_BASE64REQUESTHEADER): string;
function CryptStringToBinary(pszString: PChar; cchString: dword; dwFlags: dword; pbBinary: pointer; var pcbBinary: dword; var pdwSkip: dword;
var pdwFlags: dword): Boolean; stdcall;
function CryptBinaryToString(pbBinary: pointer; cbBinary: dword; dwFlags: dword; pszString: PChar; var pcchString: dword): Boolean; stdcall;
function CryptStringToBinaryA(pszString: PChar; cchString: dword; dwFlags: dword; pbBinary: pointer; var pcbBinary: dword; var pdwSkip: dword;
var pdwFlags: dword): Boolean; stdcall;
function CryptBinaryToStringA(pbBinary: pointer; cbBinary: dword; dwFlags: dword; pszString: PChar; var pcchString : dword): Boolean; stdcall;
function CryptStringToBinaryW(pszString: PWideChar; cchString: dword; dwFlags: dword; pbBinary: pointer; var pcbBinary: dword; var pdwSkip: dword;
var pdwFlags: dword): Boolean; stdcall;
function CryptBinaryToStringW(pbBinary: pointer; cbBinary: dword; dwFlags: dword; pszString: PWideChar; var pcchString: dword): Boolean; stdcall;
Implementation
function CryptStringToBinary; external crypt32 name 'CryptStringToBinaryA';
function CryptBinaryToString; external crypt32 name 'CryptBinaryToStringA';
function CryptStringToBinaryA; crypt32 external name 'CryptStringToBinaryA';
function CryptBinaryToStringA; external crypt32 name 'CryptBinaryToStringA';
function CryptStringToBinaryW; external crypt32 name 'CryptStringToBinaryW';
function CryptBinaryToStringW; external crypt32 name 'CryptBinaryToStringW';
function ToBase64(s: string; Flags: dword = CRYPT_STRING_BASE64REQUESTHEADER): string;
var sz: dword;
begin
CryptBinaryToString(pointer(s), Length(s), Flags, nil, sz);
SetLength(result, sz);
CryptBinaryToString(pointer(s), Length(s), Flags, pointer(result), sz);
end;
function FromBase64(s: string; Flags: dword = CRYPT_STRING_BASE64REQUESTHEADER): string;
var sz, Skip: dword;
begin
CryptStringToBinary(pointer(s), Length(s), Flags, nil, sz , Skip, Flags);
SetLength(result, sz);
CryptStringToBinary(pointer(s), Length(s), Flags, pointer(result), sz, Skip, Flags);
end;
end.
کد:
uses XPBase64;
// Author: snowy, dumb.
procedure TForm1.Button1Click(Sender: TObject);
begin
MEMO2 include.Text : = ToBase64(Memo1.Text);
end;
procedure TForm1.Button2Click(Sender: TObject);
begin
Memo3.Text : = FromBase64(MEMO2 include.Text);
end;