Aes_Decrypt

2022-06-28  本文已影响0人  七月不下雨

Aes_Decrypt

@implementation NSData (PWXAes_Decrypt)

//解密

//加密
-(NSData *)aes_encrypt:(NSString *)key{
return [self AES256operation:kCCEncrypt key:key];
}

CCCryptorStatus cryptorStatus = CCCrypt(operation,
                                        kCCAlgorithmAES128,
                                        kCCOptionPKCS7Padding,
                                        keyPtr,
                                        kCCKeySizeAES256,
                                        ivPtr,
                                        [self bytes],
                                        [self length],
                                        buffer,
                                        bufferSize,
                                        &numBytesEncrypted);

if(cryptorStatus == kCCSuccess){
    NSLog(@"Success");
    return [NSData dataWithBytesNoCopy:buffer length:numBytesEncrypted];
}else{
    NSLog(@"Error");
}

free(buffer);
return nil;

}

import "NSData+PWXAes_Decrypt.h"

@implementation NSString (PWXAes_Decrypt)
//解密

//加密
-(NSString *)aes_encrypt:(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 aes_encrypt:key];
if (result && result.length > 0) {
    
    return [[NSString alloc] initWithData:result encoding:NSUTF8StringEncoding];
}
return nil;

}

@end

@end

上一篇下一篇

猜你喜欢

热点阅读