We help IT Professionals succeed at work.

Word VBA Code to decrypt jpeg image

AlixTech
AlixTech asked
on
483 Views
Last Modified: 2012-05-11
Dear Expert,
I need a Word VBA code which upon running will access the (V:) network drive, pick a specified TrueCrypt encrypted jpg file, use the Key which is contained in the same primary folder, and return the decrypted image.
How do I write this code?
Thanks
Comment
Watch Question

aikimarkSocial distance; Wear a mask; Don't touch your face; Wash your hands for 20 seconds
CERTIFIED EXPERT
Top Expert 2014

Commented:
In order to mount a TC volume, you would have to have already supplied a password.  If you already have a V: volume, your VBA code shouldn't have to do anything but access the file.
btanExec Consultant
CERTIFIED EXPERT
Distinguished Expert 2019

Commented:
truecrypt has commandline option to input the password, state the container volume file  to be mounte. and als oexample of usag. putting into vba would be just executing the truecryp twithe keypath loc

http://www.truecrypt.org/docs/?s=command-line-usage

Mount a volume called myvolume .tc using the password MyPassword, as the drive letter X . TrueCrypt will open an explorer window and beep ; mounting will be automatic : truecrypt /v myvolume .tc /lx /a /p MyPassword /e /b

Author

Commented:
Thanks Breadtan:

So, what is the correct syntax for the usage of this volume mounting in Word VBA? Is it just:

truecrypt /v myvolume.tc /lx /a /p MyPassword /e /b

Also, how do I then call the jpg image to be decrypted?

And then I assume that I use:
truecrypt /q /dx
to dismount the volume after the jpeg is decrypted?

Thanks for your help.


aikimarkSocial distance; Wear a mask; Don't touch your face; Wash your hands for 20 seconds
CERTIFIED EXPERT
Top Expert 2014

Commented:
@AlixTech

Here is the TC command line document page:
http://www.truecrypt.org/docs/?s=command-line-usage

Your /lx parameter should probably be /lv if you want a V: drive.

You probably want to add a /s parameter to enable silent operation.

========
So you need to automate the mounting of a TC volume as well as extract the file.

You could create a batch file to do all of this, feeding the password and file name as parameters.  All three commands (mount, copy, dismount) would be in the command file.  That would make the VBA Shell command very simple.

After shelling the batch file, your VBA code would enter a sleep/wait loop until the file was copied.

Alternatively, you could chain the three commands together on a single line along with the launching of the command shell.

Alternatively, you could use Wscript/Cscript to execute the three commands, possibly capturing the success/error messages.
btanExec Consultant
CERTIFIED EXPERT
Distinguished Expert 2019

Commented:
for vb running exe, you can try shell(). see the link below:

http://msdn.microsoft.com/en-us/library/xe736fyk(v=vs.71).aspx

as for truecrypt cmdline, just to add on, consider the options below:

Specifies a keyfile or a keyfile search path eg: /k c:\\keyfile 1.dat

/dismount or /d Dismount volume specified by drive letter (e. g ., /d x ). When no drive letter is specified , dismounts all currently mounted TrueCrypt volumes . /force or /f Forces dismount (if the volume to be dismounted contains files being used by the system or an application ) and forces mounting in shared mode (i.e ., without exclusive access ).

/silent or /s If /q is specified , suppresses interaction with the user (prompts , error messages , warnings , etc . ). If /q is not specified , this option has no effect.

/wipecache or /w Wipes any passwords cached in the driver memory. /password or /p The volume password. If the password contains spaces, it must be enclosed in quotation marks (e.g ., /p "My Password").

Author

Commented:
Yes, I could really use some help.

Thanks!
aikimarkSocial distance; Wear a mask; Don't touch your face; Wash your hands for 20 seconds
CERTIFIED EXPERT
Top Expert 2014

Commented:
@AlixTech

Please post the code, in its current state, and let us know what error messages you are seeing.

Author

Commented:
Hi Aikimark,

My first problem is that I am not sure of the correct format to use and I receive format error messages when I insert:

Dim ProcID As Object
    ProcID = Shell("C:\Windows\system32\cmd.exe", AppWinStyle.NormalFocus, Shell("C:\Documents and Settings\asmith.ALERA\Desktop>"c:\program files\truecrypt\truecrypt.exe" /volume "\\fileserver\signatures$\Volume\Signatures" /letter V: /beep /keyfile "\\fileserver\signatures$\Keyfile" /password abc123/q)

Once I get this portion of code to work, I was hoping that it would allow me to access the Excel spread sheet in theV drive in the standard way that I currently have the VBA code written. Is this a correct assumption or do I need to something different here to?

Thanks for the help.
aikimarkSocial distance; Wear a mask; Don't touch your face; Wash your hands for 20 seconds
CERTIFIED EXPERT
Top Expert 2014

Commented:
try the following:

Dim ProcID As Long
    ProcID = Shell(chr(34) & "c:\program files\truecrypt\truecrypt.exe" & chr(34) & " /volume " & chr(34) &  "\\fileserver\signatures$\Volume\Signatures" & chr(34) & " /letter V: /beep /keyfile " & chr(34) & "\\fileserver\signatures$\Keyfile" & chr(34) & " /password abc123/q", vbNormalFocus)

Open in new window

Author

Commented:
Hi Aikimark,

Thank you for the code...
So now it will start the TruCrypt window, but it does not mount the V: drive. So my very next code after yours which should mount the spread sheet is:

Set xlApp = CreateObject("excel.application")
Set xlWB = xlApp.Workbooks.Open("V:\TechDB.xls")

But, because the V drive is not mounted, it does not see the spread sheet in the drive.

Also, how do I dismount the V drive after it obtains the information from the spread sheet?

Thanks
aikimarkSocial distance; Wear a mask; Don't touch your face; Wash your hands for 20 seconds
CERTIFIED EXPERT
Top Expert 2014

Commented:
Since Shell() is an asynchronous function, your code will need to wait until the V: drive has been mounted before it can start looking for the file.  I've placed code in the snippet for waiting.

We need to make sure that the command is working.  Break on the Shell() statement and copy/paste the first parameter (the command to execute) in the Immediate window to see what is actually being created.  You will need to prepend a question mark before the text you paste to see what is going to be shelled.  
Copy the resulting string in the Immediate window to the clipboard.
Open a command window and paste the clipboard contents and press Enter.  You should see messages about the attempt to mount the TrueCrypt volume as well as give you some idea of the time required to mount the volume.

=====
Once you are sure that the command will mount the volume, place a Stop statement after the Shell function statement.  When you run your code, it will break on the stop statement.  You can look at MyComputer and Task Manager to see what is happening.
Public Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)

On Error Resume Next
Do
  Sleep 1000
  DoEvents
Loop Until Len(Dir("V:\TechDB.xls"))<>0

Open in new window

Author

Commented:
Hi Aikimark:

You are way beyond my understanding of how to write the code you are recommending I try. Please see the actual code attached for this function. Please show me exactly what I need to insert into the code, and I'll try it.
Thank you very much.
Function secretSquirrel1()
    Dim strTry As String
    Dim intTry As Integer
    Dim strPassword1 As String
    Dim bolSuccess As Boolean
    Dim strMessage As String
    Dim xlApp As Object
    Dim xlWB As Object
    Dim xlws As Object
    Dim xlRange As Object
    Const bolCaseSensitive = True
    Const intTries As Integer = 4
    
    Dim ProcID As Long
    ProcID = Shell(Chr(34) & "c:\program files\truecrypt\truecrypt.exe" & Chr(34) & " /volume " & Chr(34) & "\\fileserver\signatures$\Volume\Signatures" & Chr(34) & " /letter V: /beep /keyfile " & Chr(34) & "\\fileserver\signatures$\Keyfile" & Chr(34) & " /password ABC123/q", vbNormalFocus)
    
    Set xlApp = CreateObject("excel.application")
    Set xlWB = xlApp.Workbooks.Open("V:\AlixTechDB1.xls")
    Set xlws = xlWB.Worksheets(4)
    On Error Resume Next
    Set xlRange = xlws.Columns(4).Find(Environ("Username"))
    If xlRange Is Nothing Then
        xlWB.Close False
        xlApp.Quit
        Exit Function
    End If
    strPassword1 = xlRange.Offset(, 2)
    xlWB.Close False
    xlApp.Quit
    If strPassword1 = "" Then Exit Function
    strMessage = "Enter Password #1 - Attempt 1"
    intTry = 1
    Do While intTry < intTries And Not bolSuccess
        strTry = vbInputBox(strMessage, "Enter Password #", "")
        If Not bolCaseSensitive Then
            If UCase(strTry) = UCase(strPassword1) Then bolSuccess = True
        Else
            If strTry = strPassword1 Then bolSuccess = True
        End If
        intTry = intTry + 1
        strMessage = "Incorrect Password Entered" & vbCrLf & vbCrLf & "Enter Password #1 - Attempt " & intTry
    Loop
    secretSquirrel1 = bolSuccess
End Function

Open in new window

aikimarkSocial distance; Wear a mask; Don't touch your face; Wash your hands for 20 seconds
CERTIFIED EXPERT
Top Expert 2014

Commented:
1. Set a breakpoint on line 15
2. Run your program
3. When the program stops at line 15, copy the first parameter of the Shell function to the clipboard.
4. Press Ctrl+G  (this will open the Immediate window, if not already open, and place the cursor into the window)
5. type a question mark and press Ctrl+V
This should look something like this:
?Chr(34) & "c:\program files\truecrypt\truecrypt.exe" & Chr(34) & " /volume " & Chr(34) & "\\fileserver\signatures$\Volume\Signatures" & Chr(34) & " /letter V: /beep /keyfile " & Chr(34) & "\\fileserver\signatures$\Keyfile" & Chr(34) & " /password ABC123/q"

Open in new window


6. press Enter
7. press F8  (this will cause the Shell() statement to execute
8. open a command prompt window
9. Enter the following command
Dir V:\*.*

Open in new window

10. try step 9 a few times.  If it succeeds, your Shell statement will have mounted the TC volume.  You can expect some error messages until the volume is mounted.

=========
If the volume does not mount within a few minutes, switch back to the Immediate window.
11. Select and Copy the text produced after the question mark statement to the clipboard.
12. Switch back to the command prompt window
13. right click on cursor and select Paste.
14. press Enter

You will then see any error messages produced by the failed TC volume mount from the Shell function.  It is quite possible that there is something wrong with the command.

Author

Commented:
Hi,
One more question... How do I put the Break in Line 15?
aikimarkSocial distance; Wear a mask; Don't touch your face; Wash your hands for 20 seconds
CERTIFIED EXPERT
Top Expert 2014

Commented:
click the grey area to the left of the line

or...place the cursor on the line and press F9

Author

Commented:
Hi Aikimark,

Thank you again for the help, I really do appreciate it.
OK - So I run through all the steps, and all work, but the Volume does not mount.

If, however, I click the MS-DOS Batch File command button (C:\WINDOWS\system32\cmd.exe) to mount the V: drive on my desktop, it does mount the drive. Its command line reads:

C:\Documents and Settings\mkralik.ALERA\Desktop>"c:\program files\truecrypt\truecrypt.exe" /volume "\\fileserver\signatures$\Volume\Signtures" /letter V: /beep /keyfile "\\fileserver\signatures$\Keyfile" /password ABC123 /q

I am sure I am missing something simple, just not sure what it is.
Thanks    
aikimarkSocial distance; Wear a mask; Don't touch your face; Wash your hands for 20 seconds
CERTIFIED EXPERT
Top Expert 2014

Commented:
so, you started a command prompt and pasted the string you copied from the immediate window?  (that is the string you posted in the comment above http:#35738056 ?)

Author

Commented:
No, this command was setup on my desktop by the contracted IT company that installed and setup the V: drive and Truecrypt.
aikimarkSocial distance; Wear a mask; Don't touch your face; Wash your hands for 20 seconds
CERTIFIED EXPERT
Top Expert 2014

Commented:
\Signtures is misspelled in your code.  It should be \Signatures

Author

Commented:
Sorry, that is just my error, it is correct in the code.
aikimarkSocial distance; Wear a mask; Don't touch your face; Wash your hands for 20 seconds
CERTIFIED EXPERT
Top Expert 2014

Commented:
what text appears in the Immediate window?

Author

Commented:
When I run the code process as you have asked me to try, the code stops at the line 15, the command starts the TruCrypt, but  never mounts the volume, even when I try initiating the command a few times. But, if I click on the command located on the desktop, it activates.
aikimarkSocial distance; Wear a mask; Don't touch your face; Wash your hands for 20 seconds
CERTIFIED EXPERT
Top Expert 2014

Commented:
@AlixTech

Please follow the instructions and post the text from the result of executing the ? line

Author

Commented:
When I hit Enter after entering the ? line, I get:

"c:\program files\truecrypt\truecrypt.exe" /volume "\\fileserver\signatures$\Volume\Signatures" /letter V: /beep /keyfile "\\fileserver\signatures$\Keyfile" /password ABC123/q

Then when I hit F8, it jumps to the next line in the code, whcih is:

Set xlApp = CreateObject("excel.application")

and stops.
aikimarkSocial distance; Wear a mask; Don't touch your face; Wash your hands for 20 seconds
CERTIFIED EXPERT
Top Expert 2014

Commented:
oops.  Reverse what I said in http:#35739023

Your code should be \\fileserver\signatures$\Volume\Signtures

It is currently producing \\fileserver\signatures$\Volume\Signatures

Author

Commented:
No, I think you are correct, I did misspell Signatures.
aikimarkSocial distance; Wear a mask; Don't touch your face; Wash your hands for 20 seconds
CERTIFIED EXPERT
Top Expert 2014

Commented:
what you said that worked at the command prompt (or shortcut) is not the same as that produced by your code.

Author

Commented:
That is true, because I typed out what the code shows in the window, which is why the error in spelling "Signatures" occurred.
aikimarkSocial distance; Wear a mask; Don't touch your face; Wash your hands for 20 seconds
CERTIFIED EXPERT
Top Expert 2014

Commented:
What is in your shortcut?

I'm on my way to a user group meeting, so I'll be back in a few hours.

Author

Commented:
My Shortcut is labeled:
mount signatures volume.bat

And when I click on it, it mounts the TrueCrypt volume. If I click on it again, a message box appears which reads:

---------------------------
TrueCrypt
---------------------------
The volume you are trying to mount is already mounted.
---------------------------
OK  
---------------------------

- and the command box appears with the following command inside:

C:\Documents and Settings\mkralik.ALERA\Desktop>"c:\program files\truecrypt\truecrypt.exe" /volume "\\fileserver\signatures$\Volume\Signatures" /letter V: /beep /keyfile "\\fileserver\signatures$\Keyfile" /password ABC123 /q



aikimarkSocial distance; Wear a mask; Don't touch your face; Wash your hands for 20 seconds
CERTIFIED EXPERT
Top Expert 2014

Commented:
A prior TC command has already mounted the volume.  Unmount it and start again.

You and I have posted a couple of formats of the Shell() parameter.  You must resolve which of the two formats is the correct one.  I would expect that the shortcut is the gold standard.  Then, you must make your code produce that exact same string as the first parameter in the Shell() function.

Author

Commented:
Hi Aikimark,

I got it to work, the problem was that I needed a space after the password before the /q. I just missed it because I copied the code written in http:#35724183. Totally my fault. Thanks for being patient with me.

So now that we know the code works, how do I dismount the volume?

Thanks,
Michael

Author

Commented:
I was told that the script to dismount the volume was something like:

c:\program files\TrueCrypt>TrueCrypt.exe/dismount V: /q

If this is correct, how do I write the format for it to function?

Thanks!
Social distance; Wear a mask; Don't touch your face; Wash your hands for 20 seconds
CERTIFIED EXPERT
Top Expert 2014
Commented:
Unlock this solution and get a sample of our free trial.
(No credit card required)
UNLOCK SOLUTION

Author

Commented:
It Works Great!!!!!

Thank you so very, very much for all the patient help!
Unlock the solution to this question.
Thanks for using Experts Exchange.

Please provide your email to receive a sample view!

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.