0

Objective-C の経験がないことをお許しください。私はそれで数週間しか遊んでいません。

データ (この場合は NSString) を暗号化および復号化するための Apple の方法をテストしようとしています。最終的な目標は、ユーザーがテキスト領域に何かを入力し、それを暗号化することです。

Xcode で基本的なシングルビュー アプリケーションを使用しており、これら 2 つのファイルに追加されています (ここから):

NSDataEncryption.h

#import <Foundation/Foundation.h>

@interface NSData (AES256) 
- (NSData *)AES256EncryptWithKey:(NSString *)key;
- (NSData *)AES256DecryptWithKey:(NSString *)key;
@end

および NSDataEncryption.m

#import "NSDataEncryption.h"

#import <CommonCrypto/CommonCryptor.h>

@implementation NSData (AES256)

- (NSData *)AES256EncryptWithKey:(NSString *)key {
    // 'key' should be 32 bytes for AES256, will be null-padded otherwise
    char keyPtr[kCCKeySizeAES256+1]; // room for terminator (unused)
    bzero(keyPtr, sizeof(keyPtr)); // fill with zeroes (for padding)

    // fetch key data
    [key getCString:keyPtr maxLength:sizeof(keyPtr) encoding:NSUTF8StringEncoding];

    NSUInteger dataLength = [self length];

    //See the doc: For block ciphers, the output size will always be less than or 
    //equal to the input size plus the size of one block.
    //That's why we need to add the size of one block here
    size_t bufferSize = dataLength + kCCBlockSizeAES128;
    void *buffer = malloc(bufferSize);

    size_t numBytesEncrypted = 0;
    CCCryptorStatus cryptStatus = CCCrypt(kCCEncrypt, kCCAlgorithmAES128, kCCOptionPKCS7Padding,
                                          keyPtr, kCCKeySizeAES256,
                                          NULL /* initialization vector (optional) */,
                                          [self bytes], dataLength, /* input */
                                          buffer, bufferSize, /* output */
                                          &numBytesEncrypted);
    if (cryptStatus == kCCSuccess) {
        //the returned NSData takes ownership of the buffer and will free it on deallocation
        return [NSData dataWithBytesNoCopy:buffer length:numBytesEncrypted];
    }

    free(buffer); //free the buffer;
    return nil;
}

- (NSData *)AES256DecryptWithKey:(NSString *)key {
    // 'key' should be 32 bytes for AES256, will be null-padded otherwise
    char keyPtr[kCCKeySizeAES256+1]; // room for terminator (unused)
    bzero(keyPtr, sizeof(keyPtr)); // fill with zeroes (for padding)

    // fetch key data
    [key getCString:keyPtr maxLength:sizeof(keyPtr) encoding:NSUTF8StringEncoding];

    NSUInteger dataLength = [self length];

    //See the doc: For block ciphers, the output size will always be less than or 
    //equal to the input size plus the size of one block.
    //That's why we need to add the size of one block here
    size_t bufferSize = dataLength + kCCBlockSizeAES128;
    void *buffer = malloc(bufferSize);

    size_t numBytesDecrypted = 0;
    CCCryptorStatus cryptStatus = CCCrypt(kCCDecrypt, kCCAlgorithmAES128, kCCOptionPKCS7Padding,
                                          keyPtr, kCCKeySizeAES256,
                                          NULL /* initialization vector (optional) */,
                                          [self bytes], dataLength, /* input */
                                          buffer, bufferSize, /* output */
                                          &numBytesDecrypted);

    if (cryptStatus == kCCSuccess) {
        //the returned NSData takes ownership of the buffer and will free it on deallocation
        return [NSData dataWithBytesNoCopy:buffer length:numBytesDecrypted];
    }

    free(buffer); //free the buffer;
    return nil;
}

@end

この質問に基づいて、次のようにメソッドを呼び出しています。

#include "NSDataEncryption.h"

//...

NSString * key = @"ThisIsAKey";
    NSDataEncryption *encryptionClass = [[NSDataEncryption alloc] init]; //Errors: "Use of undeclared identfier 'encryptionClass'" and "Use of undeclared identifer 'NSDataEncryption'"
    NSData * newData = [encryptionClass AES256EncryptionWithKey:key]; //Errors: "Use of undeclared identfier 'encryptionClass'" and "Use of undeclared identifer 'NSDataEncryption'"

これを main() 内と別のクラス (ViewController) の新しい関数内に配置しようとしました:

- (IBAction)someFunctionName { code here }

大きな疑問: Xcode が NSDataEncryption をクラスとして受け入れず、その関数 AES256EnryptionWithKey を呼び出せないのはなぜですか? アプリの別の場所で暗号化を実行する必要がありますか?

ありがとう!

4

2 に答える 2

1

NSDataEncryption はクラスではありません。標準の NSData クラスのカテゴリです。これは、 と の 2 つのメソッドで NSData クラスを「拡張」することを意味し- (NSData *)AES256EncryptWithKey:(NSString *)key;ます- (NSData *)AES256DecryptWithKey:(NSString *)key;。これらは両方とも NSData を返し、1 つの NSString をパラメーターとして受け取ります。

次のように呼び出します。

NSData *dataToBeEncrypted = [NSData data]; //Put your data here
NSString *key = @"ThisISAKEy";
NSData *newData = [dataToBeEncrypted AES256EncryptionWithKey:key];

文字列の暗号化/復号化には、次の方法を使用できます。

- (NSData*) encryptString:(NSString*)plaintext withKey:(NSString*)key {
    return [[plaintext dataUsingEncoding:NSUTF8StringEncoding] AES256EncryptWithKey:key];

}

- (NSString*) decryptData:(NSData*)ciphertext withKey:(NSString*)key {
    return [[[NSString alloc] initWithData:[ciphertext AES256DecryptWithKey:key]
                                  encoding:NSUTF8StringEncoding] autorelease];
}
于 2012-06-21T12:22:25.313 に答える
0
NSData *data = [myString dataUsingEncoding:NSUTF8StringEncoding];
NSData *encrypted = [data AES256EncryptionWithKey:@"RandomString"];
于 2012-06-21T12:34:26.663 に答える