Objective-C 中的 AES 字符串加密 [英] AES string encryption in Objective-C

查看:19
本文介绍了Objective-C 中的 AES 字符串加密的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的 Objective-C 应用需要文本/字符串加密(特别是 ).

My Objective-C App requires text / string encryption (specifically nsstring).

我知道 AES 是可供消费者使用的最安全的加密方法.我也了解如何将字符串转换为数据并返回......(只是一个初学者).

I know AES is the most secure encryption method available for consumer use. I also understand how to convert strings to data and back... (just a beginner).

许多关于 AES 加密的网页和问答都不清楚,而且没有一个说明如何使用给定的代码.例如,一个网页可能会说:这是代码……这是它的作用……"但没有解释如何使用它.

Many webpages and Q/As about encryption with AES are unclear, and none of them state how to use the code given. For example, a webpage might say: "here is the code... here is what it does..." but no explanation for how to use it.

我通过大量研究发现了这段代码:

I've found this code through lots of research:

#import "<CommonCrypto/CommonCryptor.h>"
@implementation NSMutableData(AES)

对于加密:

- (NSMutableData*) EncryptAES:(NSString *)key {
    char keyPtr[kCCKeySizeAES256+1];
    bzero( keyPtr, sizeof(keyPtr) );

    [key getCString: keyPtr maxLength: sizeof(keyPtr) encoding: NSUTF16StringEncoding];
    size_t numBytesEncrypted = 0;

    NSUInteger dataLength = [self length];

    size_t bufferSize = dataLength + kCCBlockSizeAES128;
    void *buffer = malloc(bufferSize);

    NSMutableData *output = [[NSData alloc] init];

    CCCryptorStatus result = CCCrypt(kCCEncrypt, kCCAlgorithmAES128, kCCOptionPKCS7Padding, keyPtr, kCCKeySizeAES256, NULL, [self mutableBytes], [self length], buffer, bufferSize, &numBytesEncrypted);

    output = [NSMutableData dataWithBytesNoCopy:buffer length:numBytesEncrypted];

    if(result == kCCSuccess) {
        return output;
    }
        return NULL;
    }

用于解密:

- (NSMutableData*)DecryptAES: (NSString*)key andForData:(NSMutableData*)objEncryptedData {

    char  keyPtr[kCCKeySizeAES256+1];
    bzero( keyPtr, sizeof(keyPtr) );

    [key getCString:keyPtr maxLength:sizeof(keyPtr) encoding:NSUTF16StringEncoding];

    size_t numBytesEncrypted = 0;

    NSUInteger dataLength = [self length];

    size_t bufferSize = dataLength + kCCBlockSizeAES128;
    void *buffer_decrypt = malloc(bufferSize);    
    NSMutableData *output_decrypt = [[NSData alloc] init];
    CCCryptorStatus result = CCCrypt(kCCDecrypt , kCCAlgorithmAES128, kCCOptionPKCS7Padding, keyPtr, kCCKeySizeAES256, NULL, [self mutableBytes], [self length], buffer_decrypt, bufferSize, &numBytesEncrypted);

    output_decrypt = [NSMutableData dataWithBytesNoCopy:buffer_decrypt length:numBytesEncrypted];

    if(result == kCCSuccess) {
        return output_decrypt;
    } 
        return NULL;
    }
}

这是我制作的代码,我想与上面的代码对应:

This is the code I made that I would like to correspond with the above code:

- (void)Encrypt {
    //Convert NSString to NSData so that it can be used to encrypt the Input
    NSString *Input  = [Inputbox text];
    NSData *InputData = [Input dataUsingEncoding:NSUTF8StringEncoding];
    //What to do here
}

我如何使用这段代码,这些方法?它在我的实现文件中的什么位置?

How do I use this code, these methods? Where does it go in my Implementation file?

推荐答案

靠近顶部的这一行表示您正在向 NSMutableData 添加 AES 功能:

This line near the top says you're adding AES functionality to NSMutableData:

@implementation NSMutableData(AES)

在 Objective-C 中,这称为类别;类别让您扩展现有的类.

In Objective-C, this is called a category; categories let you extend an existing class.

此代码通常位于名为 NSMutableData-AES.m 的文件中.也创建一个头文件 NSMutableData-AES.h.它应该包含:

This code would typically go in a file named NSMutableData-AES.m. Create a header file too, NSMutableData-AES.h. It should contain:

@interface NSMutableData(AES)
- (NSMutableData*) EncryptAES: (NSString *) key;
@end

在您的主文件中包含(#import)该标题.在代码中添加对加密函数的调用:

Include (#import) that header in your main file. Add a call to the encryption function in your code:

NSData *InputData = [Input dataUsingEncoding:NSUTF8StringEncoding];
NSData *encryptedData = [InputData EncryptAES:@"myencryptionkey"];

同样用于解密.

这篇关于Objective-C 中的 AES 字符串加密的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆