zoukankan      html  css  js  c++  java
  • iOS经常使用加密方式(MD5,AES,BASE64)与网络数据安全

    演示样例项目下载地址  https://github.com/cerastes/Encryption


    1MD5

    创建MD5类

    #import <Foundation/Foundation.h>
    
    @interface CJMD5 : NSObject
    +(NSString *)md5HexDigest:(NSString *)input;
    @end

    #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

    MD5是不可逆的仅仅有加密没有解密使用方式例如以下

    NSString *userName = @"cerastes";
    NSString *password = @"hello Word";
    //   MD5加密
    NSString *md5 = [CJMD5 md5HexDigest:password];
    NSLog(@"%@",md5);

    2AES加密

    AES加密开源码git下载地址

    https://github.com/Gurpartap/AESCrypt-ObjC

    用法

    //  AES加密
        NSString *encryptedData = [AESCrypt encrypt:userName password:password];//加密
        NSString *message = [AESCrypt decrypt:encryptedData password:password]; //解密
        NSLog(@"加密结果 = %@",encryptedData);
        NSLog(@"解密结果 = %@",message);

    3 BASE64加密

    下载地址 https://code.google.com/p/google-toolbox-for-mac/source/browse/trunk/Foundation/?r=87

     加入例如以下方法

    .h

    + (NSString*)encodeBase64String:(NSString *)input;
    + (NSString*)decodeBase64String:(NSString *)input;
    + (NSString*)encodeBase64Data:(NSData *)data;
    + (NSString*)decodeBase64Data:(NSData *)data;

    .m

    + (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;
    }
    

    用法

    //    BASE64加密
        NSString *baseEncodeString = [GTMBase64 encodeBase64String:password];
        NSString *baseDecodeString = [GTMBase64 decodeBase64String:baseEncodeString];
        NSLog(@"baseEncodeString = %@",baseEncodeString);
        NSLog(@"baseDecodeString = %@",baseDecodeString);
    


  • 相关阅读:
    项目太多工作环境互相干扰?virtualenv 一招教你轻松解决。
    安装的 Python 版本太多互相干扰?pyenv 建议了解一下。
    Python 拓展之详解深拷贝和浅拷贝
    Python 操作 SQLite 数据库
    IQueryable接口与IEnumberable接口的区别
    Resharper的配置(习惯使用了VS的F6编译和F12(快速非resharper查询编译代码)转到定义的默认设置)【设置了好多次resharper的使用了,特此记下简单的思路】
    程序人生,人生程序。(面向对象的奇葩理解)
    SQL表连接查询(inner join、full join、left join、right join)
    MYSQL中存储过程的创建,调用及语法
    mysql存储过程详解
  • 原文地址:https://www.cnblogs.com/mfrbuaa/p/3890328.html
Copyright © 2011-2022 走看看