//////////////////////////////////////////////////////////////////// // // Mifare Card Explorer for Mac // // Created by Francesco Germinara // // Freeware (c) 2005-2006 SofTech di F.Germinara - www.germinara.it. // // For information please contact me at info@germinara.it //////////////////////////////////////////////////////////////////// // History: // // Description: // //////////////////////////////////////////////////////////////////// #import "FGMifareCard.h" @implementation FGMifareCard - (id) init { self = [super init]; if (self != nil) { strCardType=[[NSString alloc] init]; strCardUID=[[NSString alloc] init]; [self ClearData]; } return self; } - (void) dealloc { [strCardType release]; [strCardUID release]; [super dealloc]; } - (void) ClearData{ [strCardType release]; strCardType=[[NSString alloc] initWithString:@"Unknow"]; strCardUID=[[NSString alloc] initWithString:@""]; nMethodType=UNKNOW_METHOD; memset(&cardInfo,0x00,sizeof(CARD_DATA)); [self SetKeyA:0xFF b1:0xFF b2:0xFF b3:0xFF b4:0xFF b5:0xFF]; [self SetKeyB:0xFF b1:0xFF b2:0xFF b3:0xFF b4:0xFF b5:0xFF]; } //Return UID as string object -(NSString*) Uid{ return strCardUID; } //Set Key A value -(void) SetKeyA:(unsigned char) byte0 b1:(unsigned char) byte1 b2:(unsigned char) byte2 b3:(unsigned char) byte3 b4:(unsigned char) byte4 b5:(unsigned char) byte5 { KeyA[0]=byte0; KeyA[1]=byte1; KeyA[2]=byte2; KeyA[3]=byte3; KeyA[4]=byte4; KeyA[5]=byte5; } //set KeyA as CString -(void) SetKeyAasStringValue:(NSString*) byte0 b1:(NSString*) byte1 b2:(NSString*) byte2 b3:(NSString*) byte3 b4:(NSString*) byte4 b5:(NSString*) byte5 { KeyA[0]=[self atoh:byte0]; KeyA[1]=[self atoh:byte1]; KeyA[2]=[self atoh:byte2]; KeyA[3]=[self atoh:byte3]; KeyA[4]=[self atoh:byte4]; KeyA[5]=[self atoh:byte5]; } //set KeyB as CString -(void) SetKeyBasStringValue:(NSString*) byte0 b1:(NSString*) byte1 b2:(NSString*) byte2 b3:(NSString*) byte3 b4:(NSString*) byte4 b5:(NSString*) byte5 { KeyB[0]=[self atoh:byte0]; KeyB[1]=[self atoh:byte1]; KeyB[2]=[self atoh:byte2]; KeyB[3]=[self atoh:byte3]; KeyB[4]=[self atoh:byte4]; KeyB[5]=[self atoh:byte5]; } //Set Key B value -(void) SetKeyB:(unsigned char) byte0 b1:(unsigned char) byte1 b2:(unsigned char) byte2 b3:(unsigned char) byte3 b4:(unsigned char) byte4 b5:(unsigned char) byte5 { KeyB[0]=byte0; KeyB[1]=byte1; KeyB[2]=byte2; KeyB[3]=byte3; KeyB[4]=byte4; KeyB[5]=byte5; } //get KeyA -(void) GetKeyA:(unsigned char *) byte0 b1:(unsigned char *) byte1 b2:(unsigned char *) byte2 b3:(unsigned char *)byte3 b4:(unsigned char *) byte4 b5:(unsigned char *) byte5 { *byte0=KeyA[0]; *byte1=KeyA[1]; *byte2=KeyA[2]; *byte3=KeyA[3]; *byte4=KeyA[4]; *byte5=KeyA[5]; } //get KeyB -(void) GetKeyB:(unsigned char *) byte0 b1:(unsigned char *) byte1 b2:(unsigned char *) byte2 b3:(unsigned char *)byte3 b4:(unsigned char *) byte4 b5:(unsigned char *) byte5 { *byte0=KeyB[0]; *byte1=KeyB[1]; *byte2=KeyB[2]; *byte3=KeyB[3]; *byte4=KeyB[4]; *byte5=KeyB[5]; } //Return Key A value 6 BYTES ARRAY -(void) GetKeyA:(unsigned char *) key{ memcpy(key,KeyA,6); } //Return Key B value 6 BYTES ARRAY -(void) GetKeyB:(unsigned char *) key{ memcpy(key,KeyB,6); } //Set Key A value 6 BYTES -(void) SetKeyA:(unsigned char *) key{ memcpy(KeyA,key,6); } //Set Key B value 6 BYTES -(void) SetKeyB:(unsigned char *) key{ memcpy(KeyB,key,6); } //Return Key A value as string object -(NSString*) GetKeyA{ NSMutableString *strResult; NSString *strTmp; strResult=[NSMutableString stringWithString:@""]; int nIndex=0; for(nIndex=0; nIndex< 6; nIndex++){ strTmp=[NSString stringWithFormat:@"%0.2X",KeyA[nIndex]]; [strResult appendString:strTmp]; } return strResult; } //Return Key B value as string object -(NSString*) GetKeyB{ NSMutableString *strResult; NSString *strTmp; strResult=[NSMutableString stringWithString:@""]; int nIndex=0; for(nIndex=0; nIndex< 6; nIndex++){ strTmp=[NSString stringWithFormat:@"%0.2X",KeyB[nIndex]]; [strResult appendString:strTmp]; } return strResult; } -(void) setNewKeyA:(NSString*) aNewKey{ unsigned char key[6]; int nByte=0; int nIndex=0; NSString *strAsciiValue; NSRange range; range.location=0; range.length=2; if([aNewKey length]==12){ for(nIndex=0; nIndex<12; nIndex+=2){ range.location=nIndex; strAsciiValue=[aNewKey substringWithRange:range]; key[nByte]=[self atoh:strAsciiValue]; nByte++; } [self SetKeyA:key[0] b1:key[1] b2:key[2] b3:key[3] b4:key[4] b5:key[5]]; } } -(void) setNewKeyB:(NSString*) aNewKey{ unsigned char key[6]; int nByte=0; int nIndex=0; NSString *strAsciiValue; NSRange range; range.location=0; range.length=2; if([aNewKey length]==12){ for(nIndex=0; nIndex<12; nIndex+=2){ range.location=nIndex; strAsciiValue=[aNewKey substringWithRange:range]; key[nByte]=[self atoh:strAsciiValue]; nByte++; } [self SetKeyB:key[0] b1:key[1] b2:key[2] b3:key[3] b4:key[4] b5:key[5]]; } } //Set UID as string object -(void) SetUid:(NSString*) CardUID{ [strCardUID release]; [CardUID retain]; strCardUID=CardUID; } //Get the Card Info structure -(CARD_DATA *) GetCardInfo{ return &cardInfo; } //Copy data object -(void) copyTo:(FGMifareCard*) destination{ [destination SetUid:[self Uid]]; [destination SetCardType:[self CardType]]; [destination SetMethodType:[self MethodType]]; memcpy(destination->KeyA,self->KeyA,6); memcpy(destination->KeyB,self->KeyB,6); memcpy(&(destination->cardInfo),&(self->cardInfo),sizeof(CARD_DATA)); } //Equal data object TRUE are Identical Card -(BOOL) equalTo:(FGMifareCard*) destination{ BOOL sts=memcmp(&(self->cardInfo.ChipID),&(destination->cardInfo.ChipID),SINGLE_UID_BYTES); return !sts; } //Get the Card Type Description - (NSString*) CardType{ return strCardType; } //Set Card Type Description - (void) SetCardType:(NSString *) str{ [str retain]; [strCardType release]; strCardType=str; } -(unsigned char) atoh:(NSString *)strValue{ int hexValue=0x00; char b0=0x00; char b1=0x00; if([strValue length]!=2) return 0; //error! b0=[strValue characterAtIndex:0]; b1=[strValue characterAtIndex:1]; if(b0 >='0' && b0 <='9') hexValue+=(b0 - '0') * 16; if(b0 >='A' && b0 <='F') hexValue+=((b0 - 'A')+10) * 16; if(b1 >='0' && b1 <='9') hexValue+=b1 - '0'; if(b1 >='A' && b1 <='F') hexValue+=(b1 - 'A')+10; return (unsigned char) hexValue; } -(void) SetMethodType:(int) meth{ nMethodType=meth; } -(int) MethodType{ return nMethodType; } @end