Shop OBEX P1 Docs P2 Docs Learn Events
XOR cipher — Parallax Forums

XOR cipher

Sniper KingSniper King Posts: 221
edited 2008-08-04 19:28 in Propeller 1
I am developing a simple XOR encryption/decryption.· Attached is the code.· it isn't working right.· Any help would be cool.

▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
·- Was it pin 11 or 26?· Hmmm....··I think the smell of smoke tells the whole story.· Must be 26.



Michael King
Application Engineer
R&D
Digital Technology Group
«1

Comments

  • Erik FriesenErik Friesen Posts: 1,071
    edited 2008-07-30 22:23
    Could you explain what you are trying to do and how much is working and what it is not doing? I have fooled a little with this xor stuff and it can trick even the programmer at times. Some times I have taken and printed out the results and then run them through step by step with the windows binary calculator to see what is happening.
  • Sniper KingSniper King Posts: 221
    edited 2008-07-30 22:26
    This software basically XORs the message against a key that loops. See example below...

    Key=MIKE
    Message=This is a test

    T XOR M
    h XOR I
    i XOR K
    s XOR E
    XOR M
    i XOR I
    s XOR K

    ... and so on.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    ·- Was it pin 11 or 26?· Hmmm....··I think the smell of smoke tells the whole story.· Must be 26.



    Michael King
    Application Engineer
    R&D
    Digital Technology Group
  • VIRANDVIRAND Posts: 656
    edited 2008-07-30 22:39
    I don't think it's legal here publish or export strong encryption. Mums the word.
  • TimmooreTimmoore Posts: 1,031
    edited 2008-07-30 22:39
    There are several things you will have problems with, if an XOR of the key and text generate 0 i.e. same char then thats end of string. You are xor the end of string 0 so it becomes non-0 so you have a problem there. You are xor with the key end of string which doesn't encrypt i.e. A = A ^ 0. You want strsize(X) -1 for the repeat and if but that doesn't fix the generation of end of string.
  • Phil Pilgrim (PhiPi)Phil Pilgrim (PhiPi) Posts: 23,514
    edited 2008-07-30 22:47
    If your plaintext is confined to 7-bit ASCI and your cipher key always has its upper bit set to 1, you will experience no problems converting plaintext characters to zero. You do need to make sure you don't convert the terminal zero in your plaintext to a non-zero character, though.

    -Phil

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    'Still some PropSTICK Kit bare PCBs left!
  • Sniper KingSniper King Posts: 221
    edited 2008-07-30 22:51
    I add 48 to each byte encrypted so I never have a zero.· then I subtract 48 from the encrypted byte before re-XORing it.· But the results are all wrong.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    ·- Was it pin 11 or 26?· Hmmm....··I think the smell of smoke tells the whole story.· Must be 26.



    Michael King
    Application Engineer
    R&D
    Digital Technology Group
  • TimmooreTimmoore Posts: 1,031
    edited 2008-07-30 22:56
    local variables i.e. z is not initialized to 0
  • Sniper KingSniper King Posts: 221
    edited 2008-07-30 22:58
    Here is the latest code.· Still a NOGO.

    I cant even post the results because the nature of the ASCii character are un reproducable.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    ·- Was it pin 11 or 26?· Hmmm....··I think the smell of smoke tells the whole story.· Must be 26.



    Michael King
    Application Engineer
    R&D
    Digital Technology Group
  • TimmooreTimmoore Posts: 1,031
    edited 2008-07-30 23:04
    try using
    byte[noparse][[/noparse]msg+i] rather than msg, since you want to do it by byte but msg I think does it by long since it can't tell the original size.
    Also you can remove buf and write directly to emsg/dmsg, which is a good idea if msg is > 64 bytes.
  • Paul BakerPaul Baker Posts: 6,351
    edited 2008-07-30 23:32
    Hi Michael,

    While this is much more complicated than what you are trying to do, I have implemented blowfish (popular xor block cipher) for the Propeller. It is availible here: http://forums.parallax.com/showthread.php?p=714050


    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Paul Baker
    Propeller Applications Engineer

    Parallax, Inc.
  • Chris SavageChris Savage Parallax Engineering Posts: 14,406
    edited 2008-07-31 14:52
    Michael,

    I wrote a DOS program back in the early 90’s that did this exact same thing. I probably still have the source code. In any event, what I was going to suggest is that you break down the steps. For example, the routines that adds 48 to each byte…try saving the output from that step before proceeding. When you get to the step of subtracting 48, save that data out as well and compare the two strings. Because you are taking multiple steps to an end goal there may be an issue in an intermediate step. For that matter, have you tried not adding/subtracting the 48? As for posting results, you can always attach a binary file if you can output to one.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Chris Savage
    Parallax Tech Support
  • TimmooreTimmoore Posts: 1,031
    edited 2008-07-31 15:53
    This works, needed end of string terminator, similar for decrypt

    PUB Encrypt(Key,Msg,Emsg) | z,i
    z := 0
    repeat I from 0 to strsize(msg)-1
    byte[noparse][[/noparse]emsg+i] := (byte[noparse][[/noparse]msg+i] ^ byte[noparse][[/noparse]key+z])+48
    if ++z == strsize(key)
    z:= 0
    byte[noparse][[/noparse]emsg+i] := 0
  • PraxisPraxis Posts: 333
    edited 2008-07-31 16:36
    I don't know what level of security is required but this type of encryption is inherently weak.

    The XOR method as applied here is basically a stream cipher with a repeating key, the only way to make it stronger is to have a random byte stream as the key the same length as the plain text message.

    This would implement a Vernan cipher AKA "one time pad" which is impossible to break as long as the key is random for each message.

    Cheers
  • Sniper KingSniper King Posts: 221
    edited 2008-07-31 16:48
    OK, Thank you everyone!· I got it to work finally.· I have attached the code.· This cipher needs to be strong enough so that the people monitoring can't read it for at least an hour or so.· This is going to be used in the "Sand box" in Iraq for a gps application.· Since the people using it will be moving very constantly,·the bad guys would really need to know the key to get the information in time to be of any use.· Since we will be shifting keys every few hours, this should be strong enough to be useful.· thank you all again!



    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    ·- Was it pin 11 or 26?· Hmmm....··I think the smell of smoke tells the whole story.· Must be 26.



    Michael King
    Application Engineer
    R&D
    Digital Technology Group
  • PraxisPraxis Posts: 333
    edited 2008-07-31 16:55
    Hi Michael,

    I should point out that with a modern laptop doing some number crunching this type on encryption will take way less than an hour to break.

    We have in the past done similar things in similar places and I would suggest that you should change the key on every location report.

    Just a thought.






    Here is a nice wikipedia article on the subject:
    en.wikipedia.org/wiki/Stream_cipher_attack

    Post Edited (Praxis) : 7/31/2008 5:10:16 PM GMT
  • Sniper KingSniper King Posts: 221
    edited 2008-07-31 17:11
    I also added it to the Object exchange. it is called XOR Cipher.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    ·- Was it pin 11 or 26?· Hmmm....··I think the smell of smoke tells the whole story.· Must be 26.



    Michael King
    Application Engineer
    R&D
    Digital Technology Group
  • PraxisPraxis Posts: 333
    edited 2008-07-31 17:56
    I think the "good Guys" are going to be the equivalent of an "all you can eat buffet" for the bad guys.

    Just a thought[noparse]:)[/noparse]
  • Phil Pilgrim (PhiPi)Phil Pilgrim (PhiPi) Posts: 23,514
    edited 2008-07-31 17:57
    Michael,

    Just out of curiosity:

    1. How long is a typical key with respect to a typical message?

    2. How many messages are sent with a given key?

    3. Is the key random characters, or a sequence of words?

    4. How are the keys distributed?

    5. What does a typical message look like? Just numbers (lat/long)?

    Can you provide the forum members a group of encoded messages (using a typical key) to crack? 'Better to have it broken here and know about so you can strengthen it than to have the bad guys do it and not know about it!

    -Phil

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    'Still some PropSTICK Kit bare PCBs left!
  • PraxisPraxis Posts: 333
    edited 2008-07-31 18:01
    If the GPS data is a NMEA string GGA/RMC for example, then a lot of that data does not update frequently i.e. the hours in the time, the date, the minutes in that Lat/Lon etc.

    If that is the case then a reverse engineer of the XOR'ed string will be easy to achieve as the NMEA string length more or less stays the same length, the problem is compounded by the regular spacing of the field delimiters namely the commas.

    Cheers


    An example, If it is an NMEA string then because of the field delimiters its easy to separate out the lat/lon fields.
    Now we know that these are numeric values only, in the format of Lat: DDMM.mmmm and Lon: DDDMM.mmmm.
    Even a brute force approach checking for a numeric result will be fairly quick.

    Just a thought.

    Post Edited (Praxis) : 7/31/2008 6:18:09 PM GMT
  • TimmooreTimmoore Posts: 1,031
    edited 2008-07-31 18:32
    xor underlies most stream ciphers. The question is how you generate the key and how often you use the key. You probably want to add a mechanism that takes a shared secret and generates keys from it. Using a secret and a incrementing number feed through a good one-way hash is a good option. The number can be sent in the clear to the receiver as long as the secret is a secret and the one-way hash is good just dont use the same number twice with teh same shared secret.
  • PraxisPraxis Posts: 333
    edited 2008-07-31 18:52
    Michael,

    Why don't you post an example of one your encrypted (using the posted spin code algorithm) GPS messages for use to try and decode ?



    Add:
    Another problem that comes to mind is key synchronization, that would require some type of public key implementation.

    Post Edited (Praxis) : 7/31/2008 7:34:33 PM GMT
  • TimmooreTimmoore Posts: 1,031
    edited 2008-07-31 21:03
    The difficultly of decrypting the output is dependent on the key strength. If the key as a random bit string longer than the input message and you have 1 output using that key then breaking it is hard. If the key is Sniper's name and the input string is longer than that then its easy.
    Many good ciphers use Xor, its dependent on how you generate and distribute the key. if you have a good scheme for key generation then xor is good.
  • John AbshierJohn Abshier Posts: 1,116
    edited 2008-08-01 01:44
    Cryptography looks easy, is fiendishly difficult in practice. Shifting the keys every few hours looks to be difficutt. Entering a key every few hours is a pain and the logistics of distributing that many keys is a problem. If an algrorithm is used to generate the key, that can be the weakness the enemy attacks.

    I have been retired for a decade or so. The rules for the military used to be quite explicit that all codes, cyphers, and keys (the system and the items) had to be approved or provided by the National Security Agency. We got slapped for using our own.
  • Phil Pilgrim (PhiPi)Phil Pilgrim (PhiPi) Posts: 23,514
    edited 2008-08-01 02:12
    John,

    I couldn't agree more, albeit without the benefit of your boots-on-the-ground experience. This whole thread worries me, since lives could be at stake; and I don't consider the matter closed by any means. I hate to use the word "cavalier"; but, under the circumstances, a lack of qualified vetting would certainly put the proffered algorithm in that category.

    -Phil

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    'Still some PropSTICK Kit bare PCBs left!
  • PraxisPraxis Posts: 333
    edited 2008-08-01 06:37
    Phil Pilgrim (PhiPi) said...

    This whole thread worries me, since lives could be at stake


    My point exactly.
  • OwenSOwenS Posts: 173
    edited 2008-08-01 11:16
    EFF said...

    In 1998 the EFF built Deep Crack for less than $250,000. In response to DES Challenge II-2, on July 17, 1998, Deep Crack decrypted a DES-encrypted message after only 56 hours of work, winning $10,000.

    That was 1998. 10 years ago. The machine would be both less expensive today and cheaper.

    Your encryption method is MANY orders of magnitude weaker than DES. And it suffers from the fact that the plaintext format is partially known: We know that it's a GPS string and that it's in ASCII format. That narrows down the characters to at most 96. Someone cracking this now just needs to brute force a 4 byte string and discard anything which has bytes outside this region. Then, from these, it cracks again, until its narrowed down the list to the point where a human can find something which looks correct.

    THIS IS VERY EASY. On a modern PC, cracking it shouldn't take more than a minute.

    DO NOT OVERESTIMATE HOW SECURE YOUR ENCRYPTION IS. ALWAYS OVERSPECIFY TO SOMETHING STRONGER THAN WHAT YOU NEED.
    I CAN TELL YOU FIRST HAND THAT SECURITY IS NOT EASY.

    Then again, if this is going out for any millitary purpose, then it shouldn't be accepted anyway. All the millitaries I know of require that a cryptographically secure encryption system be used.
  • PraxisPraxis Posts: 333
    edited 2008-08-01 13:26
    The level of encryption is also relevant to time sensitivity i.e. say after 1 hour even if the information is decrypted then it is old news already etc etc.

    The "sand box" application would be I guess for civilian contractors, keep in mind that the old information is also useful as it gives a history of locations, routes etc.

    I would suggest that you use the Blowfish cipher as posted by Paul Baker.

    Just a thought.
  • VIRANDVIRAND Posts: 656
    edited 2008-08-01 17:12
    If you send a message in so much as Pig Latin it gets marked as a secret and looked at twice.
    XOR codes are very easily recognized as such and very easily cracked.
    Got a 256 bit key? Whatever it is, it might as well be 256 zeroes or ones.
    If your phone uses Code Division Multiplexing (CDMA), you can be eavesdropped even if
    you have a continuous True Random Number Generator as your code key.
    Common Knowledge: Code Division Multiplex signal = (Your Voice) XOR (Pseudo Random Number Generator)
  • TimmooreTimmoore Posts: 1,031
    edited 2008-08-01 18:41
    Xor is a common scheme for encryption - see http://en.wikipedia.org/wiki/Block_cipher_modes_of_operation the CTR section. The strength of the encryption is dependent on the crypto strength of the key stream generator not on the xor function.
  • Sniper KingSniper King Posts: 221
    edited 2008-08-03 06:07
    Ok, this is getting heavy!· To put some fears to rest.· I have added two additional layers to this cipher.·



    1.· The addition of 48 to the ascii cypher byte has been changed to a random number.

    2.· The position in the key where the· encryption starts·from is also random.

    3.· The key is 64 bytes long and are·values from 0 to 254. These keys are randomly generated.



    In my tests sending the same message for over an hour to my computer for processing, I never saw the same encrypted message twice. My software captured every message and compared.

    The messages being sent are letters, numbers and a few operators.· I will post 20 encrypted strings of the same message for you all on monday and lets see what happens.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    ·- Was it pin 11 or 26?· Hmmm....··I think the smell of smoke tells the whole story.· Must be 26.



    Michael King
    Application Engineer
    R&D
    Digital Technology Group
Sign In or Register to comment.