klow5171
asked on
Encryption with Ascii Chars 0, 13, an 26 -- How do I go about it, or read data with them?
I have a VB program that encrypts data for another VB program to read via a text file.
The encryption creates Ascii characters in CHR format in the output text file.
In my tests, I noticed that when the following ASC values are written on the output file, the second program the reads it does not get the entire record:
ASC(0) - NULL
ASC(13) - Carriage Return
ASC(26) - SUB- Substitute
For example:
1- if the output record has 136 characters and the 25th one is ASC(13), the output file is read by the "reading" program as 2 lines.
2- If the output record has 136 chars and the 25th one is ASC(0), only 25 bytes/characters are written to the output file. This is not correct as the "reading" program expects all of the 136 characters.
Any ideas or answers can help.
Thanks.
The encryption creates Ascii characters in CHR format in the output text file.
In my tests, I noticed that when the following ASC values are written on the output file, the second program the reads it does not get the entire record:
ASC(0) - NULL
ASC(13) - Carriage Return
ASC(26) - SUB- Substitute
For example:
1- if the output record has 136 characters and the 25th one is ASC(13), the output file is read by the "reading" program as 2 lines.
2- If the output record has 136 chars and the 25th one is ASC(0), only 25 bytes/characters are written to the output file. This is not correct as the "reading" program expects all of the 136 characters.
Any ideas or answers can help.
Thanks.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
No his problem is that he is offsetting the character value, so a file with normal characters, when encrypted, normal characters are converted into parse-breaking characters.
The problem is that if your decryptor is using readline or a tokenizer, it is ignoring characters 13, 10 and 26 because they are delimiters. What you need to do is read in the characters one at a time, using an equivalent of the getchar() command, so that even delimiters are read in, and you can decrypt them as normal.
In summary, the problem is that the decryptor is skipping those characters because they are delimiters.
1. The solution is to either encrypt in such a way that those characters are never generated
or
2. Decrypt in such a way that those characters are not delimited.
The problem is that if your decryptor is using readline or a tokenizer, it is ignoring characters 13, 10 and 26 because they are delimiters. What you need to do is read in the characters one at a time, using an equivalent of the getchar() command, so that even delimiters are read in, and you can decrypt them as normal.
In summary, the problem is that the decryptor is skipping those characters because they are delimiters.
1. The solution is to either encrypt in such a way that those characters are never generated
or
2. Decrypt in such a way that those characters are not delimited.
what is also an solution:
don't read and write the files with strings, but use Byte Arrays
example for reading and writing:
'reading:
dim bBuffer() as byte
open "c:\test.txt" for binary as #1
redim bBuffer(1 to lof(1))
get #1,,bBuffer
close
'writing:
dim bBuffer() as byte
'fill your buffer here
open "c:\test.txt" for binary as #1
'or fill your buffer here
put #1,,bBuffer
close
ASKER
cool12399 :
I thought of the Substitution/Replace approach. However, there's still a "tiny" chance that I may have valid encrypted values that is identical to the substitute value (e.g., "&&&CHR0&&&")
Mark_FreeSoftware:
I like the byteArray approach.
However:
1. Is there a way to convert String to byteArray (for the write) and vice-versa (for the read)?
2. How do I fill the bufferArray with string data in the "writing: " section?
Thanks in advance for your help...
I thought of the Substitution/Replace approach. However, there's still a "tiny" chance that I may have valid encrypted values that is identical to the substitute value (e.g., "&&&CHR0&&&")
Mark_FreeSoftware:
I like the byteArray approach.
However:
1. Is there a way to convert String to byteArray (for the write) and vice-versa (for the read)?
2. How do I fill the bufferArray with string data in the "writing: " section?
Thanks in advance for your help...
'from string to array:
Private Sub StrToArr(str As String, ByRef bytArr() As Byte)
Dim n As Long
ReDim bytArr(0 To Len(str) - 1)
For n = 1 To Len(str)
bytArr(n - 1) = Asc(Mid$(str, n, 1))
Next
End Sub
'from array to string:
Private Sub ArrToStr(bytArr() As Byte, ByRef str As String)
Dim n As Long
str = ""
For n = LBound(bytArr) To UBound(bytArr)
str = str & Chr(bytArr(n))
Next
End Sub
'usage:
Private Sub Form_Load()
Dim strData As String, bytData() As Byte
strData = "Hello!"
StrToArr strData, bytData
'bytData is now an array that holds the data from strData
strData = "" 'clear it just to make sure we really assign to this var
ArrToStr bytData, strData
'the data from bytData is now back in strData
MsgBox strData
End Sub
Private Sub StrToArr(str As String, ByRef bytArr() As Byte)
Dim n As Long
ReDim bytArr(0 To Len(str) - 1)
For n = 1 To Len(str)
bytArr(n - 1) = Asc(Mid$(str, n, 1))
Next
End Sub
'from array to string:
Private Sub ArrToStr(bytArr() As Byte, ByRef str As String)
Dim n As Long
str = ""
For n = LBound(bytArr) To UBound(bytArr)
str = str & Chr(bytArr(n))
Next
End Sub
'usage:
Private Sub Form_Load()
Dim strData As String, bytData() As Byte
strData = "Hello!"
StrToArr strData, bytData
'bytData is now an array that holds the data from strData
strData = "" 'clear it just to make sure we really assign to this var
ArrToStr bytData, strData
'the data from bytData is now back in strData
MsgBox strData
End Sub
Hi klow5171,
Thanks for the points :) As for the 'likelihood' -- *extremely* extremely* extremely* extremely* extremely* extremely* extremely*
unlikely. But if that is a concern, simply make a different string, like:
"-------------------->CHR0 <--------- ---------- " (so the 'likelihood' of ever ending up with a legitimate 'string' sequence like that is next
to 0).
Thanks for the points :) As for the 'likelihood' -- *extremely* extremely* extremely* extremely* extremely* extremely* extremely*
unlikely. But if that is a concern, simply make a different string, like:
"-------------------->CHR0
to 0).
The functions used to read the file determine the characters it will use as delimeters...
You should be able to read in the entire file at once and then process it.