美文网首页
iOS 数据加密

iOS 数据加密

作者: komo21 | 来源:发表于2016-11-25 16:43 被阅读0次

    iOS 常用加密方式算法包括MD5加密、AES加密、BASE64加密

    • MD5加密

    MD5:全称是Message Digest Algorithm 5,译为“消息摘要算法第5版”
    效果:对输入信息生成唯一的128位散列值(32个字符)\不可逆的
    MD5解密网站

    1. MD5加密
    MD5 iOS代码加密
    创建MD5类,代码如下
    #import <Foundation/Foundation.h>
    @interface CJMD5 : NSObject
    +(NSString *)md5HexDigest:(NSString *)input;
    @end
    2
    #import "CJMD5.h"
    #import <CommonCrypto/CommonDigest.h>
    @implementation CJMD5
    +(NSString *)md5HexDigest:(NSString *)input{
        
        const char* str = [input UTF8String];
        unsigned char result[CC_MD5_DIGEST_LENGTH];
        CC_MD5(str, strlen(str), result);
        NSMutableString *ret = [NSMutableString stringWithCapacity:CC_MD5_DIGEST_LENGTH];
        
        for(int i = 0; i<CC_MD5_DIGEST_LENGTH; i++) {
            [ret appendFormat:@"%02X",result];
        }
        return ret;
    }
    @end
    
    1. 加盐(在指定位置插入字符)
    -(NSString *)md5AddSalt:(NSString *)str withSalt:(NSString *)salt{
             str = [str stringByAppendingString:salt];
             return [NSString md5HexDigest:str];
    }
    
    1. 多次MD5
    -(NSString *)moreMD5:(NSString *)str{
        NSString * md5Str = [NSString md5HexDigest:str];//一次处理
        NSString * md5Str2 = [NSString md5HexDigest:md5Str];//两次处理
        return md5Str2
    }
    
    1. 先加密, 后乱序
    -(NSString *)digest4:(NSString *)str60 {61 NSString *anwen = [str md5String];
    // 注册: 123 ---- 2CB962AC59075B964B07152D234B702064 
    // 登录: 123 --- 202CB962AC59075B964B07152D234B7066 67 NSString *header = [anwen substringToIndex:2];
    NSString *footer = [anwen substringFromIndex:2];
    anwen = [footer stringByAppendingString:header];
    NSLog(@"%@ - %@", str, anwen);
    return anwen;73 }
    
    • AES加密
      博客原文
    • BASE64加密
      为NSString新建一个分类
    #import <Foundation/Foundation.h>  
    @interface NSString (Base64)    
    //编码  
    -(NSString *)Base64Encoded;  
    +(NSString *)Base64StringWithStr:(NSString *)aSourceStr;  
     //解码  
    -(NSString *)base64Decoded;  
    +(NSString *)StringFromBase64Str:(NSString *)aSourceStr;  
    @end  
    

    .m文件:

    #import "NSString+Base64.h"  
    @implementation NSString (Base64)  
    -(NSString *)Base64Encoded  
    {  
        return [NSString Base64StringWithStr:self];  
    }  
    +(NSString *)Base64StringWithStr:(NSString *)aSourceStr  
    {  
        NSData * data = [aSourceStr dataUsingEncoding:NSUTF8StringEncoding];  
        return [data base64EncodedStringWithOptions:NSDataBase64Encoding64CharacterLineLength];  
    }  
    -(NSString *)base64Decoded  
    {  
        return [NSString StringFromBase64Str:self];  
    }  
    +(NSString *)StringFromBase64Str:(NSString *)aSourceStr  
    {  
        NSData *nsdataFromBase64String = [[NSData alloc]  
                                          initWithBase64EncodedString:aSourceStr options:NSDataBase64DecodingIgnoreUnknownCharacters];  
        NSString *base64Decoded = [[NSString alloc]  
                                   initWithData:nsdataFromBase64String encoding:NSUTF8StringEncoding];  
        return base64Decoded;  
    }  
    @end  
    

    相关文章

      网友评论

          本文标题:iOS 数据加密

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