首页 > 代码库 > IOS AES加密之ECB128模式

IOS AES加密之ECB128模式

 

1、AES加密模式有好几种,网上大多是CBC、256模式,找了好久才找到解决ECB128模式加密。

AES需要导入头文件

#import <CommonCrypto/CommonCryptor.h>

#import "GTMBase64.h" //加密后的数据在Base64转字符串类型

+(NSString *)AES128Encrypt:(NSString *)plainText key:(NSString *)key{    char keyPtr[kCCKeySizeAES128+1];    memset(keyPtr, 0, sizeof(keyPtr));    [key getCString:keyPtr maxLength:sizeof(keyPtr) encoding:NSUTF8StringEncoding];        NSData* data =http://www.mamicode.com/ [plainText dataUsingEncoding:NSUTF8StringEncoding];    NSUInteger dataLength = [data length];        size_t bufferSize = dataLength + kCCBlockSizeAES128;    void *buffer = malloc(bufferSize);    size_t numBytesEncrypted = 0;    CCCryptorStatus cryptStatus = CCCrypt(kCCEncrypt,                                          kCCAlgorithmAES128,                                          kCCOptionPKCS7Padding|kCCOptionECBMode,                                          keyPtr,                                          kCCBlockSizeAES128,                                          NULL,                                          [data bytes],                                          dataLength,                                          buffer,                                          bufferSize,                                          &numBytesEncrypted);    if (cryptStatus == kCCSuccess) {        NSData *resultData =http://www.mamicode.com/ [NSData dataWithBytesNoCopy:buffer length:numBytesEncrypted];        return [GTMBase64 stringByEncodingData:resultData];    }    free(buffer);    return nil;}

 

直接放 Demo: AES/ECB模式128Demo下载 密码:qj57

 

IOS AES加密之ECB128模式