博客> ios加密解密
ios加密解密
1小时前 评论:0 阅读:648 易_君
ios oc 加密 解密

第一种方法: // // AESCipher.h // // Created by 易元江 on 17-07-05. // Copyright (c) 2017年 com.jianzhen.zj. All rights reserved. //

import

NSString aesEncryptString(NSString content, NSString key); NSString aesDecryptString(NSString content, NSString key);

NSData aesEncryptData(NSData data, NSData key); NSData aesDecryptData(NSData data, NSData key);

// // AESCipher.h // // Created by 易元江 on 17-07-05. // Copyright (c) 2017年 com.jianzhen.zj. All rights reserved. //

import "AESCipher.h"

import

NSString const *kInitVector = @"yyj123456789012";

size_t const kKeySize = kCCKeySizeAES128;

NSData cipherOperation(NSData contentData, NSData *keyData, CCOperation operation) {

NSUInteger dataLength = contentData.length;

void const *initVectorBytes = [kInitVector dataUsingEncoding:NSUTF8StringEncoding].bytes;
void const *contentBytes = contentData.bytes;
void const *keyBytes = keyData.bytes;

size_t operationSize = dataLength + kCCBlockSizeAES128;
void *operationBytes = malloc(operationSize);
size_t actualOutSize = 0;

CCCryptorStatus cryptStatus = CCCrypt(operation,
                                      kCCAlgorithmAES128,
                                      kCCOptionPKCS7Padding,
                                      keyBytes,
                                      kKeySize,
                                      initVectorBytes,
                                      contentBytes,
                                      dataLength,
                                      operationBytes,
                                      operationSize,
                                      &actualOutSize);

if (cryptStatus == kCCSuccess) {
    return [NSData dataWithBytesNoCopy:operationBytes length:actualOutSize];
}
free(operationBytes);
return nil;

}

NSString aesEncryptString(NSString content, NSString key) { NSData contentData = [content dataUsingEncoding:NSUTF8StringEncoding]; NSData keyData = [key dataUsingEncoding:NSUTF8StringEncoding]; NSData encrptedData = aesEncryptData(contentData, keyData); return [encrptedData base64EncodedStringWithOptions:NSDataBase64EncodingEndLineWithLineFeed]; }

NSString aesDecryptString(NSString content, NSString key) { NSData contentData = [[NSData alloc] initWithBase64EncodedString:content options:NSDataBase64DecodingIgnoreUnknownCharacters]; NSData keyData = [key dataUsingEncoding:NSUTF8StringEncoding]; NSData decryptedData = aesDecryptData(contentData, keyData); return [[NSString alloc] initWith[removed] sizeof(keyPtr));

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

char ivPtr[kCCBlockSizeAES128 + 1];

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

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

NSUInteger dataLength = [contentData length];

size_t bufferSize = dataLength + kCCBlockSizeAES128;

void *buffer = malloc(bufferSize);

size_t numBytesCrypted = 0;

CCCryptorStatus cryptStatus = CCCrypt(operation,

                                      kCCAlgorithmAES128,

                                      kCCOptionPKCS7Padding,

                                      keyPtr,

                                      kCCBlockSizeAES128,

                                      ivPtr,

                                      [contentData bytes],

                                      dataLength,

                                      buffer,

                                      bufferSize,

                                      &numBytesCrypted);

if (cryptStatus == kCCSuccess) {

    return [NSData dataWithBytesNoCopy:buffer length:numBytesCrypted];

}

free(buffer);

return nil;

}

@end

收藏
0
sina weixin mail 回到顶部