[Webinar] Streamline your web hosting managementRegister Today

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 162
  • Last Modified:

Make a file readable by my app only

This might be an easy one, but I'm not sure. Even if it is I am in kind of a rush, that's why the 250 points.

I want my app to write data to a file that can't be read or viewed in any way other than reading back in with my app.
Maybe encryting or something simular.
My app has to do all this with code. I am using VB6.


Is this possible, and if it isn't can I come close.

I already thought of replacing every letter with a different character, kinda like a secret code or something, but I would rather not do it that way.

If this is possible, it will probably be new code to me, so details or examples will be nice.

Thank you!

istc
0
istc
Asked:
istc
  • 7
  • 6
1 Solution
 
kamallCommented:
This is a simple encryption method (from the MS knowledge base).

SUMMARY
=======

To restrict access by computer hackers, you can encrypt strings, such as
strings stored in your compiled executable .EXE program. Encryption helps
protect your program from unauthorized modifications.

The sample Visual Basic code below encrypts a string with the XOR operator
using password security. You can also adapt this technique to other
dialects of Basic or other languages.

MORE INFORMATION
================

Software tools for debugging and viewing binary code can easily find ASCII
strings stored in compiled executable .EXE programs. If you want to hide or
protect strings in .EXE programs, you can use techniques such as these:

 - Append a series of Chr$() functions in your Basic code. For example,

   the following string concatenates ASCII values 65 through 68, which
   represent the capital letters A through D:

      A$ = Chr$(65) + Chr$(66) + Chr$(67) + Chr$(68)
      Print A$

   This code prints ABCD. Since the ABCD is stored in code instead of in a
   string constant, it is not directly visible when viewed in a debugger.
   This method is okay for small amounts of data, but is inefficient for
   larger strings.

 - Add or subtract a constant or a letter's position value to the
   ASCII/ANSI value of each character in the string.

   NOTE: Don't exceed the byte value range of 0 to 255. For example, if you

   add 50 to the extended-ASCII character CHR$(230) and assign it to a
   string in Basic, you get an "Illegal function call" error.

 - Use the Xor function in a formula using a key or password string. Byte
   values changed with Xor always stay within the byte value range 0 to
   255. This technique is flexible and elegant. See the sample program in
   the next section below.

   NOTE: The Xor function is also known as the exclusive-OR function. An
   exclusive-OR means A or B, but not both. For example, if A is true,
   and B is false, then A Xor B is true, but if both A and B are true,
   then A Xor B is false.

 - Use third-party compression and encryption software, such as LZEXE or

   PKLITE, on the compiled executable EXE file. For secure encryption
   routines, see the QuickPak Professional Library from Crescent Software,
   Inc., shown in the Reference section below.

 - Keep a checksum to protect against viruses or hackers. You can keep a
   checksum of various important values in the program, and the program can
   refuse to run if any checksums have changed.

   NOTE: A checksum is an error-detection scheme that involves creating a
   sum of the bits in a set of bytes of data, then using that sum to later
   check for a change in the data.

Example of String Encryption Using Xor Function
-----------------------------------------------

Calling the Encrypt routine below encrypts a string using a password.

Calling the routine again decrypts the encrypted string.

1. Start a new project in Visual Basic. Form1 is created by default.

2. Add the following code to the Form Load event:

   Sub Form_Load ()
      form1.Show  ' Must Show form in Load event before Print is visible.
      secret$ = "This is the string that will be encrypted."
      PassWord$ = "password"

      Call Encrypt(secret$, PassWord$)     'Encrypt the string.
      Print " After encrypting it once: "  'Print the result.
      Print secret$
      Print

      Call Encrypt(secret$, PassWord$)  'A second encryption decrypts it.
      Print "After a second encryption: "
      Print secret$

   End Sub

3. Add the following Encrypt procedure to the general declarations section:

   Sub Encrypt (secret$, PassWord$)
      ' secret$ = the string you wish to encrypt or decrypt.
      ' PassWord$ = the password with which to encrypt the string.
      L = Len(PassWord$)
      For X = 1 To Len(secret$)
         Char = Asc(Mid$(PassWord$, (X Mod L) - L * ((X Mod L) = 0), 1))
         Mid$(secret$, X, 1) = Chr$(Asc(Mid$(secret$, X, 1)) Xor Char)
      Next
   End Sub

4. Start the program, or press the F5 key. Close the form to end the
   program.

Xor: The Exclusive-OR Operator

------------------------------

The exclusive-OR operator (Xor in the Basic language) performs a logical
exclusion on two expressions. For example:

   Result = expr1 Xor expr2

A useful behavior of Xor is that the first expression expr1 is returned
without losing any bits when you perform Result Xor expr2. This ability to
restore the first expression from the Result combined with the second
expression is why the Xor function is useful for encryption.

The Xor operator performs a bit-wise comparison of identically positioned
bits in two numeric expressions and sets the corresponding bit in the
result according to the following truth table:

   If bit in expr1 is:     And bit in expr2 is:    The result is:

   ---------------------------------------------------------------
         0                           0                    0
         0                           1                    1
         1                           0                    1
         1                           1                    0

ASCII and ANSI Character Sets
-----------------------------

For a listing of the ASCII and ANSI character sets, see the Help menu in
Visual Basic.

American Standard Code for Information Interchange (ASCII) is the 7-bit
character set widely used to represent letters and symbols found on a
standard United States keyboard. The ASCII character set is the same as the

first 128 characters (0 to 127) in the American National Standards
Institute (ANSI) character set. The ANSI character set uses all 8 bits in a
byte, and includes 256 characters (0 to 255). ANSI characters 128 to 255
are sometimes referred to as the extended-ASCII characters.
0
 
kamallCommented:
i.e. encryption by using the XOR function (in the above).
If you want a more powerful way of encryption, let me know.
0
 
Erick37Commented:
Visit www.planet-source-code.com with cookies turned on.
Do a search for "encryption" in the VB area.
There are lots of downloadable samples.
0
Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

 
istcAuthor Commented:
Wow! That was fast!

Thank you!

I'll start on it right now but it'll probably take me a day or two. I need to get this done as fast as I can though, so I'll be back soon.

Thanks again!

istc
0
 
kamallCommented:
istc,
I searched my system and found some sample VB projects for encryption using other methods. If you like, I can e-mail them to you. Just comment me your e-mail address.
0
 
istcAuthor Commented:
kamall,
You don't happen to live in Idaho USA do you.
Just curious because we have a mall here named Karcher mall, and you name almost could abbreviate it....:-)
Anyway, I really like the Xor example because it is pretty simple and it's plenty good enough. It works good too, as it is in the example, but I can't read it back in from a file. I've tried and tried, and I can print it to the file just fine, but I can't read it back in again. I'm not sure why. I tried Input #1, Line Input #1, and Binary, but it will only read the first 2 characters, and that's it.
If you want to try and figure it out, I still might use it.
I have 2 buttons on a form: cmdEncrypt and cmdDecrypt and this is all the code in the form:
("c:\altemp\Passwords.txt" is the path and filename it is writing to so you will have to change that part if you try this)
(Also more after the code)
***********************************************************************************
Option Explicit
Sub Encrypt(secret, Password)
Dim L As Integer
Dim x As Integer
Dim Char


' secret = the string you wish to encrypt or decrypt.
' PassWord = the password with which to encrypt the string.
L = Len(Password)
    For x = 1 To Len(secret)
          Char = Asc(Mid$(Password, (x Mod L) - L * ((x Mod L) = 0), 1))
          Mid$(secret, x, 1) = Chr$(Asc(Mid$(secret, x, 1)) Xor Char)
    Next

End Sub

Private Sub cmdDecrypt_Click()
Dim Password As String
Dim secret As String
    Password = "password"

    Open "c:\altemp\Passwords.txt" For Input As #1
      Input #1, secret
    Close #1
     
      Call Encrypt(secret, Password)  'A second encryption decrypts it.
      MsgBox secret
     
     
End Sub

Private Sub cmdEncrypt_Click()
Dim Password As String
Dim secret As String
    secret = "This is the string that will be encrypted."
   
   
   
    Password = "password"
 If Dir$("c:\altemp\Passwords.txt") <> "" Then Kill "c:\altemp\Passwords.txt"
 
      Call Encrypt(secret, Password)     'Encrypt the string.
    Open "c:\altemp\Passwords.txt" For Append As #1
      Print #1, secret & Format(Str$(Len(secret)), "00")
    Close #1
   
End Sub


So I am definately interested in anything else you can give me too. My e-mail is:
ahammar@cyberhighway.net

Hi Erick37,
Thanks for the URL. I will check that out too.

Thanks again!
istc
0
 
istcAuthor Commented:
Oh! One more thing. A password is not necessary if that makes it easier. Maybe it is required in the code, but I don't know, because I'm not exactly sure what it's doing, but just so you know, it makes no difference just in case you do try and figure it out.

Thanks!
istc
0
 
kamallCommented:
istc,
You must open the file in binary mode and use Get and Put to read/write data. Here is the code. Just requires one form and one command button. I also e-mailed you the project.
Regards.

CODE
--------------
Dim MyString As String

Private Sub Encrypt(secret$, PassWord$)
   
    ' secret$ = the string you wish to encrypt or decrypt.
    ' PassWord$ = the password with which to encrypt the string.
    L = Len(PassWord$)
    For X = 1 To Len(secret$)
       Char = Asc(Mid$(PassWord$, (X Mod L) - L * ((X Mod L) = 0), 1))
       Mid$(secret$, X, 1) = Chr$(Asc(Mid$(secret$, X, 1)) Xor Char)
    Next

End Sub

Private Sub Command1_Click()
   
    MyFile = "c:\encrypt.txt"
   
    'Form1.Show  ' Must Show form in Load event before Print is visible.
    secret$ = "This is the string that will be encrypted."
    PassWord$ = "password"

    Call Encrypt(secret$, PassWord$)     'Encrypt the string.
    Print " After encrypting it once: "  'Print the result.
    Print secret$
    Print
   
    'write to file
    Open MyFile For Binary Access Write As #1
        Put #1, , secret$
    Close 1


    Call Encrypt(secret$, PassWord$)  'A second encryption decrypts it.
    Print "After a second encryption: "
    Print secret$

    'read from file
    Open MyFile For Binary Access Read As #1
        MyString = String(FileLen(MyFile), " ")
        Get #1, , MyString
    Close 1
    Call Encrypt(MyString, PassWord$)  'A second encryption decrypts it.
    MsgBox "This is from the file: " & vbCrLf & MyString

End Sub
0
 
istcAuthor Commented:
Hi!
Well, I got my app to work finally. I actually found some code that I used on the site that Erick37 told me to check out, that worked out really well. It was a little longer than I wanted, but it works very good and it was easy to impliment into my project.


Kamall
I don't know if you got my e-mail that I sent you, but I got your code to work. I couldn't figure out how to make it add to the file to make a list of records instead of overwriting it each time, but it was perfect for something else I was doing, so I still used the code you supplied, but in a completely different application. It worked perfectly for that.

So, if there is no quarrel, I would like to split the points between the 2 of you.
I'll ask to have this question deleted, and give you each 125 points with an Excellent grade. I am real thankful to both of you for the help, and at how fast you were.

Is that Ok?

Cheers!
istc
0
 
kamallCommented:
istc,
I've been away for a couple of days. Sorry for the delay.
Yes, I've just saw your e-mail.
Do you still have problem, i.e the problem you mentioned in your e-mail or is everything OK?
Regards.
0
 
istcAuthor Commented:
Hi Kamall,
I got it working fine now. The situation is still as it was in my last comment ie...I couldn't figure out how to append to the file instead of overwrite it each time. I couldn't find anything in the help files, so I checked the website that Erick37 posted and found something that worked good with strong encryption. I did use your code in a different project though that I needed encryption for also, but didn't require that I append to it, so yours worked good for that!

That's why I wanted to make sure there was no quarrel if I split the points between the 2 of you.

Thanks!
istc
0
 
kamallCommented:
Hi istc,
I am glad that you did your job.
There is no problem on the points. However, Erick37 had just suggested a link to search in, while I gave a real working code.
It is up to you the way you split the points.
Regards.
0
 
istcAuthor Commented:
Here's what I'll do.
All the help was well worth it to me, so I will give Kamall these points, and I will post another question for Erick37 for 150 points (that's all I got left or I'd give you more). I doubt that I'll need any for awhile so they should build back up again by the time I need any.

Thank you both very very much!!
istc
0
 
kamallCommented:
istc,
Thank you very much for being so kind.
I greatly appreciate it.
Just one thing: I think that you can ask the EE staff (Linda) for some points. Explain her the issue and she will give you some additional points. You can do that by posting her a free question in the Customer support topic area. There were many such instances in the past.
Best regards.
0

Featured Post

The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

  • 7
  • 6
Tackle projects and never again get stuck behind a technical roadblock.
Join Now