美文网首页
iOS常用加密算法

iOS常用加密算法

作者: 飞鱼ll | 来源:发表于2017-10-20 11:12 被阅读12次

    AES(对称加密)

    代码:

    #import <Foundation/Foundation.h>
    
    @interface NSString (AES)
    
    - (NSString *)aes256_encrypt:(NSString *)key;
    
    - (NSString *)aes256_decrypt:(NSString *)key;
    
    - (NSString *)aes128_encrypt:(NSString *)key;
    
    - (NSString *)aes128_decrypt:(NSString *)key;
    
    @end
    
    
    
    #import "NSString+AES.h"
    #import <CommonCrypto/CommonCryptor.h>
    
    @implementation NSString (AES)
    
    - (NSString *)aes256_encrypt:(NSString *)key {
        const char *cstr = [self cStringUsingEncoding:NSUTF8StringEncoding];
        NSData *data = [NSData dataWithBytes:cstr length:self.length];
        
        //对数据进行加密
        char keyPtr[kCCKeySizeAES256+1];
        bzero(keyPtr, sizeof(keyPtr));
        [key getCString:keyPtr maxLength:sizeof(keyPtr) encoding:NSUTF8StringEncoding];
        NSUInteger dataLength = [data length];
        size_t bufferSize = dataLength + kCCBlockSizeAES128;
        void *buffer = malloc(bufferSize);
        size_t numBytesEncrypted = 0;
        CCCryptorStatus cryptStatus = CCCrypt(kCCEncrypt, kCCAlgorithmAES,
                                              kCCOptionPKCS7Padding | kCCOptionECBMode,
                                              keyPtr, kCCKeySizeAES256,
                                              NULL,
                                              [data bytes], dataLength,
                                              buffer, bufferSize,
                                              &numBytesEncrypted);
        if (cryptStatus == kCCSuccess)
        {
            NSData *result = [NSData dataWithBytesNoCopy:buffer length:numBytesEncrypted];
            //base64
            return [result base64EncodedStringWithOptions:NSDataBase64Encoding64CharacterLineLength];
        }else
        {
            return nil;
        }
        
    }
    
    
    - (NSString *)aes256_decrypt:(NSString *)key {
        NSData *data = [[NSData alloc] initWithBase64EncodedData:[self dataUsingEncoding:NSASCIIStringEncoding] options:NSDataBase64DecodingIgnoreUnknownCharacters];
        
        //对数据进行解密
        char keyPtr[kCCKeySizeAES256+1];
        bzero(keyPtr, sizeof(keyPtr));
        [key getCString:keyPtr maxLength:sizeof(keyPtr) encoding:NSUTF8StringEncoding];
        NSUInteger dataLength = [data length];
        size_t bufferSize = dataLength + kCCBlockSizeAES128;
        void *buffer = malloc(bufferSize);
        size_t numBytesDecrypted = 0;
        CCCryptorStatus cryptStatus = CCCrypt(kCCDecrypt, kCCAlgorithmAES,
                                              kCCOptionPKCS7Padding | kCCOptionECBMode,
                                              keyPtr, kCCKeySizeAES256,
                                              NULL,
                                              [data bytes], dataLength,
                                              buffer, bufferSize,
                                              &numBytesDecrypted);
        if (cryptStatus == kCCSuccess)
        {
            NSData* result = [NSData dataWithBytesNoCopy:buffer length:numBytesDecrypted];
            
            return [[NSString alloc] initWithData:result encoding:NSUTF8StringEncoding];
            
        }else
        {
            return nil;
        }
        
    }
    
    - (NSString *)aes128_encrypt:(NSString *)key {
        const char *cstr = [self cStringUsingEncoding:NSUTF8StringEncoding];
        NSData *data = [NSData dataWithBytes:cstr length:self.length];
        
        //对数据进行加密
        char keyPtr[kCCKeySizeAES128+1];
        bzero(keyPtr, sizeof(keyPtr));
        [key getCString:keyPtr maxLength:sizeof(keyPtr) encoding:NSUTF8StringEncoding];
        NSUInteger dataLength = [data length];
        size_t bufferSize = dataLength + kCCBlockSizeAES128;
        void *buffer = malloc(bufferSize);
        size_t numBytesEncrypted = 0;
        CCCryptorStatus cryptStatus = CCCrypt(kCCEncrypt, kCCAlgorithmAES,
                                              kCCOptionPKCS7Padding | kCCOptionECBMode,
                                              keyPtr, kCCKeySizeAES128,
                                              NULL,
                                              [data bytes], dataLength,
                                              buffer, bufferSize,
                                              &numBytesEncrypted);
        if (cryptStatus == kCCSuccess)
        {
            NSData *result = [NSData dataWithBytesNoCopy:buffer length:numBytesEncrypted];
            //base64
            return [result base64EncodedStringWithOptions:NSDataBase64Encoding64CharacterLineLength];
        }else
        {
            return nil;
        }
        
    }
    
    
    - (NSString *)aes128_decrypt:(NSString *)key {
        NSData *data = [[NSData alloc] initWithBase64EncodedData:[self dataUsingEncoding:NSASCIIStringEncoding] options:NSDataBase64DecodingIgnoreUnknownCharacters];
        
        //对数据进行解密
        char keyPtr[kCCKeySizeAES128+1];
        bzero(keyPtr, sizeof(keyPtr));
        [key getCString:keyPtr maxLength:sizeof(keyPtr) encoding:NSUTF8StringEncoding];
        NSUInteger dataLength = [data length];
        size_t bufferSize = dataLength + kCCBlockSizeAES128;
        void *buffer = malloc(bufferSize);
        size_t numBytesDecrypted = 0;
        CCCryptorStatus cryptStatus = CCCrypt(kCCDecrypt, kCCAlgorithmAES,
                                              kCCOptionPKCS7Padding | kCCOptionECBMode,
                                              keyPtr, kCCKeySizeAES128,
                                              NULL,
                                              [data bytes], dataLength,
                                              buffer, bufferSize,
                                              &numBytesDecrypted);
        if (cryptStatus == kCCSuccess)
        {
            NSData* result = [NSData dataWithBytesNoCopy:buffer length:numBytesDecrypted];
            
            return [[NSString alloc] initWithData:result encoding:NSUTF8StringEncoding];
            
        }else
        {
            return nil;
        }
        
    }
    
    
    
    @end
    
    

    使用:

    #import "NSString+AES.h"
    
        NSString *str = @"name=14513525132&pwd=145361313";
        NSLog(@"str=%@",str);
        NSString *aesStr = [str aes256_encrypt:@"key"];
        NSLog(@"aseencryptstring=%@",aesStr);
        NSString *newStr = [aesStr aes256_decrypt:@"key"];
        NSLog(@"asedecryptStr=%@",newStr);
        
        NSString *aes128Str = [str aes128_encrypt:@"key"];
        NSLog(@"ase128encryptstring=%@",aes128Str);
        NSString *new128Str = [aes128Str aes128_decrypt:@"key"];
        NSLog(@"ase128decryptStr=%@",new128Str);
    

    RSA(非对称加密)

    参考文章

    相关文章

      网友评论

          本文标题:iOS常用加密算法

          本文链接:https://www.haomeiwen.com/subject/ysgmuxtx.html