Page 1 of 1

The Poor Man Cryptography Method ( PMCM )

Posted: Sat Sep 13, 2008 6:43 am
by Rochinha
Friends

The code of tip:

Code: Select all

// Some MDI application testing

// Cryptograph some fields
// By Rochinha - 5Volution

#include "FiveWin.ch"
#include "Customer.ch"

static oWnd, oClients, oClient, oName
static cName
static oSplit
static lEncrypted

//----------------------------------------------------------------------------//
function main()
   SET _3DLOOK ON

   // ********** Check if file have a field encrypted **********
   cPath      := cFilePath( GetModuleFileName( GetInstance() ) )
   cEncrypted := VerifyINI( "Customer", "Encripted", "N", cPath+"crypto.ini" )
   lEncrypted := iif( cEncrypted="N", .f., .t. )
   if ! lEncrypted
      USE crypto EXCLUSIVE
      do while .not. eof()
         dbRLock()
         crypto->FIRST := FEncripta( crypto->FIRST )
         crypto->LAST  := FEncripta( crypto->LAST )
         crypto->STREET:= FEncripta( crypto->STREET )
         crypto->CITY  := FEncripta( crypto->CITY )
         dbRUnLock()
         dbCommit()
         skip
      enddo
      USE
      VerifyINI( "Customer", "Encripted", "S", cPath+"crypto.ini", .t. )
      ? "Encrypted"
   endif

   // ********** Make index on encrypted field **********
   if !FILE( "crypto.NTX" )
      USE crypto EXCLUSIVE
      INDEX ON FDecripta( FIRST ) TO crypto
      USE
   endif

   USE crypto NEW SHARED INDEX crypto
   DEFINE WINDOW oWnd TITLE "Cryptograph Test" MDI MENU BuildMenu() COLOR "N/W"

          DEFINE BUTTONBAR OF oWnd
          SET MESSAGE OF oWnd TO "Fivewin Power" CENTERED

   ACTIVATE WINDOW oWnd ;
            VALID MsgYesNo( "Tem certeza?" )
   dbCloseAll()
   return nil

//----------------------------------------------------------------------------//
function BuildMenu()
   local oMenu
   MENU oMenu
      MENUITEM "&DataBases"
      MENU
         MENUITEM "&Clients Encrypteds..." ACTION  BrwClientsE() ;
            MESSAGE "Clients management"
         MENUITEM "&Clients Decrypteds..." ACTION  BrwClientsD() ;
            MESSAGE "Clients management"
         MENUITEM "&Report..." ACTION GenReport()
         SEPARATOR
         MENUITEM "&End" ACTION oWnd:End() ;
            MESSAGE "End this test"
      ENDMENU
      oMenu:AddMdi()              // Add standard MDI menu options
   ENDMENU
   return oMenu

//----------------------------------------------------------------------------//
function brwClientsE()
   local oBrw, oIco, oBar
   if oClients != nil
      return nil
   endif
   DEFINE ICON oIco FILENAME "..\icons\customer.ico"
   DEFINE WINDOW oClients TITLE "Clients management" MDICHILD ICON oIco
          DEFINE BUTTONBAR oBar OF oClients
          DEFINE BUTTON OF oBar ACTION ShowClient()
          @ 2, 0 LISTBOX oBrw FIELDS first,last,street,city OF oClients ;
                 HEADERS "First","Last","Street","City" ;
                 FIELDSIZES 200, 200, 300.150 ;
                 SIZE 500, 500 ;
                 ON CHANGE ChangeClient()
          oClients:SetControl( oBrw )
   ACTIVATE WINDOW oClients VALID( oClients := nil, .t. )
   return nil

//----------------------------------------------------------------------------//
function brwClientsD()
   local oBrw, oIco, oBar
   if oClients != nil
      return nil
   endif
   DEFINE ICON oIco FILENAME "..\icons\customer.ico"
   DEFINE WINDOW oClients TITLE "Clients management" MDICHILD ICON oIco
          DEFINE BUTTONBAR oBar OF oClients
          DEFINE BUTTON OF oBar ACTION ShowClient()
          @ 2, 0 LISTBOX oBrw FIELDS ;
                 FDecripta( first ),;
                 FDecripta( last ),;
                 FDecripta( street ),;
                 FDecripta( city ) OF oClients ;
                 HEADERS "First","Last","Street","City" ;
                 FIELDSIZES 200, 200, 300.150 ;
                 SIZE 500, 500 ;
                 ON CHANGE ChangeClient()
          oClients:SetControl( oBrw )
   ACTIVATE WINDOW oClients VALID( oClients := nil, .t. )
   return nil

//----------------------------------------------------------------------------//
function GenReport()
   local oWnd, oIco
   DEFINE ICON oIco FILENAME "..\icons\print.ico"
   DEFINE WINDOW oWnd MDICHILD TITLE "Clients report" ;
          VSCROLL HSCROLL ICON oIco
   ACTIVATE WINDOW oWnd
   return nil

//----------------------------------------------------------------------------//
function ShowClient()
   local oIco
   if oClient != nil
      return nil
   endif
   DEFINE ICON oIco FILENAME "..\icons\Person.ico"
   DEFINE DIALOG oClient RESOURCE "Client" ICON oIco
          REDEFINE SAY ID 3 OF oClient   // To get the proper color
          REDEFINE SAY ID 4 OF oClient
          REDEFINE SAY ID 5 OF oClient
          REDEFINE GET oName VAR cName ID ID_NAME OF oClient
          REDEFINE BUTTON ID ID_NEXT OF oClient ACTION GoNext()
          SELECT Sales     // We select Sales to properly initialize the Browse
          REDEFINE LISTBOX FIELDS ID ID_SALES OF oClient
   ACTIVATE DIALOG oClient CENTERED NOWAIT ;
            VALID ( oClient := nil, .t. )           // Destroy the object
   SELECT Clients
   return nil

//----------------------------------------------------------------------------//
function ChangeClient()
   if oClient != nil
      cName = AllTrim( FDecripta( crypto->Last ) ) + ", " + FDecripta( crypto->First )
      oName:Refresh()
   endif
   return nil

//----------------------------------------------------------------------------//
function GoNext()
   if oClients != nil
      oClients:oControl:GoDown()
   else
      SKIP
      if EoF()
         GO BOTTOM
      endif
   endif
   ChangeClient()
   return nil

Function VerifyINI( _section_, _entry_, _var_, _inifile_, _grava_ )
   oIni := TIni():New( _inifile_ )
   if _grava_ = .t.
      oIni:Set( _section_, _entry_, _var_ )
   endif
   return oIni:Get( _section_, _entry_, _var_, _var_ )

Function FEncripta( _oque_ )
   return "5VZ" + Codifica( alltrim( _oque_ ) )

Function FDecripta( _oque_ )
   return iif( "5VZ" $ _oque_, Decodifica( StrTran( _oque_, "5VZ", "" ) ), _oque_ )

Function codifica( _pass_ )
    _senha_ := ''
    for i = 1 to len(alltrim(_pass_))
        _senha_ := _senha_ + chr(asc(substr(_pass_,i,1))+9)
    next
    return _senha_

Function decodifica( _pass_ )
    _senha_ := ''
    for i = 1 to len(alltrim(_pass_))
        _senha_ := _senha_ + chr(asc(substr(_pass_,i,1))-9)
    next
    return _senha_
This sample speek for me:
Download at http://www.5volution.com/forum/crypto.zip

Need customer.ch

Posted: Sat Sep 13, 2008 2:06 pm
by Antonio Linares
Rochinha,

Does Function codifica( _pass_ ) use a standard crypto routine ?

If not, you could use FWH Encrypt() and Decrypt() as they are coded in C language and are extremelly fast :-)

Posted: Sat Sep 13, 2008 7:01 pm
by Rochinha
Antonio,

My routines are didactic. but, you can use the FWH functions. I needed to know the encrypted outcome. I will use your functions.

Posted: Sat Sep 13, 2008 7:07 pm
by James Bott
Antonio,

>If not, you could use FWH Encrypt() and Decrypt() as they are coded in C language and are extremelly fast

I tried using these some time ago, and they only worked some of the time. Was this ever reported, and/or fixed?

Regards,
James

Posted: Sat Sep 13, 2008 7:19 pm
by Euclides
Hi all,
I use the FW functions Encrypt() / Decrypt() since the FW 2.0 to check the UserĀ“s login password. No coplains about.
Regards, Euclides

Posted: Sat Sep 13, 2008 9:25 pm
by James Bott
I reviewed my notes and the problem I had was that encrypted text sometimes contains characters that cannot be saved and retreived from an INI file. I checked and the encrypt()/decrypt() works if the same encrypted text is not saved to an INI file.

So the problem is NOT that encrypt()/decrypt() doesn't work, but rather that encrypted text cannot reliably be saved and retrieved from an INI file. I don't know if encrypted text could be saved to other media such as a DBF, but I suspect that the same problem would exist.

Without being able to store encrypted text, I am not sure what we can use the encrypt/decrypt routines for.

James

Posted: Sat Sep 13, 2008 11:10 pm
by Rochinha
I agree with James,

I needed to save passwords in .INI files and could not with the current functions, then used simple and especific functions.

Posted: Sun Sep 14, 2008 9:53 am
by Enrico Maria Giordano
You can encode/decode the crypted string. I'm using the following functions:

Code: Select all

FUNCTION ENCODE( cStr )

    LOCAL cRes := ""

    LOCAL i

    cStr = HB_CRYPT( cStr, "MYKEY" )

    FOR i = 1 TO LEN( cStr )
        cRes += STRZERO( ASC( SUBSTR( cStr, i, 1 ) ), 3 )
    NEXT

    RETURN cRes


FUNCTION DECODE( cStr )

    LOCAL cRes := ""

    LOCAL i

    FOR i = 1 TO LEN( cStr ) STEP 3
        cRes += CHR( VAL( SUBSTR( cStr, i, 3 ) ) )
    NEXT

    RETURN HB_DECRYPT( cRes, "MYKEY" )
EMG

Posted: Sun Sep 14, 2008 5:16 pm
by James Bott
Enrico,

So these are not the same as the FWH functions? Are you saying you can store the encrypted string in an INI and/or a DBF?

Regards,
James

Posted: Sun Sep 14, 2008 6:47 pm
by Enrico Maria Giordano
James Bott wrote:Enrico,

So these are not the same as the FWH functions?
I used xHarbour functions but FWH functions can be used as well.
James Bott wrote:Are you saying you can store the encrypted string in an INI and/or a DBF?
Yes, after encoding it.

EMG

Posted: Sun Sep 14, 2008 9:15 pm
by James Bott
Enrico,

>I used xHarbour functions but FWH functions can be used as well.

FWH functions cannot be used if you save the encrypted data to an INI file. For instance, I encrypt and save "toad" and when retrieved and decrypted you get "shf".

James

Posted: Sun Sep 14, 2008 9:21 pm
by James Bott
Enrico,

When I try to compile your functions I am gretting the following error:

Error: Unresolved external '_adler32' referenced from C:\XHARBOUR\LIB\RTL.LIB|hbcrypt

Am I missing a LIB?

I am using FWH 8.08 and xHarbour.

James

Posted: Mon Sep 15, 2008 7:25 am
by Enrico Maria Giordano
James Bott wrote:FWH functions cannot be used if you save the encrypted data to an INI file.
You can if you encode the encrypted string. This is my encoding function modified for FWH Encrypt() function:

Code: Select all

FUNCTION ENCODE( cStr ) 

    LOCAL cRes := "" 

    LOCAL i 

    cStr = ENCRYPT( cStr, "MYKEY" ) 

    FOR i = 1 TO LEN( cStr ) 
        cRes += STRZERO( ASC( SUBSTR( cStr, i, 1 ) ), 3 ) 
    NEXT 

    RETURN cRes
EMG

Posted: Mon Sep 15, 2008 7:32 am
by Enrico Maria Giordano
James Bott wrote:Enrico,

When I try to compile your functions I am gretting the following error:

Error: Unresolved external '_adler32' referenced from C:\XHARBOUR\LIB\RTL.LIB|hbcrypt

Am I missing a LIB?
These are the LIBs I'm using:

Code: Select all

fivehx.lib +
fivehc.lib +
rtl.lib +
vm.lib +
common.lib +
gtgui.lib +
lang.lib +
rdd.lib +
dbffpt.lib +
dbfntx.lib +
dbfcdx.lib +
hbsix.lib +
macro.lib +
pcrepos.lib +
hbzip.lib +
zlib.lib +
tip.lib +
ct.lib +
odbc32.lib +
import32.lib +
cw32.lib +
wininet.lib +
winscard.lib +
rasapi32.lib +
msimg32.lib +
iphlpapi.lib
EMG