美文网首页
iOS常用加密方法(aes、base64、md5)

iOS常用加密方法(aes、base64、md5)

作者: Mortimey_yt | 来源:发表于2016-08-17 16:36 被阅读104次

    一. aes加密* or *解密

        //  NSData+AES.h
       //  MM_FESCO
       //  Created by Mortimey on 16/8/17.
       //  Copyright © 2016年 Mortimey. All rights reserved.
       //
        #import <Foundation/Foundation.h>
        @class NSString;
    
        @interface NSData (AES)
    
       - (NSData *)AES365EncryptWithKey:(NSString *)key;   //加密
    
       - (NSData *)AES365DecryptWithKey:(NSString *)key;   //解密
    
       @end
          //
        //  NSData+AES.m
       //  MM_FESCO
         //
       //  Created by Mortimey on 16/8/17.
        //  Copyright © 2016年 Mortimey. All rights reserved.
         //
    
        #import "NSData+AES.h"
    
       #import <CommonCrypto/CommonCryptor.h>
    
       @implementation NSData (AES)
    
    
    - (NSData *)AES365EncryptWithKey:(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 *)AES365DecryptWithKey:(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
    

    二. base64加密 * or * 解密
    AES在iOS加过密以后以NSData的形式存下来,如果想以NSString形式存储,那么对NSData进行BASE64位编码。
    首先下载GTMBase64文件,在工程中加入下面三个文件
    GTMDefines.h
    GTMBase64.h
    GTMBase64.m
    这三个文件请参考: https://github.com/Mortime/MM_EncodeAndCodeFile.git

      //
      //  NSString+BASE64.h
      //  MM_FESCO
      //
      //  Created by Mortimey on 16/8/17.
      //  Copyright © 2016年 Mortimey. All rights reserved.
      //
    
       #import <Foundation/Foundation.h>
    
       @interface NSString (BASE64)
        //  输入类型为NSString
       + (NSString*)encodeBase64String:(NSString *)input;
       + (NSString*)decodeBase64String:(NSString *)input;
    
        //  输入类型为NSData
       + (NSString*)encodeBase64Data:(NSData *)data;
       + (NSString*)decodeBase64Data:(NSData *)data;
       @end
    
        //
       //  NSString+BASE64.m
       //  MM_FESCO
       //
       //  Created by Mortimey on 16/8/17.
       //  Copyright © 2016年 Mortimey. All rights reserved.
       //
    
       #import "NSString+BASE64.h"
       #import "GTMBase64.h"
    
       @implementation NSString (BASE64)
    
    
       + (NSString*)encodeBase64String:(NSString * )input {
    NSData *data = [input dataUsingEncoding:NSUTF8StringEncoding allowLossyConversion:YES];
    data = [GTMBase64 encodeData:data];
    NSString *base64String = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
    return base64String;
     }
    
    + (NSString*)decodeBase64String:(NSString * )input {
    NSData *data = [input dataUsingEncoding:NSUTF8StringEncoding allowLossyConversion:YES];
    data = [GTMBase64 decodeData:data];
    NSString *base64String = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
    return base64String;
    }
    
     + (NSString*)encodeBase64Data:(NSData *)data {
    data = [GTMBase64 encodeData:data];
    NSString *base64String = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
    return base64String;
    }
    
      + (NSString*)decodeBase64Data:(NSData *)data {
    data = [GTMBase64 decodeData:data];
    NSString *base64String = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
    return base64String;
    }
    @end
    

    三. md5加密

    //
    //  NSString+MD5.h
    //
    //  Created by Keith Smiley on 3/25/13.
     //  Copyright (c) 2013 Keith Smiley. All rights reserved.
     //
    
      @import Foundation;
    
       #ifdef NS_ASSUME_NONNULL_BEGIN
       NS_ASSUME_NONNULL_BEGIN
       #endif
    
        @interface NSString (MD5)
    
         - (NSString *)MD5Digest;
    
        @end
    
       #ifdef NS_ASSUME_NONNULL_END
      NS_ASSUME_NONNULL_END
      #endif
        //
        //  NSString+MD5.m
      //
     //  Created by Keith Smiley on 3/25/13.
     //  Copyright (c) 2013 Keith Smiley. All rights reserved.
     //
    
    #import <CommonCrypto/CommonDigest.h>
    #import "NSString+MD5.h"
    
    @implementation NSString (MD5)
    
    - (NSString *)MD5Digest
    {
    const char* input = [self UTF8String];
    unsigned char result[CC_MD5_DIGEST_LENGTH];
    CC_MD5(input, (CC_LONG)strlen(input), result);
    
    NSMutableString *digest = [NSMutableString stringWithCapacity:CC_MD5_DIGEST_LENGTH * 2];
    for (NSInteger i = 0; i < CC_MD5_DIGEST_LENGTH; i++) {
        [digest appendFormat:@"%02x", result[i]];
    }
    
    return digest;
    }
    
    @end
    

    ==========================================================================================
    aes、base64、md5 加密和解密方法也可参考本人Github: https://github.com/Mortime/MM_EncodeAndCodeFile.git

    相关文章

      网友评论

          本文标题: iOS常用加密方法(aes、base64、md5)

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