Link to home
Start Free TrialLog in
Avatar of Altaf Patni
Altaf PatniFlag for India

asked on

how to encrypt csv file with password


Hi
After create a csv file want to encrypt it with password protection
please help
Avatar of Arty K
Arty K
Flag of Kazakhstan image

You may take a look at this project: http://sourceforge.net/projects/vbcorlib/
You can download that .DLL, then use it from VB6 to access .NET functions.

Read (though there are no direct VB6 examples):

http://social.msdn.microsoft.com/forums/en-US/vblanguage/thread/cd282eb8-4146-453e-80d3-2810689fe41a
http://msdn.microsoft.com/en-us/magazine/cc301566.aspx
http://support.microsoft.com/kb/317535

Hope this helps.
Avatar of Altaf Patni

ASKER

i am looking for encrypt and dec using vb6.0

Hello! Use capicom for that, here you have a example project that do what you want:
 CAPICOM-EncDec.zip

If you have a reference error to capicom.dll, add a new reference to the project (Project > References...) and look for capicom.dll in your system32 folder (ex: C:\Windows\System32\capicom.dll)
You should mention who receives the csv file and how they will open it. If it is to be opened by your own application, there are many options. But, if it needs to be opened via Excel then you may be out of luck.
@BrianVSoft:
file sent from client to server
and it is not gonna open via excel
automation process running on server
application opened it read it and transfer data from this file to the database. and then delete it.
purpose to encrypt it to make secure data from theft.

@Nopius:
server application is in vb6,
so requirement is to create any function in vb6. and add in same single vb6 application.
thats why i need this function in vb6.0
We use a VB6 Encr/Decrypt function (shareware) provided by the VS20 Group. (College Graduate mentors). I could paste it here if your data is alpha-numeric (confined to the Ascii range 9 to 126)
I doubt it would work outside that range..
Also, if your data is app to app, you should avoid CSV and use TAB delimited.. VB6 has some nasty bugs reading CSV files that include the "inch symbol".
Why dont test my solution?
https://www.experts-exchange.com/questions/26973418/how-to-encrypt-csv-file-with-password.html?cid=1573&anchorAnswerId=35453424#a35453424

My strong recommendation is to use capicom api, it comes with windows, so you dont have to install any other software in the client/server machine, it is easy to implement, more info:
http://msdn.microsoft.com/en-us/library/ms995332.aspx

@BrianVSoft:
Thanks for information,
but this application running successfully at both side (client and server).
from 2002 to till date, so i think no need to change from CSV to TAB delimited,

please check attached data like this in csv file
And divide column by "colon ; "
please check
303458;N/A;25-Apr-11;0;0;ABCD EFGHI JKLMNO;PQRSTU VWXY;USA;JAPAN;5;PROCESSOR;264;7;1848.00;0;CASH;0.00;0.00;0.00;0.00;0.00;1848.00;161*5;104370.00;FAST TRACK;0;N/A;0;0;WADI (M.H.);0;0;0;20-Oct-1976;0.00;BABLU;0;0;20-Oct-1976;264;1848.00;0;0;0;0;0;CAR;WEIGHT;0;0;TRANSPORTER;0.00;0.00;N/A;0;415263789012;742398631205

Open in new window

Did you check the CAPICOM link mentioned above?
You can try the shareware code attached from the VS20 Group..
It doesn't use a password though..
Function Decript4(ByVal TxtIn As String) As String
 ' Shareware by VSoft Systems.. www.Vs2020.com (VS20 Group)
 'Eg.  X2$ = Encript4(X1$)
 '     X3$ = Decript4(X2$)

  Dim Cnt As Integer, Cnt2 As Integer, Key2 As Integer
  Dim c7 As Integer, c1 As Integer, u As Integer, SC2 As Double
  Dim TxtOut As String
  
  Cnt = 0: Cnt2 = 0: Key2 = 0: TxtOut = ""
  For u = 1 To Len(TxtIn)
    c7 = Asc(Mid$(TxtIn, u, 1)) - 32
    If c7 >= 0 And c7 <= 94 Then
      c1 = (c7 - Key2) Mod 95
      If c1 < 0 Then c1 = c1 + 95
    Else
      c1 = c7
    End If
    TxtOut = TxtOut & Chr$(c1 + 32)
    Cnt = (Cnt + 1) Mod 37: Cnt2 = (Cnt2 + 1) Mod 53
    SC2 = (Sin(Cnt2 * 2.2) + 2) * 10 '2.4
    Key2 = ((Key2 + c1 + Cnt) * 7 + Int(SC2)) Mod 95
  Next u
  Decript4 = TxtOut

End Function

Function Encript4(ByVal TxtIn As String) As String
  ' Shareware by VSoft Systems.. www.Vs2020.com (VS20 Group)
  Dim Cnt As Integer, Cnt2 As Integer, Key2 As Integer
  Dim c7 As Integer, c1 As Integer, u As Integer, SC2 As Double
  Dim TxtOut As String, t1 As String
  
  Cnt = 0: Cnt2 = 0: Key2 = 0: TxtOut = ""
  For u = 1 To Len(TxtIn)
    t1 = Mid$(TxtIn, u, 1)
    c7 = Asc(t1) - 32
    If c7 >= 0 And c7 <= 94 Then c1 = (c7 + Key2) Mod 95 Else c1 = c7
    TxtOut = TxtOut & Chr$(c1 + 32)
    Cnt = (Cnt + 1) Mod 37: Cnt2 = (Cnt2 + 1) Mod 53
    SC2 = (Sin(Cnt2 * 2.2) + 2) * 10
    Key2 = (((Key2 + c7 + Cnt) * 7) + Int(SC2)) Mod 95
  Next u
  Encript4 = TxtOut

End Function

Open in new window

Simple Encrypt/Decrypt Functions
Public Function EncryptText(ByVal strText As String, ByVal strPwd As String, Optional bPasswordCaseSencitive As Boolean) As String
    Dim i As Integer, c As Integer
    If Not bPasswordCaseSensitive Then strPwd = UCase$(strPwd)
    'Encrypt string
    If Len(strPwd) Then
        For i = 1 To Len(strText)
            c = Asc(Mid$(strText, i, 1))
            c = c + Asc(Mid$(strPwd, (i Mod Len(strPwd)) + 1, 1))
            Mid(strText, i, 1) = Chr$(c And &HFF)
        Next i
    End If
    EncryptText = strText
End Function

'Decrypt text encrypted with EncryptText
Public Function DecryptText(ByVal strText As String, ByVal strPwd As String, Optional bPasswordCaseSencitive As Boolean) As String
    Dim i As Integer, c As Integer
    If Not bPasswordCaseSencitive Then strPwd = UCase$(strPwd)
    'Decrypt string
    If Len(strPwd) Then
        For i = 1 To Len(strText)
            c = Asc(Mid$(strText, i, 1))
            c = c - Asc(Mid$(strPwd, (i Mod Len(strPwd)) + 1, 1))
            Mid(strText, i, 1) = Chr$(c And &HFF)
        Next i
    End If
    DecryptText = strText
End Function

Open in new window

writing a csv file like this
 
Print #iFileNo, Text1.text & ";" & Combo2.text & ";" & DTPicker1.Value & ";" & Text21.text & ";" & 0 & ";" & Text4.text & ";" & Text5.text & ";" & Text2.text & ";" & Text3.text & ";" & Text8.text & ";" & Text9.text & ";" & Text10.text & ";" & Text12.text & ";" & TopyAmt & ";" & PadAmt & ";" & Combo1.text & ";" & Text27.text & ";" & Text28.text & ";" & Text33.text & ";" & Text30.text & ";" & Text29.text & ";" & Text37.text & ";" & Text14.text & ";" & Text13.text & ";" & Text15.text & ";" & 0 & ";" & Text19.text & ";" & 0 & ";" & Text23.text & ";" & ctgrp & ";" & Text24.text & ";" & 0 & ";" & 0 & ";" & dummyDate & ";" & Text36.text & ";" & Text40.text & ";" & 0 & ";" & 0 & ";" & dummyDate & ";" & Text11.text & ";" & TotTopyAmt & ";" & TotPadAmt & ";" & 0 & ";" & 0 & ";" & 0 & ";" & 0 & ";" & Text7.text & ";" & Combo3.text & ";" & Text17.text & ";" & Text18.text & ";" & Combo7.text & ";" & Text32.text & ";" & Text31.text & ";" & Text60.text & ";" & 0 & ";" & Text58.text & ";" & Text59.text
''close the file (if you dont do this, you wont be able to open it again!)

    Close #iFileNo

Open in new window


so before closing the file,  what code for encryption..?
It depends how you read the file..
You can encrypt each field or encrypt the whole line..
If you  encrypt the whole line, you have to use a Line Input.
Eg. Line Input #iFileNo, T99$
2. Decrypt T99$
3. Use the "Split" statement to.. Split T99$ on the ";" character into the fields.

Otherwise you would encrypy each field as you print them..
Eg.. Print #iFileNo, Encr(Text1.text) & ";" & Encr(Combo2.text) & ";" Encr(...
 (using Function Encr( as per one of the above encrypt functions..)
Then, when you read the fields, you use the Decrypt function on every field.
ok and how to call Encr function
like this..?
call Encr
You should do a little revision on VB Functions.. You never "Call" a Function..
A function is like the Tan function on a calculator - You just use it like xx = Tan(yy)
If your Encrypt function is named Encry( and your Decrypt function is called Decry(
Then it is as simple as..
Encrypted_Text = Encry(Plain_Text)
and
Back2Plain_Text = Decry(Encrypted_Text)
So, in your print example..
 Print #iFileNo, Encry(Text1.text) & ";" & Encry(Combo2.text) & ";" Encry(...  Etc.
oh yeah
Let me test,

ok
Encryption is working fine
 
''''open the file for writing
    Open Myflname For Output As #iFileNo

''''Encrypt myfile file
Print #iFileNo, Encry(Text1.text) & ";" & Encry(Combo2.text) & ";" & Encry(DTPicker1.Value) & ";" & _  (etc...)

Close #iFileNo

Open in new window


now what i want is
i want to Decrypt this file using same open method
how to do this
You read the file the same way you did before - If your file has 20 fields (and you used to read those fields into 20 variables) Those 20 variables will now be encrypted, so, for all 20 variables, you just have to say Variable13 = Decry(Variable13)
If you read into an array instead, you could use a For Next loop..
Eg.  For i = 1 To 20 : Variable(i) = Decry(Variable(i)) : Next i
(By the way.. your file format is NOT CSV - CSV is Comma Delimited, yours uses semicolons)
I thing something is wrong in encryption
because when i am trying to decrypt , decryption data is wrong.
like you can see here column No. 11 and 12
'RELLI','*'
Encry(Text9.text) is RELLING
Encry(Text10.text) is 300
 
''Ecrypted file
Print #iFileNo, Encry(Text1.text) & ";" & Encry(Combo2.text) & ";" & Encry(DTPicker1.Value) & ";" & _
                Encry(Text21.text) & ";" & Encry(0) & ";" & Encry(Text4.text) & ";" & Encry(Text5.text) & ";" & _
                Encry(Text2.text) & ";" & Encry(Text3.text) & ";" & Encry(Text8.text) & ";" & Encry(Text9.text) & ";" & _
                Encry(Text10.text) & ";" & Encry(Text12.text) & ";" & Encry(TopyAmt) & ";" & Encry(PadAmt) & ";" & _

Open in new window


actual data is RELLING and there is no data like *
so why its produced wrong data like
RELLI  and *
it should be RELLING, 300
 not *
 
''this is a Decrypted data
'303803','N/A','17-05-11','0','0','A ALAM DRESSES','SAREE  SANGAM','KOLKATA','AHMEDNAGAR','100','RELLI','*','300','50','5000.00','0'

Open in new window

I think decryption is not working
or
some thing wrong in both function

because its not only column 11 or 12
but many columns are wrong
Which functions are you using for Encry( & Decry( ?? The Ark ones? The VS20 ones? (There were two choices offered above)
VS20 ones
Using  the different names might have caused a problem..
Try this test code using the original names of those 2 functions..

X2$ = Encript4("ABCDEFG123456")
Msgbox X2$
X3$ = Decript4(X2$)
Msgbox X3$

If that works, try using those same names and functions in your normal code..
Those 2 functions have been widely used for over 15 years - they should be ok..
its ok when i am running it on separate project

how am i decrypt in main project
please check code
If Not IsNull(Decry(props(i))) And (Decry(props(i))) <> "" Then
    
        If valuelist = "" Then
            If i = 0 Then
                BNo = Decry(props(i))
            End If
            valuelist = valuelist & "'" & Decry(props(i)) & "'"
        Else
            If i = 0 Then
                BNo = Decry(props(i))
            End If
            
            valuelist = valuelist & ",'" & Decry(props(i)) & "'"
        End If
    End If
    
    Next i
    
    objFile.Close

Open in new window

SOLUTION
Avatar of BrianVSoft
BrianVSoft
Flag of Australia 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
Please check attached encrypted and plain file
I am using VS20 encrypt and decrypt function
attached file is encrypted by using VS20 encrypt function
 439303.csv

original file is which is encrypted above
 plain-439303.csv

now when i am trying to decrypt it
result is
 
'439303','C.','YSikTX`VZ','17-05-11','0','0','A J FASHION GALLERY','ALTAF KHAN','KOLKATA','AKLUJ','40','AUTO PARTS','50','100','4000.00','0','TBB','0.','[','0.','[','0.','[','0.','[','0.','[','4165.00','54/63','20000.00','BATCO TRANS INDIA','0','ABCDE','0','0','WADI (M.H.)','0','0','0','20-Oct-1976','105.00','N/A','0','0','20-Oct-1976','50','4165.00','0','0','0','0','0','BUNDLE','ARTICLE','415263987','415263987','TRANSPORTER','60.00','0.','[','N/A','0','919038822201','4152630'

Open in new window

Hi BrianVSoft, I think that you must help to crystal_Tech by using the capicom encryption api or another decent encryption mechanism, by using the Decript4 method is like do nothing, that is very easy to decrypt, if he really want to secure that data he must use another encryption method, crystal_Tech question was:

Hi
After create a csv file want to encrypt it with password protection
please help
I really dont understand why he dont use this already working code, it do exactly what he is asking for in the correct way, by using a standard encryption algorithm like, for example, Triple DES.

Well, this is only an advice.
Thanks yv989c:
let me try your suggestion
@ yv989c
how to encrypt this data using CAPICOM.DLL
like i am writing a file like this
so how to encrypt it
 
Print #iFileNo, Text1.text & ";" & Combo2.text & ";" & DTPicker1.Value & ";" & Text21.text & ";" & 0 & ";" & Text4.text & ";" & Text5.text & ";" & Text2.text & ";" & Text3.text & ";" & Text8.text & ";" & Text9.text & ";" & Text10.text & ";" & Text12.text & ";" & TopyAmt & ";" & PadAmt & ";" & Combo1.text & ";" & Text27.text & ";" & Text28.text & ";" & Text33.text & ";" & Text30.text & ";" & Text29.text & ";" & Text37.text & ";" & Text14.text & ";" & Text13.text & ";" & Text15.text & ";" & 0 & ";" & Text19.text & ";" & 0 & ";" & Text23.text & ";" & ctgrp & ";" & Text24.text & ";" & 0 & ";" & 0 & ";" & dummyDate & ";" & Text36.text & ";" & Text40.text & ";" & 0 & ";" & 0 & ";" & dummyDate & ";" & Text11.text & ";" & TotTopyAmt & ";" & TotPadAmt & ";" & 0 & ";" & 0 & ";" & 0 & ";" & 0 & ";" & Text7.text & ";" & Combo3.text & ";" & Text17.text & ";" & Text18.text & ";" & Combo7.text & ";" & Text32.text & ";" & Text31.text & ";" & Text60.text & ";" & 0 & ";" & Text58.text & ";" & Text59.text

Open in new window

Hi, did you opened the demo project?
http://filedb.experts-exchange.com/incoming/2011/04_w17/447100/CAPICOM-EncDec.zip
After you have created the csv file, just load its content into the Text1 control, and use the right button to encrypt that text with a password, after that, just save the Text3 control text into a new file, that will be your encrypted file. Use the same process to decrypt the file, but load your encrypted file data into the Text3 control, click the Decrypt button and save the Text4 control text into a new file, that will be your decrypted file.
don't you think its a long process for encryption and decryption
means
each and every file need to write two times for encryption and decryption.
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

Thanks
This question has been classified as abandoned and is closed as part of the Cleanup Program. See the recommendation for more details.