Link to home
Start Free TrialLog in
Avatar of Hideharu
HideharuFlag for United States of America

asked on

Encrypt/Decrypt Binary File using CAPICOM in VBA

I've been searching around for anything that can help me with this but turned up with nothing.
Does anyone know how to Encrypt/Decrypt binary files using Capicom from visual basic?  (the one feature they seemed to have left out is the one feature I want the most.  Easy to use File Encryption)

The attached code appears to work (and does work with simple text files) but the code I use to decrypt it does not.  This really tells me there's a very strong chance this code is not encrypting it properly.

Trying to decrypt it tells me "Error 8009310b: ASN1 bad tag value met."
Function Capicom_EncryptFile_Cert(strIn As String) As String
    Dim Enc As New EnvelopedData
    Dim inFile As Integer
    
    '  If the file doesn't exist, quit
    If Dir(strIn) = "" Then
        Capicom_EncryptFile_Cert = "File not found"
        Exit Function
    End If
    
    inFile = FreeFile
    
    '  Opens the file and loads it into "Enc" for encryption
    Open strIn For Binary As inFile
    Enc.Content = Input(LOF(inFile), inFile)
    Close inFile
    
    '  Creates a new blank file.  Don't know how to do it any other way.
    Set ObjFSO = CreateObject("Scripting.FileSystemObject")
    Set ObjFile = ObjFSO.CreateTextFile(strIn & ".encrypted")
    ObjFile.Close
    Set ObjFile = Nothing
    Set ObjFSO = Nothing
    
    '  Open the new blank file to print the encrypted data to.
    Open strIn & ".encrypted" For Output As #inFile
    Print #inFile, Enc.Encrypt(CAPICOM_ENCODE_BINARY)
    Close #inFile
    
    Capicom_EncryptFile_Cert = "File Encryption Complete.  New filename is:" & vbNewLine & strIn & ".encrypted"
End Function
 
Function Capicom_DecryptFile_Cert(strIn As String) As String
    Dim Enc As New EnvelopedData
    Dim inFile As Integer
    
    '  Same deal as above.
    If Dir(strIn) = "" Then
        Capicom_DecryptFile_Cert = "File not found"
        Exit Function
    '  Lazy.  Don't want to figure out if it's encrypted or not so quit if not named properly.
    ElseIf Replace(strIn, ".encrypted", "") = strIn Then
        Capicom_DecryptFile_Cert = "Rename encrypted file's extention to .encrypted in order to proceed."
        Exit Function
    End If
    
    inFile = FreeFile
    
    '  Open encrypted file to load into "Enc" to decrypt it.
    Open strIn For Binary As inFile
    Enc.Decrypt Input(LOF(inFile), inFile)
    '
    '  THIS IS WHERE IT FAILS.  It's telling me Error 8009310b: ASN1 bad tag value met.
    
    Close inFile
    
    Set ObjFSO = CreateObject("Scripting.FileSystemObject")
    Set ObjFile = ObjFSO.CreateTextFile(Replace(strIn, ".encrypted", ""))
    ObjFile.Close
    Set ObjFile = Nothing
    Set ObjFSO = Nothing
    
    Open Replace(strIn, ".encrypted", "") For Binary As #outFile
    Print #outFile, Enc.Content
    Close #outFile
    
    Capicom_DecryptFile_Cert = "File Encryption Complete.  New filename is:" & vbNewLine & Replace(strIn, ".encrypted", "")
End Function

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of puppydogbuddy
puppydogbuddy

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