Advertisement
Guest User

Untitled

a guest
Jun 25th, 2019
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.82 KB | None | 0 0
  1. import sys
  2. sys.path.append('/home/pi/lpz/source')
  3.  
  4. import RPi.GPIO as GPIO
  5. import MFRC522
  6. import signal
  7.  
  8. import aes
  9.  
  10. continue_reading = True
  11.  
  12. cardData = ['1','1','1','-','1', '1', '1', '1', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0']
  13.  
  14. ativaData = ['\0','\0','\0','4D','0', 'a', 't', 'i', 'v', 'o', '50', '\0', '\0', '\0', '\0', '\0']
  15.  
  16. def encryptToWrite(data):
  17.     # Converte os dados (list) para str
  18.     strData = ''.join(str(e) for e in data)
  19.  
  20.     # Realiza a cifragem Rijindael
  21.     cypher = aes.encrypt(strData)
  22.    
  23.     # Converte a cifra (str) para list
  24.     cypherData = []
  25.     for c in range(0, len(cypher)-1, 2):
  26.         cypherData.append(int(cypher[c:c+2], 16))
  27.    
  28.     # Retorna o dado
  29.     return cypherData
  30.  
  31. def end_read(signal,frame):
  32.     global continue_reading
  33.     print("Ctrl+C captured, ending read.")
  34.     continue_reading = False
  35.     GPIO.cleanup()
  36.  
  37. signal.signal(signal.SIGINT, end_read)
  38.  
  39. MIFAREReader = MFRC522.MFRC522()
  40.  
  41. while continue_reading:
  42.     # Faz a requisição do cartão se detectado
  43.     (status,TagType) = MIFAREReader.MFRC522_Request(MIFAREReader.PICC_REQIDL)
  44.     if status == MIFAREReader.MI_OK:
  45.         print("Card detected")
  46.    
  47.     # Previne conflito
  48.     (status,uid) = MIFAREReader.MFRC522_Anticoll()
  49.     if status == MIFAREReader.MI_OK:
  50.         print("Card read UID: %s,%s,%s,%s" % (uid[0], uid[1], uid[2], uid[3]))
  51.         key = [0xFF,0xFF,0xFF,0xFF,0xFF,0xFF]
  52.         MIFAREReader.MFRC522_SelectTag(uid)
  53.  
  54.         # Sector 1
  55.         status = MIFAREReader.MFRC522_Auth(MIFAREReader.PICC_AUTHENT1A, 4, key, uid)
  56.         if status == MIFAREReader.MI_OK:
  57.             print("Sector 1 -> MI_OK")
  58.             print("Write -> Sector 1 -> Block 4")
  59.             MIFAREReader.MFRC522_Write(4, encryptToWrite(cardData))
  60.            
  61.             # Read block 4
  62.             MIFAREReader.MFRC522_Read(4)
  63.             print("Read -> Sector 1 -> Block 4")
  64.             buff = MIFAREReader.MFRC522_Read(4)
  65.             buff = buff.split(',')
  66.            
  67.             data = ''.join(str(e) for e in buff)
  68.             print('Data: %s\n'%data)
  69.  
  70.         status = MIFAREReader.MFRC522_Auth(MIFAREReader.PICC_AUTHENT1A, 8, key, uid)
  71.         if status == MIFAREReader.MI_OK:
  72.             print("Sector 1 -> MI_OK")
  73.             print("Write -> Sector 2 -> Block 8")
  74.             MIFAREReader.MFRC522_Write(8, encryptToWrite(ativaData))
  75.            
  76.             # Read block 4
  77.             MIFAREReader.MFRC522_Read(8)
  78.             print("Read -> Sector 2 -> Block 8")
  79.             buff = MIFAREReader.MFRC522_Read(8)
  80.             buff = buff.split(',')
  81.            
  82.             data = ''.join(str(e) for e in buff)
  83.             print('Data: %s\n'%data)
  84.  
  85.         # Stop
  86.         MIFAREReader.MFRC522_StopCrypto1()
  87.  
  88.         # Make sure to stop reading for cards
  89.         continue_reading = False
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement