I'm currently working on a project that involves reading and writing data to MIFARE cards. I've successfully managed to read and write data using the KEY_DEFAULT, but now I want to enhance the security of my application by using a custom key for these operations.
Here's what I've achieved so far:
- Successful read/write operations using KEY_DEFAULT.
- Established communication with the MIFARE card and retrieved card information.
However, I'm unsure about the process of setting and using a custom key for secure read/write operations. I understand that using the default key poses security risks, and I'd like to implement my own key management system.
Here's a snippet of my current code for reference:
val isAuthenticated =
isAuthenticated(
mif = mif,
sectorIndex = SECTOR_INDEX,
customKey = MifareClassic.KEY_DEFAULT
)
private fun readDataFromBlock(mif: MifareClassic?, blockIndex: Int, sectorIndex: Int) {
val block = mif?.readBlock(blockIndex)
val readData = String(block!!, Charsets.US_ASCII).trimEnd('\u0000')
Log.e(TAG, readData)
if (blockIndex == BLOCK_INDEX) {
val dataFromReadOperation = try {
readData.toInt()
} catch (_: Exception) {
0
}
}
Log.d(
TAG,
"Sector: $sectorIndex Block: $blockIndex $readData"
)
}
private fun isAuthenticated(
mif: MifareClassic,
sectorIndex: Int? = SECTOR_INDEX,
customKey: ByteArray
): Boolean {
var isAuthenticated = false
Log.e(TAG, "" + mif.sectorToBlock(SECTOR_INDEX) + " " + sectorIndex)
if (mif.authenticateSectorWithKeyA(sectorIndex!!, customKey)) {
isAuthenticated = true
Log.d(TAG, "Authentication successful with custom key A")
} else if (mif.authenticateSectorWithKeyB(sectorIndex, customKey)) {
isAuthenticated = true
Log.d(TAG, "Authentication successful with custom key B")
} else {
Log.d(TAG, "Authorization denied")
}
return isAuthenticated
}
so as per the mentioned code above everything can be read and write through the KEY_DEFAULT. I want my custom keys to play role in so that nobody else can read and write the data in it without the keys.