Intel® Fortran Compiler
Build applications that can scale for the future with optimized code designed for Intel® Xeon® and compatible processors.
28592 Discussions

Accessing a text file added to the resources during runtime

christianh
Beginner
517 Views

Ladies an gentlemen,

I have the following problem. I have an ascii text file, that is normally loaded during the runtime of my program.
Due to the fact that the file is a text file, everybody who runs this program could directly look at the contents of the file.

I don't want this because it contains inforamtion that should not be published.

That's why I tought it would be possible to add this textfile to the resources of my program.
I had to crate a new group (I named it "File") and added the file to this group.
In theresource.rc my file is shown with its ID and when I double click on the file I can see the contents in HEX and ASCII.
When I compile mywhole program it seems that the text file has also been compiled into the executable without a problem refering too the size of the .exe.
So far so good.

Unfortunately I have some troublein accessing the file during runtime.

I tried "LoadString", but this doesn't seem to work for text files.
When I tried to use LoadString to load text from a string table everything is fine.

Converting my text file to a stringtable would make no sense because it's way to big.

Is there a chance to add a text file to the resources and to access this textfile during runtime?

Thanks for your help.
Best regards,

Christian

0 Kudos
12 Replies
Paul_Curtis
Valued Contributor I
517 Views

As you note, even if you were to add your text as a resource or as an internal character constant, the original contents can be viewed by inspecting the .exe with an editor, and would thus not achieve your security objective.

You could easily make a (weakly) encrypted version of your text file (eg, add a constant integer offset to each char value), then have your .exe read the text at runtime and remove the encryption, so the original content is never directly exposed.

0 Kudos
anthonyrichards
New Contributor III
517 Views
Compaq Visual Fortran gave anEncrypt/Decrypt example, which allows you to encrypt your file so that you can then decrypt it when you want to. Intel probably supplies the same.

0 Kudos
onkelhotte
New Contributor II
517 Views
Here is a way to access any files, that are stored as binary data in your resource file (i.e. script.rc):
1. Add your text file (here: neu.txt which is stored in folder res) as binary data to your script.rc. Ive named it TEXT. Your script.rc should look like this somewhere:
// Binary data
TEXT BINARYDATA "res\Neu.txt"
2.call the subroutine extractBinary
Jugoslav Dujic once told mehowI can store files in your executable andextract them during run-time. Ive coded that in the subroutine extractBinary. As parameter youhave the description of your file (here: TEXT) and the destination filename.
Butyour text will be visible in your executable.
Markus
[cpp]subroutine extractBinary(codeWort,zielDatei)
	use ifwin
!
	implicit none
!
	character*(*) codeWort,zielDatei
	integer(kind=4) dwResourceSize,hRes,hGlob
	integer(1)::sVoidData(*); pointer(lpVoidData,sVoidData)

	hRes=FindResource(null,loc(codeWort),loc("binarydata"c))
	hGlob=LoadResource(null,hRes);
	dwResourceSize=SizeofResource(NULL,hRes);
	lpVoidData=LockResource(hGlob);

	open(11,file=zielDatei,form="Binary")
	write(11) sVoidData(1:dwResourceSize)
	close(11)

	return
end subroutine extractBinary
[/cpp]
0 Kudos
Jugoslav_Dujic
Valued Contributor II
517 Views
Quoting - onkelhotte
[cpp]subroutine extractBinary(codeWort,zielDatei)

CHARACTER::sVoidData(*); pointer(lpVoidData,sVoidData)

lpVoidData=LockResource(hGlob);

! open(11,file=zielDatei,form="Binary")
! write(11) sVoidData(1:dwResourceSize)
! close(11)

return
end subroutine extractBinary
[/cpp]

Note, also, that with minor change I introduced above, the character array sVoidData(1:dwResourceSize) now contains the full text of your original file (well, including line endings -- char(13)//char(10)) so you can parse it using statements such as READ and INDEX, without the need to actually save it to disk.

The point that the data remain visible if someone wants to peek into the .exe file using a hex editor still stands, though.

You can also add a level of obfuscating if you use a binary file, or e.g. pack your data into a big TYPE. That all depends on how much time you want to invest in changing and which obfuscation level is necessary.

0 Kudos
anthonyrichards
New Contributor III
517 Views
Here is Compaq Visual Fortran encrypt/decrypt project that you could modify for the Intel compiler. I have deleted the .exe files. The example given is a text file 'myprogram.txt' which has been encrypted to a binaryfile called 'encrypted.txt' using the encrypt program and the password 'mypassword'. With the 'encrypted.txt' file used as input by the decrypt program, using the same password, the result is the file 'decrypted.txt' which you can see is identical to the original 'myprogram.txt'. All youneed to do is include the decrypt code into your program and store the password in it for later use.#
I have tried to add a file, but the software fails miserably and hangs at the create folder stage.

0 Kudos
anthonyrichards
New Contributor III
517 Views
Here goes..I will make another attempt at attaching a zipped archive...
0 Kudos
anthonyrichards
New Contributor III
517 Views
Quoting - anthonyrichards
Here goes..I will make another attempt at attaching a zipped archive...

Well, I got as afar as 'upload' there, then everything went quiet. Here goes on attempt #3...

0 Kudos
Steven_L_Intel1
Employee
517 Views

That looks like the sample provided with IVF. I don't recall that CVF had such a sample.

0 Kudos
anthonyrichards
New Contributor III
517 Views

That looks like the sample provided with IVF. I don't recall that CVF had such a sample.

You are correct - the Fortran Code is Intel's, as the text clearly states, so it is Intel's property ultimately I guess. We can still use it, no?

0 Kudos
Steven_L_Intel1
Employee
517 Views

Sure, you can use it. I was just confused by your saying it came from CVF and could be "modified" to use with IVF.

0 Kudos
Paul_Curtis
Valued Contributor I
517 Views

But what is the point of adding 500 lines of complex code calling multiple APIs, when the actual problem can be solved simply by

[cpp]DO j = 1, buflen
  buf(j) = CHAR(ICHAR(buf(j)) - offset)
END DO[/cpp]

which probably won't fool the NSA but certainly ensures the content of the (modified) text is not directly human-readable, which is all the OP asked for.

0 Kudos
Steven_L_Intel1
Employee
517 Views

Or you could XOR the characters with some bit pattern, For better security, use the ROT13 cipher - just don't use it twice! :-)

0 Kudos
Reply