Zvvycode: A Universal Safe Text Encoding Method

Status
Not open for further replies.

drwool

Seniorius Lurkius
10
Software developers often need to write code that handles chunks of text typed by a user and whose content is therefore unpredictable. Every programming language has methods for dealing with such text. And every programming language has a way of “escaping” certain characters in text that normally have some syntactic meaning in the programming language – for example, quote marks (which most programming languages use to signify the beginning and end of a text string).

Different programming languages use different methods of “escaping” such characters so they can be interpreted as data rather than carrying their usual syntactic meaning. And therein lies a problem for programmers. If you are dealing with multiple programming languages or systems, you sometimes need a way of encoding text so that ALL of the disparate systems it passes through will treat it as data and not mistake some character embedded within it as a signal to terminate the string or whatever.

I recently ran up against this issue while writing a web application that used Javascript, PHP, HTML, and MySQL, as well as the potential for passing bits of text as parameters embedded in a web URL. The standard functions for escaping text were not sufficient to ensure that ANY text entered by the user could make it through all parts of the program without tripping something up. So I decided to create a universal text encoding method that works for any text and any combination of programming languages and systems you might happen to be using.

Here were my requirements:

* Must be able to faithfully encode ANY text that’s valid in Unicode and then decode it to be identical to the original.

* After being encoded, the string must consist of nothing but the ASCII alphabetic letters (a-z, A-Z) and digits (0-9), plus a single “escape” character.

* An application must be able to designate ANY Unicode character code as the escape (except the ASCII letters and digits), so as to avoid collisions with whatever languages or systems it needs to be compatible with.

* It should not matter if the chosen escape character happens to occur in the text that is to be encoded.

* Any program must be able to decode the text without having prior knowledge of what character was used as the escape during encoding.

* The encoded text should be basically readable by a human.


I came up with a method that satisfies all of these requirements. I call it ZvvyCode. (That’s two v’s in “Zvvy”. I pronounce it “zivy”.)

My full write-up on Zvvycode is here: http://just.thinkofit.com/zvvycode
 

MrMills

Ars Praefectus
3,727
Moderator
So why bother with the //zvvy// bit? Is there an unstated major requirement that you have to be able to know if a string is the encoded or decoded form? If you're decoding, anything that's not /A-Za-z0-9/ is an escape character, so the decoder doesn't need to know what the escape character is beforehand. Anything that's outside the range can be treated as one. Then take the input character stream, convert to UTF-8, and anything outside the normal accepted range needs to be escaped.

So, basically, URI component encoding, but encoding ~!*()' where URI component encoding leaves those alone, with the % being flexible.

Source:
I said, "Joe’s bag is 3/4 full!"

Your stated encoding:
//zvvy//I/20/said/2c//20//22/Joe/27/s/20/bag/20/is/20/3/2f/4/20/full/21//22/

Proposed modified URI component encoding:
I/20said/2c/20/22Joe/27s/20bag/20is/203/2f4/20full/21/22
 

Drizzt321

Ars Legatus Legionis
34,920
Subscriptor++
If you're that concerned, just Base64 encode everything. a-z, A-Z, 0-9, +, /, = are the possible characters you'll see. Granted, the last 3 need to be URL encoded, so there's base64url which uses different chars which don't need to be URL encoded.

However, Base64 encoding tends to increase the total data size by 1/4 to 1/3 or so. But is already a widely used and implemented standard.

Relevant XKCD

 

KallDrexx

Ars Tribunus Militum
2,040
So why bother with the //zvvy// bit? Is there an unstated major requirement that you have to be able to know if a string is the encoded or decoded form? If you're decoding, anything that's not /A-Za-z0-9/ is an escape character, so the decoder doesn't need to know what the escape character is beforehand. Anything that's outside the range can be treated as one. Then take the input character stream, convert to UTF-8, and anything outside the normal accepted range needs to be escaped.

It looks like the beginning part is needed to figure out what the actual escape character is, since at least on the blog it says that the escape character is not agreed upon by both parties before hand. So it uses a known first 8 characters so the encoder can use any escape character it wants and it will still decode properly.

That being said I don't see what the advantage is to Base64 (and url encoding it when it's in a url).
 
If you need to escape the text in an environment where it’s ok to present it exactly as entered: you are doing it wrong.

The only reason to escape text is if the environment has tokens it will interpret in a different context. Ie: html entities, JavaScript, etc. the only place to escape such text is on the exit points. If your environment is trying to interpret those symbols or tokens elsewhere in your data processing you have a security issue you need to address.

In the unbelievably rare circumstance where this is not true, use a base64 like encoding. It’s recognizable, easy to reverse engineer and there are lots of existing tools that use it, so validating it externally is also easy.
 

Drizzt321

Ars Legatus Legionis
34,920
Subscriptor++
If you're that concerned, just Base64 encode everything.
One of his requirements was "The encoded text should be basically readable by a human". I'm guessing that means alphanumeric characters should encode to themselves.

Oh, missed that one.

Frankly, I find it easy enough to read. "Oh, that's base64. Open up base64 decoder utility website, copy & paste, decode. That's it, perfect!" :p :eng101:
 

technophile

Ars Legatus Legionis
23,768
If you're that concerned, just Base64 encode everything.
One of his requirements was "The encoded text should be basically readable by a human". I'm guessing that means alphanumeric characters should encode to themselves.

Oh, missed that one.

Frankly, I find it easy enough to read. "Oh, that's base64. Open up base64 decoder utility website, copy & paste, decode. That's it, perfect!" :p :eng101:
Or just open up Chrome/Firefox, hit F12, and put atob("<base64>") in the console and see what it spits out. :p
 
Status
Not open for further replies.