INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Jobs

How To

Use the Windows Crypto API for Base64 encoding/decoding by Glenn9999
Posted: 7 Jan 13 (Edited 7 Jan 13)

For the unit that this code uses, see FAQ102-7423: Use the Windows Crypto API to get a Hash of a file.

Base64 encode

CODE

function StringToBase64(instr: string; Flags: dword): string;
var
  sz: dword;
begin
  CryptBinaryToStringA(pointer(instr), Length(instr), Flags, nil, sz);
  SetLength(result, sz);
  CryptBinaryToStringA(pointer(instr), Length(instr), Flags, pointer(result), sz);
end;

cstr := StringToBase64(instr, CRYPT_STRING_BASE64); 

Base64 decode

CODE

function StringFromBase64(instr: string; Flags: dword): string;
var
  sz, skip: dword;
begin
  CryptStringToBinaryA(pointer(instr), Length(instr), Flags, nil, sz, skip,
             Flags);
  SetLength(result, sz);
  CryptStringToBinaryA(pointer(instr), Length(instr), Flags, pointer(result),
            sz, skip, Flags);
end;
cstr := StringFromBase64(cstr, CRYPT_STRING_BASE64); 

Other flag values are in the wincrypt unit posted elsewhere.

Encoding a file into base64. I'm not sure if you can do this another way than using the API file mapping (TFileStream?), but this seems to be the best way to use the functions to do that.

CODE

procedure Base64EncFile(infilename, outfilename: string);
// copies file in infile to outfile.  Uses file mapping to read, but regular
// Delphi "file" to write.

var
  FileHandle: THandle;
  MapHandle: THandle;
  ViewPointer: Pointer;
  outptr: Pointer;
  outfile: file;
  outsize: longint;
begin
  FileHandle := CreateFile(Pchar(infilename), GENERIC_READ,
      FILE_SHARE_READ or FILE_SHARE_WRITE,nil, OPEN_EXISTING,
      FILE_ATTRIBUTE_NORMAL or FILE_FLAG_SEQUENTIAL_SCAN, 0);
  MapHandle := CreateFileMapping(FileHandle, nil, PAGE_READONLY, 0, 0, nil);
  ViewPointer := MapViewOfFile(MapHandle, FILE_MAP_READ, 0, 0, 0);
  if ViewPointer <> nil then
    try
      Assign(outfile, outfilename);
      rewrite(outfile, 1);
      CryptBinaryToStringA(ViewPointer, GetFileSize(FileHandle, nil),
                CRYPT_STRING_BASE64, nil, outsize);
      GetMem(outptr, outsize);
      CryptBinaryToStringA(ViewPointer, GetFileSize(FileHandle, nil),
                CRYPT_STRING_BASE64, outptr, outsize);
      BlockWrite(outfile, outptr^, outsize);
      Close(outfile);
      FreeMem(outptr);
    finally
      UnMapViewofFile(ViewPointer);
    end;
  CloseHandle(maphandle);
  CloseHandle(FileHandle);
end; 

Back to Embarcadero: Delphi FAQ Index
Back to Embarcadero: Delphi Forum

My Archive

Resources

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close