iOS 5:数据加密 AES-256 EncryptWithKey:未找到

iOS 5: Data encryption AES-256 EncryptWithKey: not found(iOS 5:数据加密 AES-256 EncryptWithKey:未找到)

本文介绍了iOS 5:数据加密 AES-256 EncryptWithKey:未找到的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问题是关于iOS5 应用程序.我有一个视图控制器,其中有一些 UITextFields.我想使用 AES-256 加密数据.

Question is about iOS5 application. I have a view controller where I have some UITextFields. I would like to encrypt data using AES-256.

事实上,我不知道我必须添加哪些必备包才能进行加密和解密.我已经浏览过其他帖子,但太多的解释搞砸了.

In fact, I don't know what are the prerequisite packages that I have to add to do encryption and decryption. I have gone trough other posts but too much explanation messed it up.

请告诉我使用 AES-256 加密数据时必须包含的所有包和头文件

Kindly let me know what and all packages, header files I have to include to encrypt data using AES-256

钱德拉

推荐答案

参考以下类别.

常见问题解答:什么是类别?

FAQ: What is a category?

简而言之,Cocoa API 添加方法.简要扩展类.

In short, Cocoa API to add the method. briefly expand class.

更多信息,

自定义现有类

类别

File-New-Cocoa Touch - Objective-C 类

File-New-Cocoa Touch - Objective-C category

如果你想使用一个类别,你的类添加一个#importNSData+Encryption.h"

If you want to use a category, your class add a #import"NSData+Encryption.h"

//NSData+加密.h

//NSData+Encryption.h

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


//NSData+Encryption.m


//NSData+Encryption.m

#import "NSData+Encryption.h"
#import <CommonCrypto/CommonCryptor.h>

@implementation NSData (Encryption)
- (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

这篇关于iOS 5:数据加密 AES-256 EncryptWithKey:未找到的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:iOS 5:数据加密 AES-256 EncryptWithKey:未找到

基础教程推荐