The problem with the chr(0) can be solved by this process.
The pseudocode is like this. In the final encrypted text, the chr(0) cuts the rest of the string. We can
mark those positions and decrypt accordingly.
In our encrypted text we can include these characters in the front.
flag & noOfFaults & pos(1), .....pos(n),
flag indicated that the problem occured. noofFaults indicated the total number of faults and pos(1),..pos(n) indicates the positions of faults. In the decryption process we will first replace those positions with chr(0) and decrypt accordingly.
Option Explicit
'Set to True to make the password case-sensitive
#Const CASE_SENSITIVE_PASSWORD = False
Private Sub cmdEncrypt_Click()
Dim buffText As String
Dim lngFileEnd As Integer
' You can encrypt twice for extra security
buffText = EncryptText((txtText), txtPassword)
buffText = EncryptTextFinal((buffText), txtPassword)
Dim path As String
path = App.path & "\a.txt"
'writing the contents to a file
'opening the file and writing contents
Open path For Binary Access Write As #1
lngFileEnd = LOF(1) + 1
'putting data into file
Put #1, lngFileEnd, buffText
Close #1
txtText = buffText
cmdEncrypt.Enabled = False
cmdDecrypt.Enabled = True
End Sub
Private Sub cmdDecrypt_Click()
txtText = DecryptTextFault((txtText), txtPassword)
txtText = DecryptText((txtText), txtPassword)
cmdEncrypt.Enabled = True
cmdDecrypt.Enabled = False
End Sub
'Encrypt text finally
Private Function EncryptTextFinal(strText As String, ByVal strPwd As String)
Dim i As Integer, c As Integer
Dim strBuff As String
Dim jhamela As Integer
Dim noFault As Integer
jhamela = 0
noFault = 0
#If Not CASE_SENSITIVE_PASSWORD Then
'Convert password to upper case
'if not case-sensitive
strPwd = UCase(strPwd)
#End If
'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))
Dim addBuff As String
'checking for faulty position
If Chr(c And &HFF) = Chr(0) Then
'marking of a faulty portion
jhamela = 1
noFault = noFault + 1
addBuff = Trim(addBuff) & Trim(Str(i)) & ","
strBuff = strBuff & "0"
Else
strBuff = strBuff & Chr(c And &HFF)
End If
Next i
End If
Dim jhBuff As String
If jhamela = 1 Then
jhBuff = "1"
strBuff = Trim(jhBuff) & Trim(Str(noFault)) & "," & Trim(addBuff) & Trim(strBuff)
Else
jhBuff = "0"
strBuff = Trim(jhBuff) & Trim(strBuff)
End If
EncryptTextFinal = strBuff
End Function
'Encrypt text
Private Function EncryptText(strText As String, ByVal strPwd As String)
Dim i As Integer, c As Integer
Dim strBuff As String
Dim jhamela As Integer
Dim noFault As Integer
jhamela = 0
noFault = 0
#If Not CASE_SENSITIVE_PASSWORD Then
'Convert password to upper case
'if not case-sensitive
strPwd = UCase(strPwd)
#End If
'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))
strBuff = strBuff & Chr(c And &HFF)
Next i
Else
strBuff = strText
End If
EncryptText = strBuff
End Function
'Decrypt text encrypted with EncryptText
Private Function DecryptText(strText As String, ByVal strPwd As String)
Dim i As Integer, c As Integer
Dim strBuff As String
#If Not CASE_SENSITIVE_PASSWORD Then
'Convert password to upper case
'if not case-sensitive
strPwd = UCase(strPwd)
#End If
'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))
strBuff = strBuff & Chr(c And &HFF)
Next i
Else
strBuff = strText
End If
DecryptText = strBuff
End Function
'Decrypt text encrypted with EncryptText
Private Function DecryptTextFault(strText As String, ByVal strPwd As String)
Dim i As Integer, c As Integer
Dim strBuff As String
#If Not CASE_SENSITIVE_PASSWORD Then
'Convert password to upper case
'if not case-sensitive
strPwd = UCase(strPwd)
#End If
'getting the first portion and finding the faulty positions
Dim jhText As String
Dim mainText As String
Dim noFault As Integer
Dim pos(100) As Integer
mainText = strText
jhText = Mid(strText, 1, 1)
'MsgBox jhText
If jhText = "1" Then
'getting the jhamelas
Dim jhNo As String
jhNo = ""
Dim j As Integer
j = 2
Do
jhNo = jhNo & Trim(Mid(mainText, j, 1))
j = j + 1
Loop While Mid(mainText, j, 1) <> ","
noFault = CInt(jhNo)
'looping through the maintext for finding the positions
'MsgBox j
For i = 1 To noFault
Do
pos(i) = pos(i) & Trim(Mid(mainText, j, 1))
j = j + 1
Loop While Mid(mainText, j, 1) <> ","
'MsgBox pos(i) & "and current j " & j
Next i
'now correction of the main text and getting our desired text
mainText = Mid(mainText, j + 1, Len(mainText))
j = 1 'updating the current position
'going to faulty positions and correcting those
Dim correctedMainText As String
correctedMainText = ""
For i = 1 To noFault
j = pos(i)
'previous text
correctedMainText = Trim(Mid(mainText, 1, j - 1))
'faulty text
correctedMainText = correctedMainText & Chr(0)
'trailing text
correctedMainText = correctedMainText & Trim(Mid(mainText, j + 1, Len(mainText)))
mainText = correctedMainText
Next i
mainText = correctedMainText
Else
mainText = Mid(strText, 2, Len(strText))
End If
'Decrypt string
If Len(strPwd) Then
For i = 1 To Len(mainText)
c = Asc(Mid(mainText, i, 1))
c = c - Asc(Mid(strPwd, (i Mod Len(strPwd)) + 1, 1))
strBuff = strBuff & Chr(c And &HFF)
Next i
Else
strBuff = strText
End If
DecryptTextFault = strBuff
End Function