I have read many pages about encryption but I am really stuck on this. For access control to a building I want to print a 10 character ASCII code on a ticket (as a barcode).
The barcode reader (controlled by an Arduino or mini PC) is not connected to the PC which creates and prints the barcodes.
When both server and client have the same keys, is it possible to validate the codes?
EDIT: My first attempt was to convert the current date in to binary, and then to ASCII with base64. The problem was that I ended up with a too big code (11 characters).
Considerating everything I came to the conclusion that it would be better too convert the date and time directly into ASCII characters. ASCII characters become readable at position 33, so year 2015 becomes 15 + 33 = ASCII character 0
In this way Year + Month + Day + Hour + Second + Validtime only takes 7 characters.
EDIT2: I tried different encryptions en base encodings. Because year and month don't vary much, the generated code before encryption looks very much alike. When I use ARC4 encryption, the generated codes are also very similar.
Using DES3 the generated codes are not alike. I have included the two encryption methods in the code below. Because I use Python 2.7 I have to use the base85 module from the mom package. In Python v3 base85 is included.
Here is the code:
from datetime import datetime
from Crypto.Cipher import DES3
from Crypto.Cipher import ARC4
import base64
from mom.codec import base85
sleutel="Dsdfk3#$294ddkwlsslwEefm"
now = datetime.now()
year = chr((now.year - 2000)+33)
month = chr((now.month)+33)
day = chr((now.day)+33)
hour = chr((now.hour)+33)
minute = chr((now.minute)+33)
seconds = chr((now.second)+33)
#validtime from 0 till 9 in this case just a random 4. So 0 would be a few hours and 5 a week etc.
validtime = chr(4+33)
key = ''.join([year,month,day,hour,minute,seconds,validtime])
print key
obj1 = ARC4.new(sleutel)
obj2 = DES3.new(sleutel)
cipher_text = obj1.encrypt(key)
cipher_text1 = obj2.encrypt(key+ 'a')
print "Base64 ARC4 encrypted:", base64.b64encode(cipher_text)
print "Base64 DES3 encrypted:", base64.b64encode(cipher_text1)
print "Base85 ARC4 encrypted:", base85.b85encode(cipher_text)
print "Base85 DES3 encrypted:", base85.b85encode(cipher_text1)
Because I added seconds, every code is unique (as long as it is impossible to print two tickets in one second)