1
다음 코드를 사용하여 키 체인에서 비밀번호를 가져옵니다.osx에서 SecKeychainFindGenericPassword를 사용할 때 passwordData에 추가 데이터를 가져 오는 중
-(OSStatus *)getPasswordFromKeyChain:(NSString *)username{
OSStatus status;
const char *cService_name = "Mac App";
UInt32 service_length = strlen(cService_name);
const char *cUser_name = [username cStringUsingEncoding:NSUTF8StringEncoding];
UInt32 username_length = strlen(cUser_name);
void *passwordData = nil;
SecKeychainItemRef itemRef = nil;
UInt32 passwordLength = nil;
status = SecKeychainFindGenericPassword(
NULL, // default keychain
service_length, // length of service name
cService_name, // service name
username_length,// length of account name
cUser_name, // account name
&passwordLength, // length of password
passwordData, // pointer to password data
NULL // the item reference
);
NSLog(@"%s",passwordData);
status = SecKeychainItemFreeContent (NULL, //No attribute data to release
passwordData); //Release data buffer allocated by SecKeychainFindGenericPassword
return status;
}
성공 상태가되면 제대로 작동합니다.
그러나 얻은 암호를 인쇄하려고하면 다음 결과가 표시됩니다. - 암호가 6 자 이상이면 실제 암호 끝에 가비지 문자가 표시됩니다. 예 :
2012-11-17 12:01:28.731 MAC App[2042:303] sssssss`—~
암호가 6 자보다 작거나 같으면 올바른 암호를 얻습니다. 예 :
2012-11-17 12:01:33.244 MAC App[2042:303] ssssss
제 질문은 왜 이러한 가비지 문자가 마지막에 오는 이유입니까? 이 문제를 어떻게 해결할 수 있습니까? 내 응용 프로그램에서이 암호를 사용해야합니다.