Encryption with Ascii Chars 0, 13, an 26 -- How do I go about it, or read data with them?

Posted on 2006-06-21
Last Modified: 2010-04-30
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(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.


Question by:klow5171
LVL 10

Accepted Solution

cool12399 earned 125 total points
ID: 16953568
Very simple solution.

Basically, once you have "encrypted" your content (assuming you do that *before* you write it to an output file, i.e.,
you store the encrypted results in a string, then output the file), you create 'tags' for special characters.


(a) Create the encrypted content in a string
(b) "Replace" all characters with a marker/tag, i.e., "&&&CRLF&&&" (which represents a chr(13)chr(10) combo), then
"&&&CHR0&&&" (which represents a chr(0)), etc, etc. So basically you would do this:

encryptedString = replace(encryptedString,chr(0),"&&&CHR0&&&")

' so you are replaciing all occurances of chr(0) with the actual "readable" string of "&&&CHR0&&&

And you do that for all 'special' characters (i.e., chr13, chr10, chr26, etc, etc)

(c) Then, write the file.
(d) When you "read" the file, assuming you read the entire content into a string, you then just do the reverse, i.e.,
encryptedString = replace(encryptedString,"&&&CHR0&&&",chr(0)) (to put it 'back' to normal).
(e) Then you do you normal decryption routine, whatever you have.

Anyways, there is your solution. Points please :)
LVL 85

Expert Comment

by:Mike Tomlinson
ID: 16953981
Did you write the "reading" program?

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.
LVL 20

Expert Comment

ID: 16954080
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
2. Decrypt in such a way that those characters are not delimited.
Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

LVL 13

Expert Comment

ID: 16954663

what is also an solution:

don't read and write the files with strings, but use Byte Arrays

example for reading and writing:

dim bBuffer() as byte
open "c:\test.txt" for binary as #1
    redim bBuffer(1 to lof(1))
    get #1,,bBuffer

dim bBuffer() as byte
'fill your buffer here
open "c:\test.txt" for binary as #1
    'or fill your buffer here
    put #1,,bBuffer

Author Comment

ID: 16955681
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&&&")

I like the byteArray approach.

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...

LVL 13

Expert Comment

ID: 16955806
'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))
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))
End Sub


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

LVL 10

Expert Comment

ID: 16956926
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).

Featured Post

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
SLMGR Switches Are Not Working On KMS Host 3 109
Visual Studio search word table and return Cell index 8 68
Recommendation vb6 to or others 14 174
RUNRMTCMD from AS/400 12 68
Introduction While answering a recent question ( in the VB classic zone, I wrote some VB code in the (Office) VBA environment, rather than fire up my older PC.  I didn't post completely correct code o…
This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…

809 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question