Archived Forum Post

Index of archived forum posts

Question:

vb6 String encoding error / chr(0) problem

Aug 06 '14 at 08:53

Hi,

i'm using chilkatcrypt and want to encode/decode VB6 String messages whicht can contain all characters from 0 to 255.

dim s1 as string,s2 as string,s3 as string

crypt.CryptAlgorithm = "none"
crypt.EncodingMode = "hex" ' or "base64"
crypt.Charset = "unicodefffe"

s1 = ""
For ax = 0 To 255
 s1 = s1 + Chr(ax)
Next 
s2 = crypt.EncryptStringENC(s1)
s3 = crypt.DecryptStringENC(s2)
debug.print s1=s3

This sample works perfect for char ranges between 1 and 255, but if my String starts with chr(0) chilkat will return an empty result and no error message.

I tried all possible Encoding/Charset modes, but it doesn't help.

Is there any chance to encode/decode binary strings using base64 without having problems? (I want to use this to send data via TCP connections)

regards Hawk78


Answer

The Chilkat API is provided for many different programming languages in many different environments. A "string" is very different than binary data. A "string" is a sequence of characters, each of which is associated with a glyph (such as "A", or "ら", or "Ω").

Each character in a string is a logical entity. To save a string to a file (or to bytes in memory), some sort of byte representation must be used. For example,

Consider this character: É

In the iso-8859-1 character encoding, it is represented by a single byte: 0xC9

In the utf-8 character encoding, it is represented by a two bytes: 0xC3 0x89

In the ucs-2 character encoding, it is represented by a two bytes: 0x00 0xC9

What you've done is to create a byte array (containing the byte values 0 through 255), and then inappropriately called it a "string". But you don't really have a string (even if VB6 calls it a CHR or "string"). Because Chilkat is offered in so many programming languages, it must be more strict about how it handles strings. If what you have are bytes that do not represent actual printable characters, then you should use the Chilkat methods for handling binary data, such as EncryptBytesENC.


Answer

This does look like a bug in ChilkatCrypt to me - if I put Chr$(0) anywhere except for the first character in the string to encode, the result is as expected (s1=s3). When the first character is Chr$(0), then the result is an empty string for s3 (even though the LastErrorText property reports success after encrypting and decrypting).


Answer

this is almost like what i face now. and as far as i know in vb6 keyascii chart, chr(0) will resulting NULL.(fix me if im wrong)

kinda need to make some another tricky perspective way.


Answer

If it's not actually a string (i.e. bytes that represent characters in some human language, according to some character encoding (utf-8, iso-8859-1, ebcdic, Shift-JIS, etc.), that are visually displayed as glyphs), then do not use the string methods (such as EncryptStringENC). Instead, use the byte methods, such as EncryptBytesENC.