この質問では、crypt() 関数を使用して iPhone でデータを暗号化する方法について説明します。別の方法として、iPhone にキーチェーンがありますか? もしそうなら、ログインの詳細を保存し、アプリケーションでそれらを取得するために、どのコードを使用してキーチェーンにアクセスしますか?
7 に答える
注意すべきもう 1 つの点: iPhone SDK の古いバージョン (2.x、3.x) を使用している場合、キーチェーン API はシミュレーターで機能しません。これにより、テスト時のフラストレーションを大幅に軽減できます。
使用できるキーチェーンがあります。コードについては、Apple の GenericKeychain サンプル アプリケーションを確認することをお勧めします。
私はバズアンダーソンのキーチェーン抽象化レイヤーが本当に好きで、イェンスアルフケのMYCryptoが使用可能な状態に達するのを心待ちにしています。後者は、同じコードを使用してMac OS XとiPhoneでの使用を許可するという有能な仕事をしますが、その機能はキーチェーンの小さなサブセットを模倣しているだけです。
これは、キーチェーンにキーと値のペアを格納するために使用するものです。プロジェクトに Security.framework を必ず追加してください
#import <Security/Security.h>
// -------------------------------------------------------------------------
-(NSString *)getSecureValueForKey:(NSString *)key {
/*
Return a value from the keychain
*/
// Retrieve a value from the keychain
NSDictionary *result;
NSArray *keys = [[[NSArray alloc] initWithObjects: (NSString *) kSecClass, kSecAttrAccount, kSecReturnAttributes, nil] autorelease];
NSArray *objects = [[[NSArray alloc] initWithObjects: (NSString *) kSecClassGenericPassword, key, kCFBooleanTrue, nil] autorelease];
NSDictionary *query = [[NSDictionary alloc] initWithObjects: objects forKeys: keys];
// Check if the value was found
OSStatus status = SecItemCopyMatching((CFDictionaryRef) query, (CFTypeRef *) &result);
[query release];
if (status != noErr) {
// Value not found
return nil;
} else {
// Value was found so return it
NSString *value = (NSString *) [result objectForKey: (NSString *) kSecAttrGeneric];
return value;
}
}
// -------------------------------------------------------------------------
-(bool)storeSecureValue:(NSString *)value forKey:(NSString *)key {
/*
Store a value in the keychain
*/
// Get the existing value for the key
NSString *existingValue = [self getSecureValueForKey:key];
// Check if a value already exists for this key
OSStatus status;
if (existingValue) {
// Value already exists, so update it
NSArray *keys = [[[NSArray alloc] initWithObjects: (NSString *) kSecClass, kSecAttrAccount, nil] autorelease];
NSArray *objects = [[[NSArray alloc] initWithObjects: (NSString *) kSecClassGenericPassword, key, nil] autorelease];
NSDictionary *query = [[[NSDictionary alloc] initWithObjects: objects forKeys: keys] autorelease];
status = SecItemUpdate((CFDictionaryRef) query, (CFDictionaryRef) [NSDictionary dictionaryWithObject:value forKey: (NSString *) kSecAttrGeneric]);
} else {
// Value does not exist, so add it
NSArray *keys = [[[NSArray alloc] initWithObjects: (NSString *) kSecClass, kSecAttrAccount, kSecAttrGeneric, nil] autorelease];
NSArray *objects = [[[NSArray alloc] initWithObjects: (NSString *) kSecClassGenericPassword, key, value, nil] autorelease];
NSDictionary *query = [[[NSDictionary alloc] initWithObjects: objects forKeys: keys] autorelease];
status = SecItemAdd((CFDictionaryRef) query, NULL);
}
// Check if the value was stored
if (status != noErr) {
// Value was not stored
return false;
} else {
// Value was stored
return true;
}
}
ユーザーがアプリを削除しても、これらのキー/値は削除されないことに注意してください。ユーザーがアプリを削除してから再インストールしても、キー/値には引き続きアクセスできます。
また、AppIDを生成するときに、複数のアプリケーションが同じキーチェーン情報にアクセスする場合は、ワイルドカードAppID(#####。com.prefix。*)を生成する必要があることにも注意してください...
GenericKeychain サンプルの最新バージョン 1.2 で、Apple は iPhone シミュレーターでも動作するキーチェーン ラッパーを提供します。詳細については、この記事をご覧ください: http://dev-metal.blogspot.com/2010/08/howto-use-keychain-in-iphone-sdk-to.html
Mr.Granoff のもう 1 つの優れたラッパー クラスを次に示し ます https://github.com/granoff/Lockbox ありがとう