Jump to content

Heinous issue with hex / ascii variables


47.46.45

Recommended Posts

Hi gents.

 

New to the forum (well... sort of, I haven't actually been online since my high school days). Apologies if any of the following uses the wrong terminology, I'm a bit of a rookie. Conversely, if encryption isn't your thing, don't get scared off by all the talk of IVs and CBC - the actual issue is a lot more basic.

 

So, on to the issue that I'm encountering. The system I'm working with operates something like this:

 

1. A remote device receives data from a variety of radio-linked nodes

2. Remote device generates a random IV and encrypts the data from step 1 using AES encryption, a predetermined key and the random IV.

3. Packet is prepared for sending; takes the form of [16 character IV][2 character data length][encrypted data]

4. Data is sent via a UDP socket to my server

5. Server receives packet

6. Server splits packet into IV, length, crypttext

7. Server decrypts packet

 

Sounds simple, no? I've got the system working (more on that later) so I'll run you through an example of what goes on at the server end.

 

We receive a packet:

 

&:¥‹$Ý£."€¡Ñ #Ÿt�œÉêÍO¡ZígâBE…ìNñR°Ipø…0r¸Ô¶¼ &ò "ƒ5€Îvw¡BU»Ë;žzž­‰;ùÍ™Ž wƒ;EVÊ7uL´|¡¿ˆä-iÛXλüƒ4~3–zª²Ús¦6·P Gæ.æx"Ú¸bÆ%7�5y¸•X5Ø%´~?¦ñUÚþ«ˆ~Ëöêûq*Íä ± toñ²

 

Which is broken down by some boring substr bits that don't need to be reproduced. For the ease of the reader I've coloured the above - red is the IV, green is the length, the rest is the encrypted data.

 

So here's our decryption routine:

 

$key = "DDDDDDDDDDDDDDDD";
$iv = "&:¥‹$Ý£."€¡Ñ #Ÿt";
$crypttext = "ÉêÍO¡ZígâBE…ìNñR°Ipø…0r¸Ô¶¼ &ò "ƒ5€Îvw¡BU»Ë;žzž­‰;ùÍ™Ž wƒ;EVÊ7uL´|¡¿ˆä-iÛXλüƒ4~3–zª²Ús¦6·P Gæ.æx"Ú¸bÆ%7�5y¸•X5Ø%´~?¦ñUÚþ«ˆ~Ëöêûq*Íä ± toñ²";
$plaintext = mcrypt_decrypt(MCRYPT_RIJNDAEL_128, $key, $crypttext, MCRYPT_MODE_CBC, $iv);

 

The output for which is:

 

S0D0075DC00 S0D0075DC00 S0D0075DC00 S0D0075DC00 S0D0075DC00 S0D0075DC00 S0D0075DC00 S0D0075DC00 S0D0075DC00 S0D0075DC00 S0D0075DC00 S0D0075DC00 S0D0075DC00

 

Bingo, that's exactly what we want. But, as always, something had to go disastrously wrong.

 

Take, for example, this encrypted packet:

 

Çj›» ™^°—�¨<”×uÜw™»Ñòæ?£äÿtWÐÍd›Ø^ÃkYÊ„*£"K,6œÂäDöô ^iDÇ®CòdŘo¾µ«…fœÍí‘6ÚŒcñF0„øh½ÛÂl­PÞŽ%¤vß[vÁøÓs*?}³r>¬£2R>[|Á@ jÿlöEÇÆc7øzŠ|| .w)§‘¥…Fn4Oë£D8šO°]¶®¨ú�=ÝÌ(ôÖôTÖ

 

The script splits it up into the constituent variables fine, but once we go to decrypt it...

 

S1F7EB2EA88 S1F7EB2EA88 S1F7EB2EA88 S1F7EB2EA88 S1F7EB2EA88 S1F7EB2EA88 S1F7EB2EÚÕ ¼e°Âë÷ãw½Ž¡Ôèë´ó®’#Ë,)å€Ss[?Ê´2¸„šìáY^çhN÷4JC;mAÎP•S€îbÿÁÞ_¦N?]ïN08Ø,Ÿ4ZÍ

 

What's going on here? As you all (probably) know, data travels in a series of binary on/off pulses. This can be displayed to a terminal in raw bin, hex, dec or ascii. When PHP receives this troublesome packet, it records those binary pulses as ASCII values. Fine, that's all well and good, except that there aren't ASCII values for every hex pair. You can see why this is an issue in the above - midway through the crypttext, something that should be a "" or something is actually being recorded as an unprintable ASCII character. The decrypter hits it, which sends the whole algorithm hurtling down a different path - meaning that the rest of the packet gets junked.

 

If these "corrupted" characters don't appear, then the script works fine. If they appear in the crypttext, everything afterwards will be ruined. If they appear in the IV, the whole packet is trashed. If they appear in the length... there is a distinct possibility the world will end.

 

What can I do to solve this issue? I could (potentially) set my socket listener up to record the variables as binary or hex instead of ascii, thus avoiding any unprintable characters, but I don't know how to change the variable type or whatever so that mcrypt will recognise them as being binary signifiers rather than an ASCII stream of 1's and 0's.

 

I can imagine this might be a bit overwhelming, especially considering my terrible attempts at explanation. Please ask any questions about the above... I've tried everything I can think of and am at my wit's end!

 

Cheers,

-Sam

 

Link to comment
Share on other sites

Thanks for the fast reply!

 

Bit of a possible issue there - the remote device is completely beyond my control and doesn't run PHP (or any language understandable by human beings) so I have to work around it's spiteful methods of data transmission.

 

Is this an issue? Where do you think I should implement the base64 encode / decode steps?

Link to comment
Share on other sites

Assuming for a second that I had the $iv stored in base 64, could I do:

 

$plaintext = mcrypt_decrypt(MCRYPT_RIJNDAEL_128, $key, $crypttext, MCRYPT_MODE_CBC, base64_decode($iv));

 

Or is that not how it's meant to work?

 

Quick edit: sorry for the double post, I can't work out how to edit posts that are more than a few minutes old?

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.