Link to home
Start Free TrialLog in
Avatar of M A
M AFlag for United States of America

asked on

Encrypt a column in Excel

I want to VBA code to encrypt the password/serial number saved in a column with a specific number.
for example if password is abc I will add a number to it I want to save it as abc+that number not the real password.
When I click on decrypt I want a popup to enter the number I added while saving.
Finally remove the number should give the correct passwords
Avatar of Roy Cox
Roy Cox
Flag of United Kingdom of Great Britain and Northern Ireland image

Just adding a number isn't going to make it that secure, if you always add 4 numbers then someone can spot the pattern If you don't how will Excel know the original number?
Avatar of M A

ASKER

Thanks for your reply.

Something like this will help?
https://tzamtzis.gr/2017/web-analytics/excel-function-md5-hashing-without-vba/
No. MD5 is a hashing algorithm, not an encrypting algorithm.  You can not un-hash a value back to its original string.

How strong do you need the encrypted text to be?
SOLUTION
Avatar of Roy Cox
Roy Cox
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Martin your code does not work. Can you explain ?
Gowflow
I updated the workbook.
I just downloaded and still same code same results !!!
Gowflow
Martin's workbook is working for me in Mac Excel 2016.
Thanks. In case anyone has a problem, this is how to use it.

  1. Click the Encrypt button
  2. It will ask you for a phrase which it will use to encrypt (actually hash) C4. The phrase can be anything from "xxx" to" I think Martin is a great programmer" (all without quotes)
  3. When you want to decrypt, press the Decrypt button and enter the phrase you used previously. It must be EXACTLY the same.
Avatar of M A

ASKER

Martin I found a bug.
When you click encrypt and click cancel it still encrypts the password.
Is it possible to adjust the VBA codes to cancel the encryption when I click cancel.
Sure.

Sub Encrypt()
Dim strPhrase As String

strPhrase = InputBox("Please enter the encryption phrase", "Encrypt Password")
If StrPtr(strPhrase) = 0 Then
    Exit Sub
End If

Range("C4") = RndCrypt(Range("C4"), strPhrase)
End Sub

Open in new window


Sub Decrypt()
Dim strPhrase As String

strPhrase = InputBox("Please enter the decryption phrase", "Decrypt Password")
If StrPtr(strPhrase) = 0 Then
    Exit Sub
End If

Range("C4") = RndCrypt(Range("C4"), strPhrase)
End Sub

Open in new window

Avatar of M A

ASKER

One more request
I want to encrypt or decrypt only selected cell.
As of now it is only encrypting  and decrypting C4 cell.
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of M A

ASKER

Thank you very much Martin
The last code when it comes to a Selection need to be amended as follows:

Sub Decrypt()
Dim strPhrase As String
Dim cCell As Range

strPhrase = InputBox("Please enter the decryption phrase", "Decrypt Password")
If StrPtr(strPhrase) = 0 Then
    Exit Sub
End If

For Each cCell In Selection
    cCell = RndCrypt(cCell, strPhrase)
Next cCell

End Sub

Open in new window


Sub Decrypt()
Dim strPhrase As String
Dim cCell As Range

strPhrase = InputBox("Please enter the decryption phrase", "Decrypt Password")
If StrPtr(strPhrase) = 0 Then
    Exit Sub
End If

For Each cCell In Selection
    cCell = RndCrypt(cCell, strPhrase)
Next cCell

End Sub

Open in new window


Gowflow
Avatar of M A

ASKER

There is a mistake I guess. Your help is appreciated.
If I type wrong phrase by mistake I cannot undo to type the correct phrase and get the correct password.
i.e. I lost the original password.  :(

Thank you very much for your time/help.
Do you have any kind of backup?
Avatar of M A

ASKER

No.
:(
But no problem I didn't loose any password which is not recoverable.
But it may happen in future.
You can use a password manager to save your passwords.