Solved

How to encrypt/decrypt various files only with my VP app?

Posted on 2002-04-05
11
273 Views
Last Modified: 2012-05-04
I have a VB app that imports .txt, .BMP,  and .PDF files for viewing.
How can I change the files so that ONLY my app will be able to view them?
This has to be quick so reading in the entire file is probably not an option.
Can I change just one bit or someting?
What vb code will encode and what code will decode the file?
0
Comment
Question by:rbend
  • 3
  • 2
  • 2
  • +4
11 Comments
 
LVL 5

Expert Comment

by:rkot2000
ID: 6921437
you can try to open file in binary mode swap/add binary data.

or you can store all your files in the password protected access database.
0
 
LVL 22

Accepted Solution

by:
rspahitz earned 50 total points
ID: 6921464
For encoded files like bmp and pdf, you can probably just add a byte to the front of the file, but for text files, you'd have to encrypt each byte.

Of course, you may be able to shortcut it by simply changing the extension name!  (Although then you'll need to figure out what extension belongs to the file so you may want to append it to the file then strip it off when using it.
0
 
LVL 75

Expert Comment

by:Anthony Perkins
ID: 6921481
Please maintain your open questions:

How to refresh a non-model dialog box Date: 01/24/2002 11:09AM PST
http://www.experts-exchange.com/jsp/qShow.jsp?ta=asp&qid=20259126
How to show system resource percentage used ? Date: 11/23/1999 05:42AM PST  
http://www.experts-exchange.com/jsp/qShow.jsp?ta=visualbasic&qid=10237360
How to get the Novel Netware username from a PC Date: 12/21/2001 06:08PM PST  
http://www.experts-exchange.com/jsp/qShow.jsp?ta=visualbasic&qid=20248374
How to distribute VB app on cd rom without need to install Date: 01/30/2002 04:36PM PST  
http://www.experts-exchange.com/jsp/qShow.jsp?ta=visualbasic&qid=20261363
How to make my PC be seen via FTP with LINKSYS 4 port router Date: 02/02/2002 02:09PM PST  
http://www.experts-exchange.com/jsp/qShow.jsp?ta=routerswitch&qid=20262426
How to recover from "CMOS checksum error..loading defaults" Date: 03/09/2002 12:07PM PST  
http://www.experts-exchange.com/jsp/qShow.jsp?ta=hardgen&qid=20275314
How do I change the SQL statement programatically using CR Object? Date: 01/14/2002 10:40AM PST
http://www.experts-exchange.com/jsp/qShow.jsp?ta=crystal&qid=20255074
Need to hook NT box to "Kbd & Mouse Switchbox" ... won't work Date: 09/09/2001 07:30AM PST
http://www.experts-exchange.com/jsp/qShow.jsp?ta=winnt&qid=20180548
What converts MP3 to AVI ? Date: 01/28/2002 05:03PM PST
http://www.experts-exchange.com/jsp/qShow.jsp?ta=win98&qid=20260480
What reg entry controls computer name? Date: 02/25/2002 09:47AM PST
http://www.experts-exchange.com/jsp/qShow.jsp?ta=win98&qid=20270783

Thanks,
Anthony
0
 

Author Comment

by:rbend
ID: 6921517
rspahtz:
Please illustrate how to add 3 bytes to the beginning of a file and how to read them and finally how to strip them off.
This might be the answer.
If I can put certian bytes on, read them to be sure they are the right ones, then strip them off to read the file, I think this may work.
??
0
 
LVL 22

Expert Comment

by:rspahitz
ID: 6921742
Private sub EncodeFile(Filename as string)
  on error goto EncodeFile_Err

  dim iFileNumIn as integer
  dim iFileNumOut as intger
  dim bytEncodingByte as Byte

  const cTempFilename as string = "xyxyx.tmp"
  const cBkupFilename as string = "backup.tmp"

  bytEncodingByte = chr$(17) ' or whatever # you want from 0 to 255

  iFileNumIn = freefile()
  open Filename for binary as #iFileNumIn
  iFileNumOut = freefile()
  open cTempFilename for output as #iFileNumOut
  print #iFileNumOut, bytEncodingByte;
' May fail if lof(iFileNumIn) exceeds 32K
  print #iFileNumOut, input$(lof(iFileNumIn),iFileNumIn);
  close #iFileNumOut
  close #iFileNumIn

  name Filename as cBkupFilename
  name cTempFilename as Filename
EncodeFile_Exit:
  exit sub

EncodeFile_Err:
  ' check for error type--if "open error", then exit
  resume EncodeFile_Exit
  ' if not "open err" then do something else, maybe
End sub

--
Decode would do similar, but the print line is:

  print #iFileNumOut, mid$(input$(lof(iFileNumIn),iFileNumIn),2);

0
Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

 

Author Comment

by:rbend
ID: 6921822
seems to put the byte on ok, but removing it doesn't seem to work by simply replacing the "print" line as you say.
Any thoughts?
bb
0
 
LVL 22

Expert Comment

by:rspahitz
ID: 6921949
Well...that's why I said "similar".  Try this:

Private sub DecodeFile(Filename as string)
 on error goto DecodeFile_Err

 dim iFileNumIn as integer
 dim iFileNumOut as intger
 dim bytEncodingByte as Byte
 dim strFirstByte as Byte

 const cTempFilename as string = "xyxyx.tmp"
 const cBkupFilename as string = "backup.tmp"

 bytEncodingByte = chr$(17) ' or whatever # you want from 0 to 255

 iFileNumIn = freefile()
 open Filename for binary as #iFileNumIn
 iFileNumOut = freefile()
 open cTempFilename for output as #iFileNumOut
 strFirstByte = input$(iFileNumIn,1)
 if strFirstByte =bytEncodingByte then
  'May fail if lof(iFileNumIn) exceeds 32K
   print #iFileNumOut, mid$(input$(lof(iFileNumIn),iFileNumIn),2);
 else
   msgbox "This file is not properly encoded, and should not be decoded!", vbOKOnly or vbExclamation
 endif
 close #iFileNumOut
 close #iFileNumIn

 name Filename as cBkupFilename
 name cTempFilename as Filename
DecodeFile_Exit:
 exit sub

DecodeFile_Err:
 ' check for error type--if "open error", then exit
 resume DecodeFile_Exit
 ' if not "open err" then do something else, maybe
End sub

0
 
LVL 17

Expert Comment

by:inthedark
ID: 6923639
rbend, the problem is that this method won't work for text files (~:}

You can create a simple encode/decode class which uses XOR and a password:

I will post some code....back soon...
0
 
LVL 17

Expert Comment

by:inthedark
ID: 6923647
Simple but effective encrypt/decrypt routine:

Hope this helps.....inthedark... :~)

Option Explicit

Private Sub Form_Load()

Dim m$
Dim l$
Dim password As String

m$ = "Mary had a little lamb"

password = "NICK WAS HERE"
Dim e$

e$ = Encrypt(m$, password)
l$ = Decrypt(e$, password)

If l$ <> m$ Then
    MsgBox "Stupid idea idea does not work"
Else
    MsgBox "Good idea idea it works: " + vbCrLf + m$ + vbCrLf + "Came back to: " + l$, vbExclamation, "First Test"
End If

' now the real test

Dim c As Long
m$ = ""
For c = 0 To 255
    m$ = m$ + Chr$(c)
Next c
l$ = Decrypt(Encrypt(m$, password), password)
If l$ <> m$ Then
    MsgBox "Stupid idea it does not work", vbExclamation, "Second Test"
Else
    MsgBox "Good idea it works very well"
    Me.AutoRedraw = True
    Me.Print "Good idea it works very well"
End If

End Sub


Public Function Encrypt(SourceData As String, password As String) As String

Dim S$
S$ = Space$(Len(SourceData))
If Len(S$) = 0 Then Exit Function

Dim PC As Long
Dim LC As Long

For LC = 1 To Len(S$)
    PC = PC + 1
    If PC > Len(password) Then
        PC = 1
    End If
    Mid$(S$, LC, 1) = Chr(Asc(Mid(SourceData, LC, 1)) Xor Asc(Mid$(password, PC, 1)))
    'MsgBox CStr(Asc(Mid$(S$, LC, 1)))
Next

Encrypt = S$

End Function

Public Function Decrypt(EncryptedData As String, password As String) As String

Decrypt = Encrypt(EncryptedData, password)


End Function


0
 
LVL 5

Expert Comment

by:Netminder
ID: 6967671
Admin notified of User neglect.
Netminder
CS Moderator
0
 
LVL 1

Expert Comment

by:Moondancer
ID: 7017257
ADMINISTRATION WILL BE CONTACTING YOU SHORTLY.  Moderators Computer101, Netminder or Mindphaser will return to finalize these if they are still open in 7 days.  Experts, please post closing recommendations before that time.

Below are your open questions as of today.  Questions which have been inactive for 21 days or longer are considered to be abandoned and for those, your options are:
1. Accept a Comment As Answer (use the button next to the Expert's name).
2. Close the question if the information was not useful to you, but may help others. You must tell the participants why you wish to do this, and allow for Expert response.  This choice will include a refund to you, and will move this question to our PAQ (Previously Asked Question) database.  If you found information outside this question thread, please add it.
3. Ask Community Support to help split points between participating experts, or just comment here with details and we'll respond with the process.
4. Delete the question (if it has no potential value for others).
   --> Post comments for expert of your intention to delete and why
   --> YOU CANNOT DELETE A QUESTION with comments; special handling by a Moderator is required.

For special handling needs, please post a zero point question in the link below and include the URL (question QID/link) that it regards with details.
http://www.experts-exchange.com/jsp/qList.jsp?ta=commspt
 
Please click this link for Help Desk, Guidelines/Member Agreement and the Question/Answer process.  http://www.experts-exchange.com/jsp/cmtyHelpDesk.jsp

Click you Member Profile to view your question history and please keep them updated. If you are a KnowledgePro user, use the Power Search option to find them.  

Questions which are LOCKED with a Proposed Answer but do not help you, should be rejected with comments added.  When you grade the question less than an A, please comment as to why.  This helps all involved, as well as others who may access this item in the future.  PLEASE DO NOT AWARD POINTS TO ME.

To view your open questions, please click the following link(s) and keep them all current with updates.
http://www.experts-exchange.com/questions/Q.20285390.html
http://www.experts-exchange.com/questions/Q.20286611.html
http://www.experts-exchange.com/questions/Q.20286687.html
http://www.experts-exchange.com/questions/Q.20297629.html
http://www.experts-exchange.com/questions/Q.20300142.html
http://www.experts-exchange.com/questions/Q.20291025.html
http://www.experts-exchange.com/questions/Q.20301019.html



*****  E X P E R T S    P L E A S E  ******  Leave your closing recommendations.
If you are interested in the cleanup effort, please click this link
http://www.experts-exchange.com/jsp/qManageQuestion.jsp?ta=commspt&qid=20274643
POINTS FOR EXPERTS awaiting comments are listed in the link below
http://www.experts-exchange.com/commspt/Q.20277028.html
 
Moderators will finalize this question if in @7 days Asker has not responded.  This will be moved to the PAQ (Previously Asked Questions) at zero points, deleted or awarded.
 
Thanks everyone.
Moondancer
Moderator @ Experts Exchange
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

When trying to find the cause of a problem in VBA or VB6 it's often valuable to know what procedures were executed prior to the error. You can use the Call Stack for that but it is often inadequate because it may show procedures you aren't intereste…
Article by: Martin
Here are a few simple, working, games that you can use as-is or as the basis for your own games. Tic-Tac-Toe This is one of the simplest of all games.   The game allows for a choice of who goes first and keeps track of the number of wins for…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

759 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now