Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

Word VBA Code to decrypt jpeg image

Posted on 2011-04-18
37
Medium Priority
?
467 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
0
Comment
Question by:AlixTech
  • 17
  • 16
  • 2
35 Comments
 
LVL 46

Expert Comment

by:aikimark
ID: 35424758
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.
0
 
LVL 65

Expert Comment

by:btan
ID: 35425355
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
0
 

Author Comment

by:AlixTech
ID: 35425700
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.


0
When ransomware hits your clients, what do you do?

MSPs: Endpoint security isn’t enough to prevent ransomware.
As the impact and severity of crypto ransomware attacks has grown, Webroot has fought back, not just by building a next-gen endpoint solution capable of preventing ransomware attacks but also by being a thought leader.

 
LVL 46

Expert Comment

by:aikimark
ID: 35426321
@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.
0
 
LVL 65

Expert Comment

by:btan
ID: 35429525
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").
0
 

Author Comment

by:AlixTech
ID: 35693834
Yes, I could really use some help.

Thanks!
0
 
LVL 46

Expert Comment

by:aikimark
ID: 35694414
@AlixTech

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

Author Comment

by:AlixTech
ID: 35698342
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.
0
 
LVL 46

Expert Comment

by:aikimark
ID: 35699857
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

0
 

Author Comment

by:AlixTech
ID: 35706199
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
0
 
LVL 46

Expert Comment

by:aikimark
ID: 35706417
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

0
 

Author Comment

by:AlixTech
ID: 35723765
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

0
 
LVL 46

Expert Comment

by:aikimark
ID: 35724183
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.
0
 

Author Comment

by:AlixTech
ID: 35732969
Hi,
One more question... How do I put the Break in Line 15?
0
 
LVL 46

Expert Comment

by:aikimark
ID: 35733884
click the grey area to the left of the line

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

Author Comment

by:AlixTech
ID: 35738056
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    
0
 
LVL 46

Expert Comment

by:aikimark
ID: 35738338
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 ?)
0
 

Author Comment

by:AlixTech
ID: 35738637
No, this command was setup on my desktop by the contracted IT company that installed and setup the V: drive and Truecrypt.
0
 
LVL 46

Expert Comment

by:aikimark
ID: 35739023
\Signtures is misspelled in your code.  It should be \Signatures
0
 

Author Comment

by:AlixTech
ID: 35739265
Sorry, that is just my error, it is correct in the code.
0
 
LVL 46

Expert Comment

by:aikimark
ID: 35739295
what text appears in the Immediate window?
0
 

Author Comment

by:AlixTech
ID: 35739421
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.
0
 
LVL 46

Expert Comment

by:aikimark
ID: 35740077
@AlixTech

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

Author Comment

by:AlixTech
ID: 35740122
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.
0
 
LVL 46

Expert Comment

by:aikimark
ID: 35740626
oops.  Reverse what I said in http:#35739023

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

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

Author Comment

by:AlixTech
ID: 35741343
No, I think you are correct, I did misspell Signatures.
0
 
LVL 46

Expert Comment

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

Author Comment

by:AlixTech
ID: 35741479
That is true, because I typed out what the code shows in the window, which is why the error in spelling "Signatures" occurred.
0
 
LVL 46

Expert Comment

by:aikimark
ID: 35741908
What is in your shortcut?

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

Author Comment

by:AlixTech
ID: 35742341
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



0
 
LVL 46

Expert Comment

by:aikimark
ID: 35742503
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.
0
 

Author Comment

by:AlixTech
ID: 35746376
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
0
 

Author Comment

by:AlixTech
ID: 35746778
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!
0
 
LVL 46

Accepted Solution

by:
aikimark earned 2000 total points
ID: 35747081
It will probably look something like this:

ProcID = Shell(Chr(34) & "c:\program files\truecrypt\truecrypt.exe" & Chr(34) & " /dismount V: /q", vbNormalFocus)

Open in new window

0
 

Author Closing Comment

by:AlixTech
ID: 35747361
It Works Great!!!!!

Thank you so very, very much for all the patient help!
0

Featured Post

Threat Trends for MSPs to Watch

See the findings.
Despite its humble beginnings, phishing has come a long way since those first crudely constructed emails. Today, phishing sites can appear and disappear in the length of a coffee break, and it takes more than a little know-how to keep your clients secure.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
When you put your credit card number into a website for an online transaction, surely you know to look for signs of a secure website such as the padlock icon in the web browser or the green address bar.  This is one way to protect yourself from oth…
This video shows where to find the word count, how to display it, and what it breaks down to in Microsoft Word.
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…
Suggested Courses

575 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