The Poor Man Cryptography Method ( PMCM )

Post Reply
Rochinha
Posts: 309
Joined: Sun Jan 08, 2006 10:09 pm
Location: Brasil - Sao Paulo
Contact:

The Poor Man Cryptography Method ( PMCM )

Post 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
User avatar
Antonio Linares
Site Admin
Posts: 37481
Joined: Thu Oct 06, 2005 5:47 pm
Location: Spain
Contact:

Post 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 :-)
regards, saludos

Antonio Linares
www.fivetechsoft.com
Rochinha
Posts: 309
Joined: Sun Jan 08, 2006 10:09 pm
Location: Brasil - Sao Paulo
Contact:

Post 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.
User avatar
James Bott
Posts: 4654
Joined: Fri Nov 18, 2005 4:52 pm
Location: San Diego, California, USA
Contact:

Post 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
User avatar
Euclides
Posts: 144
Joined: Wed Mar 28, 2007 1:19 pm

Post 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
User avatar
James Bott
Posts: 4654
Joined: Fri Nov 18, 2005 4:52 pm
Location: San Diego, California, USA
Contact:

Post 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
Rochinha
Posts: 309
Joined: Sun Jan 08, 2006 10:09 pm
Location: Brasil - Sao Paulo
Contact:

Post 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.
User avatar
Enrico Maria Giordano
Posts: 7355
Joined: Thu Oct 06, 2005 8:17 pm
Location: Roma - Italia
Contact:

Post 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
User avatar
James Bott
Posts: 4654
Joined: Fri Nov 18, 2005 4:52 pm
Location: San Diego, California, USA
Contact:

Post 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
User avatar
Enrico Maria Giordano
Posts: 7355
Joined: Thu Oct 06, 2005 8:17 pm
Location: Roma - Italia
Contact:

Post 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
User avatar
James Bott
Posts: 4654
Joined: Fri Nov 18, 2005 4:52 pm
Location: San Diego, California, USA
Contact:

Post 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
User avatar
James Bott
Posts: 4654
Joined: Fri Nov 18, 2005 4:52 pm
Location: San Diego, California, USA
Contact:

Post 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
User avatar
Enrico Maria Giordano
Posts: 7355
Joined: Thu Oct 06, 2005 8:17 pm
Location: Roma - Italia
Contact:

Post 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
User avatar
Enrico Maria Giordano
Posts: 7355
Joined: Thu Oct 06, 2005 8:17 pm
Location: Roma - Italia
Contact:

Post 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
Post Reply