Objective-C中的AES字符串加密 [英] AES string encryption in Objective-C
问题描述
我的Objective-C应用程式需要文字/字串加密(特别是 nsstring )。
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
}
,这些方法?
推荐答案
靠近顶部的这一行表示您正在向NSMutableData添加AES功能:/ p>
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屋!