iOS 四种加密方法

来源:互联网 发布:观星的app软件 编辑:程序博客网 时间:2024/04/30 10:53

1>MD5加密方法

需要引用头:CommonCrypto/CommonDigest.h

我将方法绑定在了按键点击的事件中:

-(IBAction)md5Clicked:(id)sender{    const char * cStrValue = [self.sourceCode.text UTF8String];    unsigned char theResult[CC_MD5_DIGEST_LENGTH];    CC_MD5(cStrValue, strlen(cStrValue), theResult);    self.resultCode.text = [NSString stringWithFormat:@"%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X",theResult[0], theResult[1], theResult[2], theResult[3],theResult[4], theResult[5], theResult[6], theResult[7],theResult[8], theResult[9], theResult[10], theResult[11],theResult[12], theResult[13], theResult[14], theResult[15]];}
直接获取字符串转换后赋给结果字符串

MD5无法解码,常用于文件校验


2>BASE64加密方法

需要引用头:CommonCrypto/CommonCryptor.h

同样绑定在了按键上

-(IBAction)base64Clicked:(id)sender{    NSData *data = [self.sourceCode.text dataUsingEncoding:NSUTF8StringEncoding];    self.resultCode.text = [data base64EncodedStringWithOptions:NSDataBase64EncodingEndLineWithLineFeed];        NSData *deData = [[NSData alloc]initWithBase64EncodedString:self.resultCode.text options:0];    self.resultDeCode.text = [[NSString alloc]initWithData:deData encoding:NSASCIIStringEncoding];}
前两行为加密方法,后两行解密

该方法使用的是iOS自带的编码解码库,还可用GTMBase64编码解码



其他两种以及解码待更新....

0 0