Forrest Keller
Posts: 3
Joined: Sat Aug 17, 2019 11:30 am

RFID MFRC522 Change Card Type (Wrong UID)

Sat Aug 17, 2019 11:38 am

I am using the RC522 connected to the GPIO pins on the Raspi, with MFRC522 acting as an interface between my python code and RFID scanner. I need to scan a Mifare Classic 1k Card, but my code is getting the wrong UID. The code displays the UID, but it is different to the correct value. I cannot figure out how to change the RFID protocol MFRC522 uses. Any help would be much appreciated.

Here is the code I am using:

Code: Select all

import RPi.GPIO as GPIO
import MFRC522
import signal

continue_reading = True

# Capture SIGINT for cleanup when the script is aborted
def end_read(signal,frame):
    global continue_reading
    print "Ctrl+C captured, ending read."
    continue_reading = False
    GPIO.cleanup()

# Hook the SIGINT
signal.signal(signal.SIGINT, end_read)

# Create an object of the class MFRC522
MIFAREReader = MFRC522.MFRC522()

# Welcome message
print "Welcome to the MFRC522 data read example"
print "Press Ctrl-C to stop."

# This loop keeps checking for chips. If one is near it will get the UID and authenticate
while continue_reading:
    
    # Scan for cards    
    (status,TagType) = MIFAREReader.MFRC522_Request(MIFAREReader.PICC_REQIDL)

    # If a card is found
    if status == MIFAREReader.MI_OK:
        print "Card detected"
    
    # Get the UID of the card
    (status,uid) = MIFAREReader.MFRC522_Anticoll()

    # If we have the UID, continue
    if status == MIFAREReader.MI_OK:

        # Print UID
        print "Card read UID: %s,%s,%s,%s" % (uid[0], uid[1], uid[2], uid[3])
    
        # This is the default key for authentication
        key = [0xFF,0xFF,0xFF,0xFF,0xFF,0xFF]
        
        # Select the scanned tag
        MIFAREReader.MFRC522_SelectTag(uid)

        # Authenticate
        status = MIFAREReader.MFRC522_Auth(MIFAREReader.PICC_AUTHENT1A, 8, key, uid)

        # Check if authenticated
        if status == MIFAREReader.MI_OK:
            MIFAREReader.MFRC522_Read(8)
            MIFAREReader.MFRC522_StopCrypto1()
        else:
            print "Authentication error"

drifterf
Posts: 134
Joined: Sat Dec 08, 2018 9:09 am
Location: Northants, England

Re: RFID MFRC522 Change Card Type (Wrong UID)

Sat Aug 17, 2019 4:17 pm

hi, i believe you will find that the encryption sometimes is down to the producer of the cards/tokens

i had this problem with using a wiegand reader as the bits etc varied as so did the card numbers.

Forrest Keller
Posts: 3
Joined: Sat Aug 17, 2019 11:30 am

Re: RFID MFRC522 Change Card Type (Wrong UID)

Mon Aug 19, 2019 1:35 am

The problem is I am implementing this into a pre-existing system of cards, so I need to be able to get the current UID to the to other form. Do you know any method to do this? Thanks!

Forrest Keller
Posts: 3
Joined: Sat Aug 17, 2019 11:30 am

Re: RFID MFRC522 Change Card Type (Wrong UID)

Mon Aug 19, 2019 1:36 am

My scanner is reading this UID: 11210193216144

But the UID I was supplied with is: 3629999472

User avatar
DougieLawson
Posts: 35805
Joined: Sun Jun 16, 2013 11:19 pm
Location: Basingstoke, UK
Contact: Website Twitter

Re: RFID MFRC522 Change Card Type (Wrong UID)

Mon Aug 19, 2019 8:00 am

You've got the wrong "endianness" for your big-endian number.

Switch the bits round to little-endian.
Note: Having anything humorous in your signature is completely banned on this forum. Wear a tin-foil hat and you'll get a ban.

Any DMs sent on Twitter will be answered next month.

This is a doctor free zone.

danjperron
Posts: 3391
Joined: Thu Dec 27, 2012 4:05 am
Location: Québec, Canada

Re: RFID MFRC522 Change Card Type (Wrong UID)

Mon Aug 19, 2019 1:01 pm

Hum,

The library from mxgxw is for the 4 bytes mifare! looks like you have a 7 bytes mifare card!


Also this library print the four bytes in decimals using a comma to separate them. I don't see the comma between the numbers on your UID.

Try to use my library which is based on mxgxw but modified to remove the GPIO, No need to connect the reset, and it will read the 4 ,7 and 10 bytes mifare card

https://github.com/danjperron/MFRC522-python

Return to “Python”