Base64 to populate Combo
Moderators: Dorian (MJT support), JRL
Base64 to populate Combo
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?
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?
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
Decrypt
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.
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
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.
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
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.
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.
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.
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.
-
- Macro Veteran
- Posts: 267
- Joined: Mon Sep 27, 2010 8:57 pm
- Location: Seattle, WA
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
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
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.
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