Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import sys
- sys.path.append('/home/pi/lpz/source')
- import RPi.GPIO as GPIO
- import MFRC522
- import signal
- import aes
- continue_reading = True
- cardData = ['1','1','1','-','1', '1', '1', '1', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0']
- ativaData = ['\0','\0','\0','4D','0', 'a', 't', 'i', 'v', 'o', '50', '\0', '\0', '\0', '\0', '\0']
- def encryptToWrite(data):
- # Converte os dados (list) para str
- strData = ''.join(str(e) for e in data)
- # Realiza a cifragem Rijindael
- cypher = aes.encrypt(strData)
- # Converte a cifra (str) para list
- cypherData = []
- for c in range(0, len(cypher)-1, 2):
- cypherData.append(int(cypher[c:c+2], 16))
- # Retorna o dado
- return cypherData
- def end_read(signal,frame):
- global continue_reading
- print("Ctrl+C captured, ending read.")
- continue_reading = False
- GPIO.cleanup()
- signal.signal(signal.SIGINT, end_read)
- MIFAREReader = MFRC522.MFRC522()
- while continue_reading:
- # Faz a requisição do cartão se detectado
- (status,TagType) = MIFAREReader.MFRC522_Request(MIFAREReader.PICC_REQIDL)
- if status == MIFAREReader.MI_OK:
- print("Card detected")
- # Previne conflito
- (status,uid) = MIFAREReader.MFRC522_Anticoll()
- if status == MIFAREReader.MI_OK:
- print("Card read UID: %s,%s,%s,%s" % (uid[0], uid[1], uid[2], uid[3]))
- key = [0xFF,0xFF,0xFF,0xFF,0xFF,0xFF]
- MIFAREReader.MFRC522_SelectTag(uid)
- # Sector 1
- status = MIFAREReader.MFRC522_Auth(MIFAREReader.PICC_AUTHENT1A, 4, key, uid)
- if status == MIFAREReader.MI_OK:
- print("Sector 1 -> MI_OK")
- print("Write -> Sector 1 -> Block 4")
- MIFAREReader.MFRC522_Write(4, encryptToWrite(cardData))
- # Read block 4
- MIFAREReader.MFRC522_Read(4)
- print("Read -> Sector 1 -> Block 4")
- buff = MIFAREReader.MFRC522_Read(4)
- buff = buff.split(',')
- data = ''.join(str(e) for e in buff)
- print('Data: %s\n'%data)
- status = MIFAREReader.MFRC522_Auth(MIFAREReader.PICC_AUTHENT1A, 8, key, uid)
- if status == MIFAREReader.MI_OK:
- print("Sector 1 -> MI_OK")
- print("Write -> Sector 2 -> Block 8")
- MIFAREReader.MFRC522_Write(8, encryptToWrite(ativaData))
- # Read block 4
- MIFAREReader.MFRC522_Read(8)
- print("Read -> Sector 2 -> Block 8")
- buff = MIFAREReader.MFRC522_Read(8)
- buff = buff.split(',')
- data = ''.join(str(e) for e in buff)
- print('Data: %s\n'%data)
- # Stop
- MIFAREReader.MFRC522_StopCrypto1()
- # Make sure to stop reading for cards
- continue_reading = False
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement