PDA

Click to See Complete Forum and Search --> : encrypting html files....?


ianpaisley
Dec 7th, 1999, 06:04 AM
Hi there,

I'm looking for a way to encrypt my html files so that they can only be opened by the Web browser control on my form. I found this code (pasted by Aaron Young, I think - thank you Aaron!) on how to encrypt text files. It works for text files. I replaced 'Text1', as the code originally was, with WebBrowser1' - the name of my browser control, but it doesn't work.

Does anybody know how I can resolve my problem - encrypt my html files? I don't want people
to be able to open them in any browser - just mine.

Thanks for any help!


Private Sub Command1_Click()
'Open Encrypted File
Dim sText As String
Dim iFile As Integer

On Error GoTo Cancelled
With CommonDialog1
.CancelError = True
.Filter = "AY Encrypted|*.aye"
.ShowOpen
iFile = FreeFile
Open .FileName For Input As iFile
sText = Input(LOF(iFile), iFile)
Close iFile
End With
If Left(sText, 12) <> "AY Encrypted" Then
MsgBox "Not an AY Encrypted File Format"
Else
WebBrowser1 = DecryptText(Mid(sText, 13))
End If

Cancelled:
End Sub
Private Sub Command2_Click()
'Save Encrypted File
On Error GoTo Cancelled
With CommonDialog1
.CancelError = True
.Filter = "AY Encrypted|*.aye"
.ShowSave
iFile = FreeFile
Open .FileName For Output As iFile
Print #iFile, EncryptText(WebBrowser1);
Close iFile
End With
MsgBox "Saved"
Cancelled:
End Sub
Function EncryptText(ByVal sText As String) As String
Dim sKey As String
Dim iKey As Integer
Dim lPos As Long
Dim iChar As Integer
Randomize Timer
sKey = Right("000" & Hex(Int(Rnd(1) * 4095)), 3)
For lPos = 1 To Len(sText)
iChar = Asc(Mid$(sText, lPos, 1))
Mid$(sText, lPos, 1) = Chr(iChar + Val("&H" & Mid(sKey, iKey + 1, 1)))
iKey = (iKey + 1) Mod 3
Next
EncryptText = "AY Encrypted" & sText & Right(Space(20) & (Val("&H" & sKey) + Len(sText)), 20)

End Function
Function DecryptText(ByVal sText As String) As String
Dim sKey As String
Dim iKey As Integer
Dim lPos As Long
Dim iChar As Integer
sKey = Right("000" & Hex(Val(Right(sText, 20)) - (Len(sText) - 20)), 3)
For lPos = 1 To Len(sText) - 20
iChar = Asc(Mid$(sText, lPos, 1))
Mid$(sText, lPos, 1) = Chr(iChar - Val("&H" & Mid(sKey, iKey + 1, 1)))
iKey = (iKey + 1) Mod 3
Next
DecryptText = Left(sText, Len(sText) - 20)
End Function

Joacim Andersson
Dec 7th, 1999, 06:20 AM
You can't assign text to the web browser object like this:
WebBrowser1 = DecryptText(Mid(sText, 13))
What you have to do is to save the decrypted file to a temporary HTML file and use the Navigate method of the web browser object to open the file.

iFile = FreeFile
Open "c:\MyEncrypted.htm" For Output As #iFile
Print #iFile, DecryptText(Mid$(sText, 13))
Close #iFile
WebBrowser1.Navigate "c:\MyEncrypted.htm"

Good luck!

------------------
Joacim Andersson
joacim@programmer.net
joacim@yellowblazer.com
www.YellowBlazer.com (http://www.YellowBlazer.com)

ianpaisley
Dec 7th, 1999, 03:27 PM
Thanks Joacim, I'll give it a go!

Jim_Hubbard
Dec 7th, 1999, 08:27 PM
Perhapsa a better way would be to directly access the webbrowser.document object. Here, you can directly manipulate the HTML text of the Webbrowser.Document object.

There is no local help (i.e. help files) for the extended objects in the Webbrowser control. To get started with these internal objects, go to http://msdn.microsoft.com/workshop/browser/webbrowser/reference/reference.asp .

Have fun!

Juan Carlos Rey
Dec 8th, 1999, 06:28 AM
May be there is a simpler/better way: Embed your code into your .exe:

-------
WebBrowser1.Navigate2 "about:blank"

'Then insert your own HTML code:

Dim MyHTML As String

MyHTML = "Your HMTL Code Here"
WebBrowser1.Document.Body.Innerhtml = MyHTML
-------

About.blank is the default page when your browser can't find a server. You can replace there a specially made page, with just usual <html> and <body> tags, as Innerhtml method will insert your code between those "body tags", so you can define your background, etc.

"Your HMTL Code Here" means your own page, i.e. all your code between <body> and </body>

P.D. I'd better be fair mentioning the source:

http://www.vbsquare.com/code/230.htm

ianpaisley
Dec 9th, 1999, 03:17 AM
Thanks for your help, everybody.

Juan Carlos,

I had thought of your idea, but the problem is that I have links between the .htm files that are encrypted. Maybe that's doesn't matter too much - I'll have to study it in more detail.

How about this idea? I already know how to encrypt a text file. When the user clicks the 'Open' button, they think they're actually opening the html file into the browser control. But what they're actually doing is opening the encrypted text file into an invisible textbox - which turns out decrypted. Then, I'll instruct the webbrowser to open whatever text is in the textbox - on the text_change event or something. Does anyone know the code for doing this last part?

In any case, thanks a lot!

Juan Carlos Rey
Dec 9th, 1999, 08:37 AM
Think twice.

With the InnerHTML method:

* You don't have to supply extra files
* Your user won't be able to see the code even if he/she right clicks on the browser (all they will see is the code for the about:blank page or your specially made page, with just bgcolor or background img)
* I don't see any problems including links. You can even use internal links within a single page ("a name" method)