0

请原谅我对 Objective-C 的缺乏经验。我只玩了几个星期。

我正在尝试测试 Apple 加密和解密数据的方法(在本例中为 NSString)。最终目标是让用户在文本区域中输入内容,然后对其进行加密。

我在 Xcode 中使用一个基本的单视图应用程序并添加到这两个文件中(从这里):

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 类的一个类别。这意味着它用两种方法“扩展”了 NSData 类:- (NSData *)AES256EncryptWithKey:(NSString *)key;- (NSData *)AES256DecryptWithKey:(NSString *)key;. 它们都返回 NSData 并将一个 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 回答