AES加密在iOS上面的实现
来源:网络 责任编辑:栏目编辑 发表时间:2013-07-01 13:33 点击:次
Encryption.h文件
#import <Foundation/Foundation.h>
@class NSString;
@interface NSData (Encryption)
- (NSData *)AES256EncryptWithKey:(NSString *)key; //加密
- (NSData *)AES256DecryptWithKey:(NSString *)key; //解密
- (NSString *)newStringInBase64FromData; //追加64编码
+ (NSString*)base64encode:(NSString*)str; //同上64编码
@end
------------------------------------------------------------------------------------------------
Encryption.m文件
#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 dataWi
#import <Foundation/Foundation.h>
@class NSString;
@interface NSData (Encryption)
- (NSData *)AES256EncryptWithKey:(NSString *)key; //加密
- (NSData *)AES256DecryptWithKey:(NSString *)key; //解密
- (NSString *)newStringInBase64FromData; //追加64编码
+ (NSString*)base64encode:(NSString*)str; //同上64编码
@end
------------------------------------------------------------------------------------------------
Encryption.m文件
#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 dataWi
相关新闻>>
- 发表评论
-
- 最新评论 更多>>