加密
1 2 3 4 5 6 7 8 | #import <Foundation/Foundation.h> #import <CommonCrypto/CommonDigest.h> #import <CommonCrypto/CommonCryptor.h> @interface NSData (AES256) -( NSData *) aes256_encrypt:( NSString *)key; -( NSData *) aes256_decrypt:( NSString *)key; @end |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 | #import "NSData+AES256.h" @implementation NSData (AES256) - ( NSData *)aes256_encrypt:( 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 *)aes256_decrypt:( 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 |
1 2 3 4 5 6 7 8 9 10 | #import <Foundation/Foundation.h> #import <CommonCrypto/CommonDigest.h> #import <CommonCrypto/CommonCryptor.h> #import "NSData+AES256.h" @interface NSString (AES256) -( NSString *) aes256_encrypt:( NSString *)key; -( NSString *) aes256_decrypt:( NSString *)key; @end |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 | #import "NSString+AES256.h" @implementation NSString (AES256) -( NSString *) aes256_encrypt:( NSString *)key { const char *cstr = [ self cStringUsingEncoding: NSUTF8StringEncoding ]; NSData *data = [ NSData dataWithBytes:cstr length: self .length]; //对数据进行加密 NSData *result = [data aes256_encrypt:key]; //转换为2进制字符串 if (result && result.length > 0) { Byte *datas = (Byte*)[result bytes]; NSMutableString *output = [ NSMutableString stringWithCapacity:result.length * 2]; for ( int i = 0; i < result.length; i++){ [output appendFormat:@ "%02x" , datas[i]]; } return output; } return nil ; } -( NSString *) aes256_decrypt:( NSString *)key { //转换为2进制Data NSMutableData *data = [ NSMutableData dataWithCapacity: self .length / 2]; unsigned char whole_byte; char byte_chars[3] = { '\0' , '\0' , '\0' }; int i; for (i=0; i < [ self length] / 2; i++) { byte_chars[0] = [ self characterAtIndex:i*2]; byte_chars[1] = [ self characterAtIndex:i*2+1]; whole_byte = strtol(byte_chars, NULL , 16); [data appendBytes:&whole_byte length:1]; } //对数据进行解密 NSData * result = [data aes256_decrypt:key]; if (result && result.length > 0) { return [[ NSString alloc] initWithData:result encoding: NSUTF8StringEncoding ]; } return nil ; } @end |
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 深入理解 Mybatis 分库分表执行原理
· 如何打造一个高并发系统?
· .NET Core GC压缩(compact_phase)底层原理浅谈
· 现代计算机视觉入门之:什么是图片特征编码
· .NET 9 new features-C#13新的锁类型和语义
· Spring AI + Ollama 实现 deepseek-r1 的API服务和调用
· 《HelloGitHub》第 106 期
· 数据库服务器 SQL Server 版本升级公告
· 深入理解Mybatis分库分表执行原理
· 使用 Dify + LLM 构建精确任务处理应用