D.Tkalcec (RTC)
|
|
« Reply #5 on: January 12, 2015, 11:56:54 AM » |
|
Here is the same code as above, but working with RtcByteArray instead of AnsiString, so it is compatible with the nextgen compiler for mobile devices. Because the NextGen compiler does not have AnsiString support, "Ex" functions and methods are used, which use RtcByteArray instread of AnsiString for all binary content.
For encryption and decryption, you can use "CryptEx" and "DecryptEx" procedures from the "rtcCrypt.pas" unit. For compression and decompression, you can the "ZCompress_Ex" and "ZDecompress_Ex" functions from the "rtcZLib.pas" unit. And for reading and writing files as RtcByteArray, you can use the "Read_FileEx" and "Write_FileEx" functions in the "rtcInfo.pas" unit. Here are two example procedure implementations. "rtcWritePackedEx" for compressing, encrypting and writing the content "packed" to a file, and then you can use the "rtcReadPackedEx" function to get the original content (decrypted and uncompressed) back into memory:
uses rtcInfo, // Read_FileEx, Write_FileEx, Utf8EncodeEx & Utf8DecodeEx rtcCrypt, // CryptEx & DecyptEx rtcZLib; // ZCompress_Ex & ZDecompress_Ex
procedure rtcWritePackedEx(const FileName:String; const Key:RtcByteArray; const Content:RtcByteArray); var PackedContent:RtcByteArray; begin // For compression to be effective, we need to compress our Content before encrypting it ...
{ "ZCompress_Ex" function returns compressed content as a result (RtcByteArray) } PackedContent := ZCompress_Ex(Content, zcDefault); // using default compression strength
{ "CryptEx" procedure encrypts content "in place", so the variable "PackedContent" will contain the result } CryptEx(PackedContent, Key);
{ Write the resulting Packed Content to the file "FileName" } Write_FileEx(FileName, PackedContent); end;
function rtcReadPackedEx(const FileName:String; const Key:RtcByteArray):RtcByteArray; var PackedContent:RtcByteArray; begin { Read compressed and encrypted content } PackedContent := Read_FileEx(FileName);
// Content has been compressed and then encrypted before being written, // so we need to do it in reverse order when reading (first decrypt, then decompress) ...
{ "DecryptEx" procedure decrypts content "in place", so the variable "PackedContent" will also contain the result } DecryptEx(PackedContent, Key);
{ "ZDecompress_Ex" function returns decompressed content as a result (RtcByteArray) } Result := ZDecompress_Ex(PackedContent); end;
If your content is in a WideString or a Unicode String ("String" type in Delphi 2009 and later), you can use "Utf8EncodeEx" and "Utf8DecodeEx" functions to get that Wide/Unicode String to and from a UTF-8 encoded RtcByteArray. "Utf8EncodeEx" function will convert a WideString into an UTF-8 encoded RtcByteArray. And to get the RtcByteArray back into a WideString, you can use the "Utf8DecodeEx" function. For example, the functions below accept WideString Key and Content parameters and return a WideString result:
procedure rtcWritePackedWideEx(const FileName:String; const Key:WideString; const Content:WideString); begin // Content and Key need to be encoded from WideString to RtcByteArray rtcWritePackedEx(FileName, Utf8EncodeEx(Key), Utf8EncodeEx(Content)); end;
function rtcReadPackedWideEx(const FileName:String; const Key:WideString):WideString; begin // Key needs to be encoded from WideString to RtcByteArray, but the resulting // Content has to be Decoded from RtcByteArray back to WideString Result:= Utf8DecodeEx( rtcReadPackedEx(FileName, Utf8EncodeEx(Key)) ); end;
Best Regards, Danijel Tkalcec
|