FiveTech Support Forums

FiveWin / Harbour / xBase community
Board index FiveWin for Harbour/xHarbour encrypt-decrypt word doc
Posts: 6983
Joined: Fri Oct 07, 2005 07:07 PM
encrypt-decrypt word doc
Posted: Sun Jan 28, 2018 10:08 AM
Hello,
I am preparing for the GDPR.
So I try to encrypt and decrypt a word file.
But I get an error if I try to reopen the decypted word file.
Could someone please help me.

Best regards,
Otto

Code (fw): Select all Collapse
// New FiveWin Encrypt() and Decrypt() functions

#include "FiveWin.ch"

//----------------------------------------------------------------------------//

function Main()

   local cText      := ""
    local cTextDecryp   := ""
    local cLetter       := "c:\fwh\samples\InfoCardDemo.docx"
    local cTextenc  := ""
    local cDSt      := "c:\fwh\samples\InfoCardDemo.enc"
    local cLetterNew    := "c:\fwh\samples\InfoCardDemoNew.docx"
   *----------------------------------------------------------

    cText := memoread( cLetter )

   cTextenc := Encrypt( cText, "User1Key" ) 
    memowrit( cDSt, cTextenc )
    ? "Doc-Datei verschlüsselt"
    
    cText := memoread( cDSt )
    cTextDecryp = Decrypt( cText , "User1Key" ) 
    memowrit( cLetterNew, cTextDecryp )
    ? "Doc-Datei entschlüsselt"

return nil

//----------------------------------------------------------------------------//
Posts: 1392
Joined: Mon May 14, 2007 09:49 AM
Re: encrypt-decrypt word doc
Posted: Sun Jan 28, 2018 10:44 AM

I did something like encrypt part of the file docx. But I used fOpen/fWrite

Posts: 1195
Joined: Mon Oct 17, 2005 05:41 AM
Re: encrypt-decrypt word doc
Posted: Sun Jan 28, 2018 01:01 PM

Otto,

Do you use a SQL-database?
Then you store the DOC-file in a BLOB-field. The SQL-database is already password-protected.

Regards,

Marc



FWH32+xHarbour | FWH64+Harbour | BCC | DBF | ADO+MySQL | ADO+MariaDB | ADO+SQLite
Posts: 6983
Joined: Fri Oct 07, 2005 07:07 PM
Re: encrypt-decrypt word doc
Posted: Sun Jan 28, 2018 05:18 PM
Hello Natter,
thank you. Fopen and fwrite is working fine.
Hello Mark, thank you. I do not use SQL.
Best regards,
Otto

Code (fw): Select all Collapse
// Encrypt() and Decrypt() functions  DOCX 

#include "FiveWin.ch"

//----------------------------------------------------------------------------//

function Main()

    local cLetter       := "c:\fwh\samples\InfoCardDemo.docx"
    local cDocenc       := "c:\fwh\samples\InfoCardDemo.enc"
    local cLetterNew    := "c:\fwh\samples\InfoCardDemoNew.docx"
   *----------------------------------------------------------
    
    Encryptraw( cLetter, cDocenc ) 
    ? "Doc-Datei verschlüsselt"
    
    Decryptraw( cDocenc, cLetterNew )
    ? "Doc-Datei entschlüsselt"
    
return nil
    
//----------------------------------------------------------------------------//

FUNCTION  Decryptraw( cSrc, cDst )    //extended from filecopyraw by NagesWaraRao

   local hSrc, hDst, nBytes
   local nBuf  := 64000
   local cBuf  := Space( nBuf )
   local lCopied := .f.
   local cTextenc := ""
   if ( hSrc := FOpen( cSrc, 64 ) ) >= 0
      if ( hDst := FCreate( cDst, 0 ) ) >= 0
         do while .t.
            nBytes   := FRead( hSrc, @cBuf, nBuf )
            if nBytes > 0
                cTextenc := Decrypt( cBuf, "User1Key" ) 
               FWrite( hDst, cTextenc, nBytes )
            endif
            if nBytes < nBuf
               lCopied  := .t.
               exit
            endif
         enddo
         fClose( hDst )
      endif
      fClose( hSrc )
   endif

return lCopied
//----------------------------------------------------------------------------//

FUNCTION  Encryptraw( cSrc, cDst )   

   local hSrc, hDst, nBytes
   local nBuf  := 64000
   local cBuf  := Space( nBuf )
   local lCopied := .f.
   local cTextenc := ""
   if ( hSrc := FOpen( cSrc, 64 ) ) >= 0
      if ( hDst := FCreate( cDst, 0 ) ) >= 0
         do while .t.
            nBytes   := FRead( hSrc, @cBuf, nBuf )
            if nBytes > 0
                cTextenc := Encrypt( cBuf, "User1Key" ) 
               FWrite( hDst, cTextenc, nBytes )
            endif
            if nBytes < nBuf
               lCopied  := .t.
               exit
            endif
         enddo
         fClose( hDst )
      endif
      fClose( hSrc )
   endif

return lCopied
//----------------------------------------------------------------------------//

Continue the discussion