Base64 to populate Combo

Technical support and scripting issues

Moderators: Dorian (MJT support), JRL

Post Reply
lcrltd
Junior Coder
Posts: 35
Joined: Thu Nov 16, 2006 8:23 am

Base64 to populate Combo

Post by lcrltd » Mon Jul 11, 2011 12:21 pm

Hi,
I`m trying to store encrypted items in a TXT file, and to get those to appear un-encrypted in a combo list.

So basically the txt file is unreadable unless you run this routine to view the items in the combobox.

The items are going into the txt file , line by line, but when i reverse the encryption they appear corrupted.

What I am doing is unecrypting the txt file and then putting the variable into the dialog combo, is this wrong?

adroege
Automation Wizard
Posts: 438
Joined: Tue Dec 07, 2004 7:39 pm

Post by adroege » Mon Jul 11, 2011 12:41 pm

please post your code.

lcrltd
Junior Coder
Posts: 35
Joined: Thu Nov 16, 2006 8:23 am

Post by lcrltd » Mon Jul 11, 2011 1:42 pm

to write to the txt
Let>Line=hello world
Crypt>abc,%line%,cryptval
Base64>cryptval,ENCODE,%newline%
WriteLn>c:\CCUI\cards.txt,nWLNRes,%newline%

to decrypt

ReadFile>c:\CCUI\cards.txt,cardslist

Base64>%acardslist%,DECODE,cards_crypt
Crypt>abc,cards_crypt,clean


Then populate the dialogue with %clean%

User avatar
JRL
Automation Wizard
Posts: 3526
Joined: Mon Jan 10, 2005 6:22 pm
Location: Iowa

Post by JRL » Mon Jul 11, 2011 2:00 pm

If you wrote one line at a time you need to decode one line at a time So you'd use ReadLn> rather than ReadFile>.

Edit: Added The base64 lines to prevent the possibility that the Crypt> function could write characters 13, 10 which would be interpreted as a carriage return and line feed or more likely that character 26 would be written which will be interpreted by some software as being an end of File marker.

Encrypt

Code: Select all

Let>var1=apple
Let>var2=orange
Let>var3=banana
Let>var4=plum

Let>file=%temp_dir%encrypt_example

//Encrypt the text
Let>kk=0
Repeat>kk
  add>kk,1
  Let>texttoencrypt=var%kk%
  Crypt>abc,texttoencrypt,encdata
  Base64>encdata,ENCODE,encdata
  //Write to the file
  WriteLn>file,wres,encdata
Until>kk=4

Decrypt

Code: Select all

Dialog>Dialog1
object Dialog1: TForm
  Left = 580
  Top = 166
  HelpContext = 5000
  BorderIcons = [biSystemMenu]
  Caption = 'CustomDialog'
  ClientHeight = 223
  ClientWidth = 439
  Color = clBtnFace
  Font.Charset = DEFAULT_CHARSET
  Font.Color = clWindowText
  Font.Height = -11
  Font.Name = 'MS Sans Serif'
  Font.Style = []
  OldCreateOrder = True
  ShowHint = True
  OnTaskBar = False
  PixelsPerInch = 96
  TextHeight = 13
  object MSComboBox1: tMSComboBox
    Left = 35
    Top = 23
    Width = 145
    Height = 21
    ItemHeight = 13
    TabOrder = 8
    Text = 'MSComboBox1'
  end
end
EndDialog>Dialog1

Let>file=%temp_dir%encrypt_example
Let>list=
Let>kkk=0
Repeat>kkk
  add>kkk,1
  //To read the data back and decrypt:
  ReadLn>file,kkk,encdata
  //Decrypt
  Base64>encdata,DECODE,encdata
  Crypt>abc,encdata,decryptedtext
  ConCat>List,%decryptedtext%%CRLF%
Until>kkk=4

SetDialogProperty>Dialog1,MSComboBox1,ListText,List
Show>Dialog1,res1
Following is an incorrect statement that I made: Thank you adroege for pointing this out

Also though the Crypt example in help includes adding Base64 it really isn't necessary unless you need the out put to be in a "text" format. This seems silly to me since you can't read the file contents (they're encypted) why would you need to display them and if you're not going to display them why do you care if it's text. Anyway the following two samples encrypt then decrypt a short list of items.
Last edited by JRL on Tue Jul 12, 2011 3:54 pm, edited 2 times in total.

adroege
Automation Wizard
Posts: 438
Joined: Tue Dec 07, 2004 7:39 pm

Post by adroege » Mon Jul 11, 2011 2:05 pm

Try this.

Code: Select all

//to write to the txt
IfFileExists>c:\cards.txt
  DeleteFile>c:\cards.txt
Endif
Let>Line=hello world
Crypt>abc,line,cryptval
Base64>cryptval,ENCODE,newline
WriteLn>c:\cards.txt,nWLNRes,newline

//to decrypt
ReadFile>c:\cards.txt,cardslist
Trim>cardslist,cardslist
Base64>cardslist,DECODE,cards_crypt
Crypt>abc,cards_crypt,clean
MessageModal>clean

adroege
Automation Wizard
Posts: 438
Joined: Tue Dec 07, 2004 7:39 pm

Post by adroege » Mon Jul 11, 2011 2:09 pm

To JRL:


Because crypt by itself could generate ANY ASCII values, it can also generate the ASCII values for CR LF OR even EOF.

To read back the values line-by-line requires us to be able to trust that
the CRLF at the end of each record is not part of the data. Therefore we need to use BASE64 (or something similar) to guarantee that we are writing only valid ASCII values to your text file.

User avatar
JRL
Automation Wizard
Posts: 3526
Joined: Mon Jan 10, 2005 6:22 pm
Location: Iowa

Post by JRL » Mon Jul 11, 2011 2:23 pm

Good point!

So what I was interpreting as a convenience is actually a necessity.

I've a script or two that have been using Crypt without Base64 for a long time. They do not write line by line nor read line by line. I don't know that I've had any problems with them. Have I just been lucky or is it that reading and writing all of the code simultaneously eliminates the problem?

I'll fix the code above in a minute or two. Thanks for pointing out the error of my ways.

adroege
Automation Wizard
Posts: 438
Joined: Tue Dec 07, 2004 7:39 pm

Post by adroege » Mon Jul 11, 2011 2:41 pm

I think you have been Okay in the past since you were not reading/writing line-by-line.

lcrltd
Junior Coder
Posts: 35
Joined: Thu Nov 16, 2006 8:23 am

Post by lcrltd » Tue Jul 12, 2011 8:28 am

That works fine, if I have 1 line in the txt file.
However I have several , as soon as I add line 2 , it comes back with 1 item in the combo box thats lots of weird characters.

User avatar
JRL
Automation Wizard
Posts: 3526
Joined: Mon Jan 10, 2005 6:22 pm
Location: Iowa

Post by JRL » Tue Jul 12, 2011 12:38 pm

Are you using ReadLn> or ReadFile>?

lcrltd
Junior Coder
Posts: 35
Joined: Thu Nov 16, 2006 8:23 am

Post by lcrltd » Tue Jul 12, 2011 1:29 pm

I had used Readfile BUT using a combination of the 2 solutions, I got it working . Thanks for all your help.

Jerry Thomas
Macro Veteran
Posts: 267
Joined: Mon Sep 27, 2010 8:57 pm
Location: Seattle, WA

Post by Jerry Thomas » Tue Jul 12, 2011 3:44 pm

Want to share your code so the next person that runs into this, has an answer already?
Thanks,
Jerry

[email protected]

lcrltd
Junior Coder
Posts: 35
Joined: Thu Nov 16, 2006 8:23 am

Post by lcrltd » Thu Jul 14, 2011 11:45 am

Its a bit of a mess, sorry but its part of a larger project.

Basically what I did is, look at the cards.txt file, read each line , decode and store in a new txt file, then work of that.

** Note the script always wanted to decode the last "blank" line hence the Lines_count-1

Code: Select all

ReadFile>%vpath%\cards.txt,filedata
Separate>filedata,CRLF,lines

Let>Lines_count2=lines_count-1

let>linecnt=1
label>nextline

readln>%vpath%\cards.txt,%linecnt%,cardslist
Trim>cardslist,cardslist
Base64>cardslist,DECODE,cards_crypt
Crypt>abc,cards_crypt,clean
WriteLn>%vpath%\decode.txt,nWLNRes,clean


if>%linecnt%=%lines_count2%
goto>startme
else
let>linecnt=%linecnt%+1
goto>nextline
endif

adroege
Automation Wizard
Posts: 438
Joined: Tue Dec 07, 2004 7:39 pm

Post by adroege » Thu Jul 14, 2011 7:50 pm

Usually there is no need to read and count lines before reading a file.

This example reads each line from the text file and displays it in a Message window.

Code: Select all

  Let>k=1
Label>start
  ReadLn>c:\temp\test.txt,k,line
  If>line=##EOF##,finish
  Message>line
  Let>k=k+1
  Goto>start
Label>finish

Post Reply
Sign up to our newsletter for free automation tips, tricks & discounts