encrypt-decrypt word doc

encrypt-decrypt word doc

Postby Otto » 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: Select all  Expand view
// 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

//----------------------------------------------------------------------------//
********************************************************************
mod harbour - Vamos a la conquista de la Web
modharbour.org
https://www.facebook.com/groups/modharbour.club
********************************************************************
User avatar
Otto
 
Posts: 6003
Joined: Fri Oct 07, 2005 7:07 pm

Re: encrypt-decrypt word doc

Postby Natter » Sun Jan 28, 2018 10:44 am

I did something like encrypt part of the file docx. But I used fOpen/fWrite
Natter
 
Posts: 1119
Joined: Mon May 14, 2007 9:49 am

Re: encrypt-decrypt word doc

Postby Marc Vanzegbroeck » Sun Jan 28, 2018 1: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
Marc Vanzegbroeck
 
Posts: 1157
Joined: Mon Oct 17, 2005 5:41 am
Location: Belgium

Re: encrypt-decrypt word doc

Postby Otto » Sun Jan 28, 2018 5: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: Select all  Expand view

// 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
//----------------------------------------------------------------------------//





 
********************************************************************
mod harbour - Vamos a la conquista de la Web
modharbour.org
https://www.facebook.com/groups/modharbour.club
********************************************************************
User avatar
Otto
 
Posts: 6003
Joined: Fri Oct 07, 2005 7:07 pm


Return to FiveWin for Harbour/xHarbour

Who is online

Users browsing this forum: No registered users and 15 guests