-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCIPHER_CRYPTO_LIBR.bas
executable file
·184 lines (133 loc) · 4.36 KB
/
CIPHER_CRYPTO_LIBR.bas
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
Attribute VB_Name = "CIPHER_CRYPTO_LIBR"
Option Explicit 'Requires that all variables to be declared explicitly.
Option Base 1 'The "Option Base" statement allows to specify 0 or 1 as the
'default first index of arrays.
Private Const PUB_CHUNK_VAL = 100000
Private CRYPTO_CLASS_OBJ As New clsCrypto
'----------------------------------------------------------------------------------
'----------------------------------------------------------------------------------
'// These algorithms encodes/decodes at about 200k per second
'----------------------------------------------------------------------------------
'----------------------------------------------------------------------------------
Function ENCRYPT_TEXT_MSG_FUNC(ByVal DATA_STR As String, _
ByVal PASSWORD_STR As String)
On Error GoTo ERROR_LABEL
CRYPTO_CLASS_OBJ.password = PASSWORD_STR
CRYPTO_CLASS_OBJ.OutBuffer = ""
CRYPTO_CLASS_OBJ.InBuffer = DATA_STR
CRYPTO_CLASS_OBJ.Encrypt
ENCRYPT_TEXT_MSG_FUNC = CRYPTO_CLASS_OBJ.OutBuffer
Exit Function
ERROR_LABEL:
ENCRYPT_TEXT_MSG_FUNC = Err.number
End Function
Function DECRYPT_TEXT_MSG_FUNC(ByVal DATA_STR As String, _
ByVal PASSWORD_STR As String)
On Error GoTo ERROR_LABEL
CRYPTO_CLASS_OBJ.password = PASSWORD_STR
'CRYPTO_CLASS_OBJ.OutBuffer = ""
CRYPTO_CLASS_OBJ.InBuffer = DATA_STR
CRYPTO_CLASS_OBJ.Decrypt
DECRYPT_TEXT_MSG_FUNC = CRYPTO_CLASS_OBJ.OutBuffer
Exit Function
ERROR_LABEL:
DECRYPT_TEXT_MSG_FUNC = Err.number
End Function
Function ENCRYPT_FILE_FUNC(ByVal ORIGINAL_FILE_STR As String, _
ByVal ENCRYPTED_FILE_STR As String, _
ByVal PASSWORD_STR As String)
'Debug.Print ENCRYPT_FILE_FUNC( _
"C:\Documents and Settings\HOME\Desktop\NICO.xls", _
"C:\Documents and Settings\HOME\Desktop\NICO.DATA_STR", "Rafael")
Dim i As Long
Dim j As Long
Dim k As Long
Dim NSIZE As Long
Dim EOF_FLAG As Boolean
Dim DATA_STR As String
On Error GoTo ERROR_LABEL
ENCRYPT_FILE_FUNC = False
CRYPTO_CLASS_OBJ.password = PASSWORD_STR
CRYPTO_CLASS_OBJ.OutBuffer = ""
'Application.StatusBar = "Reading file"
NSIZE = FileLen(ORIGINAL_FILE_STR) / PUB_CHUNK_VAL
DATA_STR = Space$(PUB_CHUNK_VAL)
j = FreeFile
k = j + 1
Open ENCRYPTED_FILE_STR For Output As j: Close j
Open ORIGINAL_FILE_STR For Binary As j
Open ENCRYPTED_FILE_STR For Binary As k
EOF_FLAG = False
Do While EOF_FLAG = False
Get j, , DATA_STR
If EOF(j) Then
DATA_STR = Space$(FileLen(ORIGINAL_FILE_STR) - i * PUB_CHUNK_VAL)
Get j, i * PUB_CHUNK_VAL + 1, DATA_STR
EOF_FLAG = True
End If
CRYPTO_CLASS_OBJ.InBuffer = DATA_STR
CRYPTO_CLASS_OBJ.Encrypt
DATA_STR = CRYPTO_CLASS_OBJ.OutBuffer
Put k, , DATA_STR
i = i + 1: If i > NSIZE Then i = NSIZE
'Application.StatusBar = "Encrypting.. " & Format(i / NSIZE, "0%")
Loop
Close j
Close k
ENCRYPT_FILE_FUNC = True
Exit Function
ERROR_LABEL:
On Error Resume Next
Close j
Close k
ENCRYPT_FILE_FUNC = False
End Function
Function DECRYPT_FILE_FUNC(ByVal DECRYPTED_FILE_STR As String, _
ByVal ENCRYPTED_FILE_STR As String, _
ByVal PASSWORD_STR As String)
'Debug.Print DECRYPT_FILE_FUNC( _
"C:\Documents and Settings\HOME\Desktop\NICO.xls", _
"C:\Documents and Settings\HOME\Desktop\NICO.DATA_STR", _
"Rafael")
Dim i As Long
Dim j As Long
Dim k As Long
Dim NSIZE As Long
Dim DATA_STR As String
Dim EOF_FLAG As Boolean
On Error GoTo ERROR_LABEL
DECRYPT_FILE_FUNC = False
CRYPTO_CLASS_OBJ.password = PASSWORD_STR
'Application.StatusBar = "Reading encrypted data"
NSIZE = FileLen(ENCRYPTED_FILE_STR) / PUB_CHUNK_VAL
DATA_STR = Space$(PUB_CHUNK_VAL)
j = FreeFile
k = j + 1
Open DECRYPTED_FILE_STR For Output As j: Close j
Open ENCRYPTED_FILE_STR For Binary As j
Open DECRYPTED_FILE_STR For Binary As k
EOF_FLAG = False
Do While EOF_FLAG = False
Get j, , DATA_STR
If EOF(j) Then
DATA_STR = Space$(FileLen(ENCRYPTED_FILE_STR) - i * PUB_CHUNK_VAL)
Get j, i * PUB_CHUNK_VAL + 1, DATA_STR
EOF_FLAG = True
End If
CRYPTO_CLASS_OBJ.InBuffer = DATA_STR
CRYPTO_CLASS_OBJ.Decrypt
DATA_STR = CRYPTO_CLASS_OBJ.OutBuffer
Put k, , DATA_STR
i = i + 1: If i > NSIZE Then i = NSIZE
'Application.StatusBar = "Decrypting.. " & Format(i / NSIZE, "0%")
Loop
Close j
Close k
DECRYPT_FILE_FUNC = True
Exit Function
ERROR_LABEL:
On Error Resume Next
Close j
Close k
DECRYPT_FILE_FUNC = False
End Function