首页 > 其他分享 >iOS AES 256加密

iOS AES 256加密

时间:2023-05-22 19:31:59浏览次数:39  
标签:AES buffer iOS NSString dataLength NSData bufferSize 256 keyPtr

#import <Foundation/Foundation.h>

@class NSString;

@interface NSData (Encryption)

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

@end


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

@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;
}

@end
 

//test
    NSString *testString = @"test";
    NSData *aesData = [SecurityUtil encryptAESData:testString];
    NSLog(@"AES加密:%@", aesData);
    NSLog(@"AES解密:%@", [SecurityUtil decryptAESData:aesData]);

标签:AES,buffer,iOS,NSString,dataLength,NSData,bufferSize,256,keyPtr
From: https://blog.51cto.com/u_16124099/6326789

相关文章

  • Unzipping Files In iOS Using ZipArchive
    Inthistutorial,IamgoingtodemonstratehowyoucanzipandunzipfilesfromwithinyouriOSapplications.WewillbeusingathirdpartylibrarycalledZipArchivetoachievethis.Whilethereareacouplesolutionsouttheretozipandunzipfiles,......
  • iOS base64 编码详解
    iOSbase64编码详解iOS中将NSData转为base64编码时有NSDataBase64EncodingOptionsNSDataBase64EncodingOptions有四个选项/****************Base64Options****************/typedefNS_OPTIONS(NSUInteger,NSDataBase64EncodingOptions){//Usezerooron......
  • iOS 内购详解
    iOSApp内购SKProductsRequestDelegate不调用原因及解决办法之前代码是这样写的://此时request没有强指针指向它这样会导致SKProductsRequestDelegate不调用但不是每次都不调用,只是偶尔出现不调用的情况这个问题不好发现SKProductsRequest*request=[[SKProductsReq......
  • iOS ijkplayer 播放器 消息循环 详解
    iOSijkplayer 消息循环_mediaPlayer=ijkmp_ios_create(media_player_msg_loop);//开始消息循环intmedia_player_msg_loop(void*arg){@autoreleasepool{IjkMediaPlayer*mp=(IjkMediaPlayer*)arg;__weakIJKFFMoviePlayerController*ffpControll......
  • iOS UITabBarController 典型应用
    -(BOOL)application:(UIApplication*)applicationdidFinishLaunchingWithOptions:(NSDictionary*)launchOptions{self.window=[[UIWindowalloc]initWithFrame:[[UIScreenmainScreen]bounds]];//Overridepointforcustomizationafterapplicationlau......
  • iOS Apple Development Document 详解
    iOS官方开发文档https://developer.apple.com/library/prerelease/ios/navigation/点击打开链接再次标记一下。......
  • 基于alios(centos 7.2) 安装 docker
    参考文档:https://help.aliyun.com/document_detail/51853.html?spm=a2c4g.264695.0.0.55c91e18zfwqrz#section-gtl-cjs-ls2https://developer.aliyun.com/article/1148896https://blog.csdn.net/qq_32828933/article/details/88656414https://github.com/huataihuang/cloud-a......
  • iOS Label 作为分割线时 上下左右切边
    -(void)viewDidLoad{[superviewDidLoad];//Doanyadditionalsetupafterloadingtheview,typicallyfromanib.//CGFloatborderWidth=20.0;//UILabel*label=[[UILabelalloc]initWithFrame:CGRectMake(100,400,100,100)];......
  • iOS 免费后台系统 Bmob平台
    Bmob平台为您的移动应用提供了一个完整的后端解决方案,我们提供轻量级的SDK开发包,让开发者以最小的配置和最简单的方式使用Bmob平台提供的服务,进而完全消除开发者编写服务器代码以及维护服务器的操作。使用CocoaPods安装BmobSDK如何使用CocoaPods安装BmobSDK可查看我们提供的文档:......
  • iOS 常用UI 部分常用 方法
    1、设置NavigationBar主题UINavigationBar*navigatonBar=[UINavigationBarappearance];//获取到navigationBar[navigatonBarsetBackgroundImage:<#(nullableUIImage*)#>forBarMetrics:<#(UIBarMetrics)#>]; 设置标题颜色NSMutableDictionary*attributes=[NSMut......