kccoptionpkcs7padding php,使用AES 128 CBC no padding/kCCOptionPKCS7Padding

//

//  AES.h

//  AES

//

//  Created by DBH on 16/7/21.

//  Copyright © 2016年com.chunqiankeji. All rights reserved.

//

#import

@interfaceAES :NSObject

+ (NSString*) AES128Encrypt:(NSString*)plainText WithPassword:(NSString*)paw;

+ (NSString*) AES128Decrypt:(NSString*)encryptText WithPassword:(NSString*)paw;

@end

//

//  AES.m

//  AES

//

//  Created by DBH on 16/7/21.

//  Copyright © 2016年com.chunqiankeji. All rights reserved.

//

#import"AES.h"

#import

#import"GTMBase64.h"

//#define gkey@"uuPNMd/vVzqbDw/zsRbU9g==" //自行修改

#define gIv             @"1234876512567348"//自行修改

@implementationAES

+(NSString *)AES128Encrypt:(NSString *)plainText WithPassword:(NSString *)paw

{

charkeyPtr[kCCKeySizeAES128+1];

memset(keyPtr,0, sizeof(keyPtr));

[paw getCString:keyPtr maxLength:sizeof(keyPtr) encoding:NSUTF8StringEncoding];

charivPtr[kCCBlockSizeAES128+1];

memset(ivPtr,0, sizeof(ivPtr));

[gIv getCString:ivPtr maxLength:sizeof(ivPtr) encoding:NSUTF8StringEncoding];

NSData* data = [plainText dataUsingEncoding:NSUTF8StringEncoding];

NSUInteger dataLength = [data length];

NSUInteger diff = kCCKeySizeAES128 - (dataLength % kCCKeySizeAES128);

NSUInteger newSize =0;

if(diff >0)

{

newSize = dataLength + diff;

}

chardataPtr[newSize];

memcpy(dataPtr, [databytes], [datalength]);

for(inti =0; i < diff; i++)

{

dataPtr[i + dataLength] =0x00;

}

size_tbufferSize = newSize +kCCBlockSizeAES128;

void*buffer = malloc(bufferSize);

memset(buffer, 0, bufferSize);

size_tnumBytesCrypted =0;

CCCryptorStatuscryptStatus =CCCrypt(kCCEncrypt,

kCCAlgorithmAES128,

kCCOptionPKCS7Padding,//No padding/kCCOptionPKCS7Padding

keyPtr,

kCCKeySizeAES128,

ivPtr,

dataPtr,

sizeof(dataPtr),

buffer,

bufferSize,

&numBytesCrypted);

if(cryptStatus ==kCCSuccess) {

NSData*resultData = [NSDatadataWithBytesNoCopy:bufferlength:numBytesCrypted];

return[GTMBase64stringByEncodingData:resultData];

}

free(buffer);

returnnil;

}

+(NSString*)AES128Decrypt:(NSString*)encryptText WithPassword:(NSString*)paw

{

charkeyPtr[kCCKeySizeAES128+1];

memset(keyPtr, 0,sizeof(keyPtr));

[pawgetCString:keyPtrmaxLength:sizeof(keyPtr)encoding:NSUTF8StringEncoding];

charivPtr[kCCBlockSizeAES128+1];

memset(ivPtr, 0,sizeof(ivPtr));

[gIv getCString:ivPtr maxLength:sizeof(ivPtr) encoding:NSUTF8StringEncoding];

NSData *data = [GTMBase64 decodeData:[encryptText dataUsingEncoding:NSUTF8StringEncoding]];

NSUInteger dataLength = [data length];

size_t bufferSize = dataLength + kCCBlockSizeAES128;

void*buffer = malloc(bufferSize);

size_t numBytesCrypted =0;

CCCryptorStatus cryptStatus = CCCrypt(kCCDecrypt,

kCCAlgorithmAES128,

kCCOptionPKCS7Padding,//

keyPtr,

kCCBlockSizeAES128,

ivPtr,

[data bytes],

dataLength,

buffer,

bufferSize,

&numBytesCrypted);

if(cryptStatus == kCCSuccess) {

NSData *resultData = [NSData dataWithBytesNoCopy:buffer length:numBytesCrypted];

return[[NSString alloc] initWithData:resultData encoding:NSUTF8StringEncoding];

}

free(buffer);

returnnil;

}

@end

在下载GTMBase64导入 就好  在我的资源里面有

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值