Page 1 of 1

Cipher and edit file notes

Posted: Mon Apr 24, 2006 9:24 am
by jqn
Hi all,
A little piece, that allows to create note files and to cipher them.
All improvements will be welcomes.

Code: Select all

;-----------------------------------------------------------------------------
; PB cipher and edit file notes
;                                by Joaquin AlcaƱiz (jqn)
; version 1 : April 2006
;-----------------------------------------------------------------------------
Declare   OpenmyFile(myfile.s)
Declare   UpdatemyFile()
Declare   ClosemyFile()
Declare.s PBCipher(Acc.s, Texto.s)
#CIFRA = "PBcipher"
#EXT   = ".mpb"
Global Filename.s, Datos.s, Modifi.l, nEdit.l
Filename = ProgramParameter()
SetGadgetFont(#PB_Default,FontID(LoadFont(#PB_Any, "Fixedsys", 9)))
OpenWindow(0,0,0,800,600,#CIFRA,#PB_Window_SystemMenu|#PB_Window_MinimizeGadget|#PB_Window_MaximizeGadget|#PB_Window_SizeGadget|#PB_Window_ScreenCentered)
CreateStatusBar(0, WindowID(0))
AddKeyboardShortcut(0, #PB_Shortcut_Escape, 10)
CreateGadgetList(WindowID(0))
nEdit = EditorGadget(#PB_Any,0,0,500,300)
SetGadgetColor(nEdit, #PB_Gadget_BackColor, RGB(255,255,200))
CreatePopupMenu(0)
MenuItem(1, "Open")
MenuItem(2, "Update")
MenuItem(3, "Close")
Initkeyboard()
if len(Filename): OpenmyFile(Filename): endif
Repeat
  select WaitWindowEvent()
    case #PB_Event_CloseWindow: break
    case #PB_Event_Gadget
          if EventGadget() = nEdit
            ExamineKeyboard()
            if KeyboardPushed(#PB_Key_All) AND IsFile(1)
              StatusBarText(0, 0, "Modified")
              Modifi = 1
            endif
          endif
    case #WM_RButtonDown: DisplayPopUpMenu(0, WindowID(0))
    case #PB_Event_Menu
          select EventMenu()
            case  1: OpenmyFile("")
            case  2: UpdatemyFile()
            case  3: ClosemyFile()
            case 10: break
          endselect
    case #WM_SIZE:
          ResizeGadget(nEdit,#PB_Ignore, #PB_Ignore, WindowWidth(0), WindowHeight(0)-StatusBarHeight(0))
  endselect
Forever
ClosemyFile()
CloseWindow(0)
END

Procedure OpenmyFile(myfile.s)
  if IsFile(1): ClosemyFile(): endif
  Pattern.s = #CIFRA+" files|*"+#EXT+"|All files|*.txt"
  if len(myfile) = 0: myfile = OpenFileRequester(#CIFRA, Filename, Pattern, 0): endif
  tamano  = FileSize(myfile)
  Datos   = ""
  Filename= ""
  Modifi  = 0
  if tamano > 0 AND ReadFile(1, myfile)
    Datos = space(tamano)
    ReadData(1, @Datos, tamano)
    StatusBarText(0, 0, "")
    SetWindowTitle(0, #CIFRA + " : " + myfile)
    Filename = myfile
  endif
  if left(Datos, len(#CIFRA)) = #CIFRA
    Datos = right(Datos, len(Datos)-len(#CIFRA))
    Datos = PBCipher("D", Datos)
  endif
  SetGadgetText(nEdit, Datos)
  SetActiveGadget(nEdit)
  ProcedureReturn Modifi
EndProcedure

Procedure UpdatemyFile()
  if len(Filename) AND IsFile(1)
    CloseFile(1)
    Nombr.s = GetFilePart(Filename)
    Nombr   = left(Nombr, len(Nombr)-len(GetExtensionPart(Filename))-1)
    Nombr   = GetPathPart(Filename) + Nombr + #EXT
    CreateFile(1, Nombr)
    Datos = #CIFRA + PBCipher("C", GetGadgetText(nEdit))
    WriteData(1, @Datos, len(Datos))
  endif
  Modifi  = 0
  StatusBarText(0, 0, "")
EndProcedure

Procedure ClosemyFile()
  if IsFile(1)
    if Modifi
      if MessageRequester(#CIFRA, "Update modified file?", #PB_MessageRequester_YesNo) = #PB_MessageRequester_Yes
        UpdatemyFile()
      endif
    endif
    CloseFile(1)
    Modifi  = 0
    StatusBarText(0, 0, "")
    SetWindowTitle(0, #CIFRA)
    SetGadgetText(nEdit, "")
  endif
EndProcedure

Procedure.s PBCipher(Acc.s, Texto.s)
; Acc=Action   "C" to cipher   "D" to decipher
  salida.s = ""
  strin.s  = ""
  Alfabe.s = ""
; create cipher string
  for n=1 to 255 step 3: Alfabe + chr(n): next
  for n=2 to 255 step 3: Alfabe + chr(n): next
  for n=3 to 255 step 3: Alfabe + chr(n): next

; select cipher key: if cipher, randomly. if decipher from last position of ciphered text
  select ucase(Acc)
    case "C": codigo = random(len(Alfabe)-1) + 1
              strin  = Texto + #CIFRA    ; Text for file identification. Checked on decipher
    case "D": codigo = FindString(Alfabe, right(Texto,1), 1)
              strin  = left(Texto, len(Texto)-1)
  endselect

  for pos = 1 to len(strin)
    posal = FindString(Alfabe, mid(strin,pos,1), 1)
;
; cipher & decipher phase. Substitution character are composed by:
;   posal.  Actual position of input character in cipher string
;   codigo. Cipher key.
;   pos.    Position in input string.
;           (Avoid repetitions like "AAAA" ciphred as "XXXX". A track to hackers)
;
    if posal
      select ucase(Acc)
        case "C": newpos = posal + codigo + pos
        case "D": newpos = posal - codigo - pos
      endselect
; chipher string act as a ring
      while newpos < 1 : newpos + len(Alfabe) : wend
      while newpos > len(Alfabe) : newpos - len(Alfabe) : wend
      salida + mid(Alfabe, newpos, 1)
    else
; characters not in cipher string
      salida + mid(strin, pos, 1)
    endif
  next

; last phase.
; If cipher adds cipher key on last position.
; If decipher checks for file identification on last characters
;
  select ucase(Acc)
    case "C": salida + mid(Alfabe, codigo, 1)
    case "D": if right(salida, len(#CIFRA)) = #CIFRA
                salida = left(salida, len(salida)-len(#CIFRA))
              else : salida = "" : endif
  endselect
  ProcedureReturn salida
EndProcedure
Best Regards
JOAQUIN

Posted: Tue Apr 25, 2006 6:35 pm
by fsw
Doesn't compile here with PB4b11.

ERROR:
ExamineKeyboard() - OpenScreen() or OpenWindowedScreen() must be called before use the keyboard commands.

:shock:

Posted: Wed Apr 26, 2006 8:44 am
by jqn
Hi,
Pls, check your code:
Line 25: Initkeyboard().
Line 32: ExamineKeyboard().

Here (PB4b11) compile and runs fine.

Posted: Wed Apr 26, 2006 11:45 am
by netmaestro
This code is not quite right. InitKeyboard(), ExamineKeyboard() and KeyboardPushed() are for dx-screened applications only. Best to remove them here and replace keyboardPushed() with GetAsyncKeystate_() for windowed applications.