views:

229

answers:

6

I'm looking for a simple encryption tutorial, for encoding a string into another string. I'm looking for it in general mathematical terms or psuedocode; we're doing it in a scripting language that doesn't have access to libraries.

We have a Micros POS ( point of sale ) system and we want to write a script that puts an encoded string on the bottom of receipts. This string is what a customer would use to log on to a website and fill out a survey about the business.

So in this string, I would like to get a three-digit hard-coded location identifier, the date, and time; e.g.: 0010912041421 Where 001 is the location identifier, 09 the year, 12 the month, and 04 the day, and 1421 the military time ( 2:41 PM ). That way we know which location the respondent visited and when.

Obviously if we just printed that string, it would be easy for someone to crack the 'code' and fill out endless surveys at our expense, without having actually visited our stores. So if we could do a simple encryption, and decode it with a pre-set key, that would be great. The decoding would take place on the website.

The encrypted string should also be about the same number of characters, to lessen the chance of people mistyping a long arbitrary string.

A: 

Which language are you using/familiar with?

The Rijndael website has c source code to implement the Rijndael algorithm. They also have pseudo code descriptions of how it all works. Which is probably the best you could go with. But most of the major algorithms have source code provided somewhere.

If you do implement your own Rijndael algorithm, then be aware that the Advanced Encryption Standard limits the key and block size. So if you want to be cross compatible you will need to use those sizes I think 128 key size and 128, 192, 256 key sizes.

Rolling your own encryption algorithm is something that you should never do if you can avoid it. So finding a real algorithm and implementing it if you have to is definitely a better way to go.

Another alternative that might be easier is DES, or 3DES more specifically. But I don't have a link handy. I'll see if I can dig one up.

EDIT: This link has the FIPS standard for DES and Triple DES. It contains all the permutation tables and such, I remember taking some 1s and 0s through a round of DES manually once. So it is not too hard to implement once you get going, just be careful not to change around the number tables. P and S Boxes they are called if I remember correctly.

If you go with these then use Triple DES not DES, 3DES actually uses two keys, doubling the key size of the algorithm, which is the only real weakness of DES. It has not been cracked as far as I know by anything other than brute force. 3DES goes through des using one key to encrypt, the other to decrypt, and the same one to encrypt again.

The Blowfish website also has links to implement the Blowfish algorithm in various languages.

Glenn Condron
+2  A: 

Encryption won't give you any integrity protection or authentication, which are what you need in this application. The customer knows when and where they made a purchase, so you have nothing to hide.

Instead, consider using a Message Authentication Code. These are often based on a cryptographic hash, such as SHA-1.

Also, you'll want to consider a replay attack. Maybe I can't produce my own code, but what's to stop me from coming back a few times with the same code? I assume you might serve more than one customer per minute, and so you'll want to accept duplicate timestamps from the same location.

In that case, you'll want to add a unique identifier. It might only be unique when combined with the timestamp. Or, you could simply extend the timestamp to include seconds or tenths of seconds.

erickson
This would certainly be a nice option. A MAC uses a key as well, so the MAC for a message can only be generated if you have the same key. This is important, otherwise they can modify the message as well as the hash...
Glenn Condron
Since this doesn't have to be cryptographically strong, you could use a MAC construction but based on a simple checksum which is easy to compute, like CRC32 (essentially `CRC(keystring || CRC(keystring || message))`, where `||` means concatenation. CRC is easy enough to do in simple script.
caf
Yes, using CRC in this way is what I suggested in this answer, to a question that is essentially the same: http://stackoverflow.com/questions/700514/adding-a-simple-mac-to-url-parameters/711646#711646
erickson
A: 

First off, I should point out that this is probably a fair amount of work to go through if you're not solving a problem you are actually having. Since you're going to want some sort of monitoring/analysis of your survey functionality anyway, you're probably better off trying to detect suspicious behavior after the fact and providing a way to rectify any problems.

I don't know if it would be feasible in your situation, but this is a textbook case for asymmetric crypto.

  1. Give each POS terminal it's own private key
  2. Give each POS terminal the public key of your server
  3. Have the terminal encrypt the date, location, etc. info (using the server's public key)
  4. Have the terminal sign the encrypted data (using the terminal's private key)
  5. Encode the results into human-friendly string (Base64?)
  6. Print the string on the receipt

You may run into problems with the length of the human-friendly string, though.

NOTE You may need to flip flop the signing and encrypting steps; I don't have my crypto reference book(s) handy. Please look this up in a reputable reference, such as Applied Cryptography by Schneier.

Hank Gay
Sign first, then encrypt. An RSA key will produce ciphertext of at least 1k. Encoded to text, it will be at least 160 characters, and that's using upper and lower case and a lot of symbols.
erickson
A: 

I've found Cryptographic Right Answers to be a helpful guide in choosing the right cryptographic primitives to use under various circumstances. It tells you what crypto/hash to use and what sizes are appropriate. It contains links to the various cryptographic primitives it refers to.

svenningsson
A: 

One way would be to use AES - taking the location, year, month, and day - encoding it using a private key and then tacking on the last 4 digits (the military time) as the inversion vector. You can then convert it to some form of Base32. You'll end up with something that looks like a product key. It may be too long for you though.

A slight issue would be that you would probably want to use more digits on the military time though since you could conceivably get multiple transactions on the same day from the same location within the same minute.

McAden
It's not a problem to have the same location+date+time from multiple people; we just don't want someone logging on, filling out a survey with bogus data, and then getting a coupon because they claimed to have visited yesterday when they didn't.
But if you're allowing duplicates you could have somebody fill it out multiple times, or hand it out to many others - thus defeating the purpose of not allowing "filling out endless surveys"
McAden
We've talked about the issue of people handing out codes to others, and in the 1.5 years we've been running the survey, it (surprisingly) hasn't been a problem.
A: 

What I want to use is XOR. It's simple enough that we can do it in the proprietary scripting language ( we're not going to be able to do any real encryption in it ), and if someone breaks it, they we can change the key easily enough.