当前位置:编程学习 > 网站相关 >>

[iOS]AES加密在iOS上面的实现

[iOS]AES加密在iOS上面的实现


Encryption.h文件


[plain]
// 
//  Encryption.h 
//  DownloadFile 
// 
//  Created by zhoumin on 12-1-16. 
//  Copyright (c) 2012年 __MyCompanyName__. All rights reserved. 
// 
 
#import <Foundation/Foundation.h> 
 
@class NSString; 
 
@inte易做图ce NSData (Encryption) 
 
- (NSData *)AES256EncryptWithKey:(NSString *)key;   //加密 
- (NSData *)AES256DecryptWithKey:(NSString *)key;   //解密 
- (NSString *)newStringInBase64FromData;            //追加64编码 
+ (NSString*)base64encode:(NSString*)str;           //同上64编码 
 
@end 

//
//  Encryption.h
//  DownloadFile
//
//  Created by zhoumin on 12-1-16.
//  Copyright (c) 2012年 __MyCompanyName__. All rights reserved.
//

#import <Foundation/Foundation.h>

@class NSString;

@inte易做图ce NSData (Encryption)

- (NSData *)AES256EncryptWithKey:(NSString *)key;   //加密
- (NSData *)AES256DecryptWithKey:(NSString *)key;   //解密
- (NSString *)newStringInBase64FromData;            //追加64编码
+ (NSString*)base64encode:(NSString*)str;           //同上64编码

@end

Encryption.m文件

 

[plain]
// 
//  Encryption.m 
//  DownloadFile 
// 
//  Created by  on 12-1-16. 
//  Copyright (c) 2012年 __MyCompanyName__. All rights reserved. 
// 
 
#import "Encryption.h" 
#import <CommonCrypto/CommonCryptor.h> 
 
 
static char base64[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; 
 
@implementation NSData (Encryption) 
 
- (NSData *)AES256EncryptWithKey:(NSString *)key   //加密 

    char keyPtr[kCCKeySizeAES256+1]; 
    bzero(keyPtr, sizeof(keyPtr)); 
    [key getCString:keyPtr maxLength:sizeof(keyPtr) encoding:NSUTF8StringEncoding]; 
    NSUInteger dataLength = [self length]; 
    size_t bufferSize = dataLength + kCCBlockSizeAES128; 
    void *buffer = malloc(bufferSize); 
    size_t numBytesEncrypted = 0; 
    CCCryptorStatus cryptStatus = CCCrypt(kCCEncrypt, kCCAlgorithmAES128, 
                                          kCCOptionPKCS7Padding | kCCOptionECBMode, 
                                          keyPtr, kCCBlockSizeAES128, 
                                          NULL, 
                                          [self bytes], dataLength, 
                                          buffer, bufferSize, 
                                          &numBytesEncrypted); 
    if (cryptStatus == kCCSuccess) { 
        return [NSData dataWithBytesNoCopy:buffer length:numBytesEncrypted]; 
    } 
    free(buffer); 
    return nil; 

 
 
- (NSData *)AES256DecryptWithKey:(NSString *)key   //解密 

    char keyPtr[kCCKeySizeAES256+1]; 
    bzero(keyPtr, sizeof(keyPtr)); 
    [key getCString:keyPtr maxLength:sizeof(keyPtr) encoding:NSUTF8StringEncoding]; 
    NSUInteger dataLength = [self length]; 
    size_t bufferSize = dataLength + kCCBlockSizeAES128; 
    void *buffer = malloc(bufferSize); 
    size_t numBytesDecrypted = 0; 
    CCCryptorStatus cryptStatus = CCCrypt(kCCDecrypt, kCCAlgorithmAES128, 
                                          kCCOptionPKCS7Padding | kCCOptionECBMode, 
                                          keyPtr, kCCBlockSizeAES128, 
                                          NULL, 
                                          [self bytes], dataLength, 
                                          buffer, bufferSize, 
                                          &numBytesDecrypted); 
    if (cryptStatus == kCCSuccess) { 
        return [NSData dataWithBytesNoCopy:buffer length:numBytesDecrypted]; 
    } 
    free(buffer); 
    return nil; 

 
 
- (NSString *)newStringInBase64FromData            //追加64编码 

  

补充:综合编程 , 安全编程 ,
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,