ios加密


#import "ViewController.h"

#import <CommonCrypto/CommonCryptor.h>

#import <CommonCrypto/CommonDigest.h>


@interface ViewController ()


@end


@implementation ViewController


- (void)viewDidLoad {

    [super viewDidLoad];

    //[self aesTest];

   // [self md5AndSha1Test];

    Byte by[8] = {33,2,3,4,5,6,7,8};

    NSData *data = [NSData dataWithBytes:by length:8];

    NSString *str = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];

    NSLog(@"%@",str);

}


#pragma mark - md5 和 sha1 散列加密 不可逆


-(void)md5AndSha1Test {

    NSString *str = @"djgj的结局给大家gjggjgjfgjfjfjfjfjfjfjfjjfjfj";

    NSLog(@"%ld  %@",[[self sha1_base64:str] length],[self sha1:str]);

    NSLog(@"%ld  %@",[[self md5_base64:str] length],[self md5:str]);

}


//40位

- (NSString*) sha1:(NSString *)str

{

    const char *cstr = [str cStringUsingEncoding:NSUTF8StringEncoding];

    NSData *data = [NSData dataWithBytes:cstr length:str.length];

    //使用对应的CC_SHA1,CC_SHA256,CC_SHA384,CC_SHA512的长度分别是20,32,48,64

    uint8_t digest[CC_SHA1_DIGEST_LENGTH];

    //使用对应的CC_SHA256,CC_SHA384,CC_SHA512

    CC_SHA1(data.bytes,(CC_LONG)data.length, digest);

    NSMutableString* output = [NSMutableString stringWithCapacity:CC_SHA1_DIGEST_LENGTH * 2];

    NSLog(@"%s",digest);

    for(int i = 0; i < CC_SHA1_DIGEST_LENGTH; i++)

        [output appendFormat:@"%02x", digest[i]];

    return output;

}


-(NSString *) md5:(NSString *)str

{

    const char *cStr = [str UTF8String];

    unsigned char digest[CC_MD5_DIGEST_LENGTH];

    CC_MD5( cStr, (CC_LONG)strlen(cStr), digest );

    NSMutableString *output = [NSMutableString stringWithCapacity:CC_MD5_DIGEST_LENGTH * 2];

    for(int i = 0; i < CC_MD5_DIGEST_LENGTH; i++)

        [output appendFormat:@"%02x", digest[i]];

    return output;

}


- (NSString *) sha1_base64:(NSString *)str

{

    const char *cstr = [str cStringUsingEncoding:NSUTF8StringEncoding];

    NSData *data = [NSData dataWithBytes:cstr length:str.length];

    uint8_t digest[CC_SHA1_DIGEST_LENGTH];

    CC_SHA1(data.bytes,(CC_LONG)data.length, digest);

    NSData * base64 = [[NSData alloc]initWithBytes:digest length:CC_SHA1_DIGEST_LENGTH];

    NSString *  base64Str =  [base64 base64EncodedStringWithOptions:0];;

    return base64Str;

}


- (NSString *) md5_base64:(NSString *)str

{

    const char *cStr = [str UTF8String];

    unsigned char digest[CC_MD5_DIGEST_LENGTH];

    CC_MD5( cStr, (CC_LONG)strlen(cStr), digest );

    NSData * base64 = [[NSData alloc]initWithBytes:digest length:CC_MD5_DIGEST_LENGTH];

    NSString *  base64Str =  [base64 base64EncodedStringWithOptions:0];

    return base64Str;

}



#pragma mark -aes加密  对称加密


-(void)aesTest {

    NSString *str = @"djgjgjgjggjgjfgjfjfjfjfjfjfjfjjfjfjjfjffjjjfjfjfjfjfjfjfjfjfjff";

    NSData *data = [str dataUsingEncoding:NSUTF8StringEncoding];

    NSData *enData =[self AES256ParmEncryptWithKey:@"123" Encrypttext:data];

    NSData *deData = [self AES256ParmDecryptWithKey:@"123" Decrypttext:enData];

    NSLog(@"%@",[[NSString alloc] initWithData:deData encoding:NSUTF8StringEncoding]);

}


-(NSData *)AES256ParmEncryptWithKey:(NSString *)key Encrypttext:(NSData *)text  //加密

{

    char keyPtr[kCCKeySizeAES256+1];

    bzero(keyPtr, sizeof(keyPtr));

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

    NSUInteger dataLength = [text length];

    size_t bufferSize = dataLength + kCCBlockSizeAES128;

    void *buffer = malloc(bufferSize);

    size_t numBytesEncrypted = 0;

    CCCryptorStatus cryptStatus = CCCrypt(kCCEncrypt, kCCAlgorithmAES128,

                                          kCCOptionPKCS7Padding | kCCOptionECBMode,

                                          keyPtr, kCCBlockSizeAES128,

                                          NULL,

                                          [text bytes], dataLength,

                                          buffer, bufferSize,

                                          &numBytesEncrypted);

    if (cryptStatus == kCCSuccess) {

        return [NSData dataWithBytesNoCopy:buffer length:numBytesEncrypted];

    }

    free(buffer);

    return nil;

}


- (NSData *)AES256ParmDecryptWithKey:(NSString *)key Decrypttext:(NSData *)text  //解密

{

    char keyPtr[kCCKeySizeAES256+1];

    bzero(keyPtr, sizeof(keyPtr));

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

    NSUInteger dataLength = [text length];

    size_t bufferSize = dataLength + kCCBlockSizeAES128;

    void *buffer = malloc(bufferSize);

    size_t numBytesDecrypted = 0;

    CCCryptorStatus cryptStatus = CCCrypt(kCCDecrypt, kCCAlgorithmAES128,

                                          kCCOptionPKCS7Padding | kCCOptionECBMode,

                                          keyPtr, kCCBlockSizeAES128,

                                          NULL,

                                          [text bytes], dataLength,

                                          buffer, bufferSize,

                                          &numBytesDecrypted);

    if (cryptStatus == kCCSuccess) {

        return [NSData dataWithBytesNoCopy:buffer length:numBytesDecrypted];

    }

    free(buffer);

    return nil;

}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值