数据加密

数据加密——MD5

  • 不可逆

  • 同样的数据加密结果是⼀一样的?密码上的⼀一个忌讳

  •  加点私钥


加密代码:

//    .H
//  NSString+Password.h
//  数据加密
//

#import <Foundation/Foundation.h>

@interface NSString (Password)

/**
    “加点盐”的过程
*/
-- (NSString *)myMD5;

/**
 *  32位MD5加密
 *
 *  @return 32位MD5加密结果
 */
- (NSString *)MD5;

/**
 *  SHA1加密
 *
 *  @return SHA1加密结果
 */
- (NSString *)SHA1;

@end
//    .M
//  NSString+Password.m
//  数据加密
//


#import "NSString+Password.h"
#import <CommonCrypto/CommonDigest.h>

/**私钥--令牌的意思(token)*/
static NSString *token = @"fdhsahgahgoh435245dsaiubviusauiash34$%#@$@fdhsaohv^%*%^dsaghf";

@implementation NSString (Password)

/**
    “加点盐”的过程
*/
-- (NSString *)myMD5{
    NSString *str = [NSString stringWithForamt:@"%@%@", self, token];
    return [str MD5];
}

#pragma mark 使用MD5加密字符串
- (NSString *)MD5
{
    const char *cStr = [self UTF8String];
    unsigned char digest[CC_MD5_DIGEST_LENGTH];

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

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

    for(int i = 0; i < CC_MD5_DIGEST_LENGTH; i++) {
        [result appendFormat:@"%02x", digest[i]];
    }

    return result;
}

#pragma mark 使用SHA1加密字符串
- (NSString *)SHA1
{
    const char *cStr = [self UTF8String];
    NSData *data = [NSData dataWithBytes:cStr length:self.length];
    uint8_t digest[CC_SHA1_DIGEST_LENGTH];

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

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

    for(int i = 0; i < CC_SHA1_DIGEST_LENGTH; i++) {
        [result appendFormat:@"%02x", digest[i]];
    }
    
    return result;
}

@end


具体使用:

#import "ViewController.h"
#import "NSString+Password.h"

@interface ViewController () <NSURLConnectionDataDelegate>

@property (weak, nonatomic) IBOutlet UITextField *userName;
@property (weak, nonatomic) IBOutlet UITextField *userPwd;

@property (nonatomic, strong) NSMutableData *data;

@property (nonatomic, strong) NSString *loginPwd;

@end

@implementation ViewController

- (NSString *)loginPwd
{
 /*******************************/
    return [self.userPwd.text MD5];   //这里调用
  //return [self.userPwd.text myMD5];  //私钥进一步加密,“加点盐”后的MD5加密
 /*******************************/
}

- (void)viewDidLoad
{
    [super viewDidLoad];
}

- (IBAction)logon
{
    NSURL *url = [NSURL URLWithString:@"http://localhost/login.php"];
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url 
                                        cachePolicy:NSURLRequestUseProtocolCachePolicy 
                                        timeoutInterval:10.0f];
    
    request.HTTPMethod = @"POST";
    NSString *body = [NSString stringWithFormat:@"username=%@&password=%@", 
                        self.userName.text, self.loginPwd];
    NSLog(@"%@", body);
    
    request.HTTPBody = [body dataUsingEncoding:NSUTF8StringEncoding];
    
    NSURLConnection *connection = [[NSURLConnection alloc] 
                                        initWithRequest:request delegate:self];
    
    [connection start];
}

#pragma mark - 代理方法
#pragma mark 接收到服务器的响应
- (void)connection:(NSURLConnection *)connection 
                    didReceiveResponse:(NSURLResponse *)response
{
    // 准备工作
    if (!self.data) {
        self.data = [NSMutableData data];
    } else {
        self.data = nil;
    }
}

#pragma mark 收到数据(有可能是一部分)
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
    [self.data appendData:data];
}

- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
    // 数据后续处理
    NSString *result = [[NSString alloc] initWithData:self.data 
                            encoding:NSUTF8StringEncoding];
    NSLog(@"%@", result);
}

- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
{
    NSLog(@"%@", error.localizedDescription);
}

@end







































转载于:https://my.oschina.net/227/blog/389833

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值