Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Read Encrypted Text File 1

Status
Not open for further replies.

iwells

Programmer
Oct 2, 2012
284
0
0
CA
Hi,

Not sure if this is the right area, but I'm trying to write some code to open/read from an encrypted text file. So far I have:

Option Explicit

'File Stream Objects
Private objFSO As Object
Private objPasswordFile As Object

'Strings

Public strAdminPassword As String
Private Const strFilePath As String = "My Dir"

Public Sub GetAdminPassword()



Set objFSO = CreateObject("Scripting.FileSystemObject")

If objFSO.FileExists(strFilePath) Then

Set objPasswordFile = objFSO.OpenTextFile(strFilePath, 1)
strAdminPassword = objPasswordFile.ReadLine
objPasswordFile.Close
Else

MsgBox "File Error", vbOKOnly, "Error"
End
End If

Set objFSO = Nothing
Set objPasswordFile = Nothing
End Sub

This code works fine since the only line/item in the file is the password, but now I'd like to add some encryption to this ... I'm wondering if someone can assist with a link or something since I'm unable to find anything related to this.

The reason for this is unfortunately we have many separate macros (sage VBA) which require the admin login and password and we've had to hardcode this in hundreds of spots and I'm trying to build a deployment structure so the file can be overwritten as need to change the password and remove the hardcoding from the macro, but the users would have access to the file location as well so we can't have them opening it and seeing the admin password.
 
The only thing you have to know is that there are no rules to encryption. There are loads of encryption methods. Presumably you need 2 way encryption.

You could use one of the common methods like DES, AES, blowfish, RSA or make up your own. You need a 2-way one. Some of these have a public and private key, which you will need to store somewhere to encrypt/decrypt. How you encrypt stuff is entirely up to you - remember - there are no rules.

Depends how knowledgeable your people are. If the people are non-programmers (they know about every single icon/menu option in excel but nothing about VBA or any form of programming), you could use a simple Caesar cipher. You don't have to say it is a Caesar cipher - you could say it is RSA or something like that (there are no rules - you can lie about it). You don't have to worry if an article says that someone has managed to crack the RSA cipher in 3 hours - yours is just a Caesar cipher which everyone thinks is RSA.

Just have your encrypted password in say characters 10 to 17 and the rest of it can be anything you like. It doesn't even have to be sequential.

Another method, you can have the password in plain sight - just jumble the characters around. The one in the password file can be

Code:
thefiveboxingwizardsjumpquickly
123456789012345678901234567890

It doesn't even have to be in English. Say it is a 5 letter password - characters 17, 8, 28, 19, 3 - abcde. Your code can just pick out those characters for the password in your code. They don't have to be unique and you can pick the same letter as many times as you like. It doesn't have to be a mathematical algorithm (like RSA etc).

Of course, the encryption specialists (yes - there are such people, I once worked in a department of 50 with 20 encryption specialists) will tell you that to have your password in plain sight is a bad idea but given that that is what is in your file, and nobody knows which characters you are picking, how would anyone know what the password is other than it is made up of some of those letters. They don't even know how long it is.
 
>make up your own

Oh, let's not, please

And the whole point of public key encryption is that the public key can quite happily be in plain sight.
 
Here is a thread with code examples illustrating hashing and encryption: thread222-535644

Note that for a long time Windows also shipped with an ActiveX dll, CAPICOM, which made this all rather easy. Sadly more recent versions have deprecated it in favour of .Nets cryptography assemblies. However, it IS still downloadable from Microsoft and my example of how to use that can be found here: thread222-1548222
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top