2

I have been trying to implement XTEA encryption on Cortex M3 Microcontroller in C , in IAR IDE , so far Encryption Decryption is working , but i am facing a problem , I have To Encode Ascii Strings,but the Encrypted String Sometimes contains a 0x00 in String Array ,not in end as null terminator but in middle somewhere , so the String functions do not get the correct length of String because of this extra 0x00 they assume its Null Terminator , Problem is that I have to Transmit this String over GPRS , and the backend is also using same kind of ASCII string expecting 0x00 only at the end , in firmware also rest of String Transmission is assuming it to be Null Terminating , is there any way to Replace this 0x00 in Encrypted String with some other value , in a manner that it can also be reproduced easily later in backend , suppose i add 0x01 to each Encrypted String Array , this may make one 0xff to 0x00 , is there a way to remove this 0x00 ,

or any other Simple Encryption Algorithm for Ascii String , easily implementable on microcontroller, that guarantee Non Zero value in encrypted string , the backend guys insist on having some algorithm being used in systems , does AES algorithm insures Non zero value?

3
  • 1
    AES an other encryption algorithms have random output so 0x00 bytes are possible like any other bytes. Commented Aug 21, 2013 at 21:54
  • 4
    Stop treating your encrypted data as text strings - it's not, you should be treating it as a byte array (length prefixed instead of null delimited) instead. Commented Aug 22, 2013 at 11:50
  • 1
    What @NickJohnson says is the real answer to this problem. If you're trusting your encrypted data to be ASCII then you are going to have serious problems. Commented Aug 28, 2013 at 8:59

3 Answers 3

1

Rather than invent a new encoding scheme consider using existing ones. (Your documentation will be easier.)

If your data is ASCII, then you are limited to codes 0 to 0x7F. Base64 is the best way to go.

Sign up to request clarification or add additional context in comments.

2 Comments

I believe there is only a problem with 0x00 byte so escaping it will be more efficient than base64 (assuming that byte 0x00 have equal probability as other bytes).
@Maciej S OP says "backend is also using same kind of ASCII string expecting 0x00". ASCII strings only use codes 0-127. OP is encrypting via XTEA which output unint32_t likely converted to groups of 4 bytes of values 0-255. So either the OP needs an ASCII communication solution or the OP is mis-using the "ASCII". If the OP is mis-using "ASCII", the red-flags go off and I suspect other unstated communication issues. At the end-of-the-day, recommending an industry standard, is a better bet to solve OPs problem, stated and unstated.
0

If you don't mind your strings growing a little you could replace 0x00 bytes with 0x01 0x01 and 0x01 bytes with 0x01 0x02 during the encode.

At the far end you can then replace 0x01 0x01 with 0x00 and 0x01 0x02 with 0x01 during the decode.

For example:

0x04 0x00 0x05 0x01 would turn into 0x04 0x01 0x01 0x05 0x01 0x02 during transmission.

1 Comment

Communication protocols often use this escaping.
0

You can always use base64 or other method of encoding but I think escaping 0x00 as suggested is better.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.