提示: MD5和 des 都可以加密但是对于文件的加密,但是 MD5加密属于单向加密,可以在某些在线的网站进行解密,而 des 则不同属于双向进行的一方面可以加密之后可以进行解密.(这里事先说明一下既然你想要对文件进行加密那为什么还要进行解密呢? 这不是一本正经的耍流氓吗?)
md5加密(只提供String和data 数据 进行加密):
.h 中代码:
#import#import "CommonCrypto/CommonDigest.h"
#import "CommonCrypto/CommonCrypto.h"
@interface NSString (MD5)
- (NSString *)md5ForString;
- (NSString *)md5ForData;
@end
/*****************.m 中代码******************/
@implementation NSString (MD5)
- (NSString *)md5ForString {
//1.获取C字符串(MD5加密基于C语言实现,Foundation框架字符串需要转化)
const char * fooData = [self UTF8String];//__strong const char *UTF8String,C语言无法持有字符串,必须用__strong修饰来拷贝内容
//2.创建字符串数组接收MD5值
//一个字节是8位,两个字节是16位,两个字符可以表示一个16位进制的数,MD5结果为32位,实际上由16位16进制数组成。
unsigned char resut[CC_MD5_DIGEST_LENGTH];
//3.计算MD5值(结果存储在result数组中)
CC_MD5(fooData, (CC_LONG)strlen(fooData), resut);
//4.获取摘要值
NSMutableString *hash = [[NSMutableString alloc] initWithCapacity:CC_MD5_DIGEST_LENGTH];
for (int i = 0; i < CC_MD5_DIGEST_LENGTH; i ++) {
[hash appendFormat:@"%02X",resut[i]];
}
return [hash lowercaseString];
}
- (NSString *)md5ForData {
//1.NSData对象获取
//获取文件路径
//根据文件路径获取NSData对象
NSData *data = [NSData dataWithContentsOfFile:self];
//2.创建MD5变量
CC_MD5_CTX md5;
//3.初始化MD5变量
CC_MD5_Init(&md5);
//4.准备MD5加密
CC_MD5_Update(&md5, data.bytes, (CC_LONG)data.length);
//5.结束MD5加密
unsigned char result[CC_MD5_DIGEST_LENGTH];
CC_MD5_Final(result, &md5);
//6.获取结果
NSMutableString *resultString = [[NSMutableString alloc] initWithCapacity:CC_MD5_DIGEST_LENGTH];
for (int i = 0; i < CC_MD5_DIGEST_LENGTH; i ++) {
[resultString appendFormat:@"%02X",result[i]];
}
return [resultString lowercaseString];
}
@end
之后 调用.h 文件,其中只有两个方法
/**********************************************************************************/
des传说中的一本正经的耍流氓加密和解密(同样针对的是String):
.h 文件代码
#import/******字符串转base64(包括DES加密)******/
#define __BASE64( text ) [Base64codeFunc base64StringFromText:text]
/******base64(通过DES解密)转字符串******/
#define __TEXT( base64 ) [Base64codeFunc textFromBase64String:base64]
@interface des : NSObject
/************************************************************
函数名称 : + (NSString *)base64StringFromText:(NSString *)text
函数描述 : 将文本转换为base64格式字符串
输入参数 : (NSString *)text 文本
输出参数 : N/A
返回参数 : (NSString *) base64格式字符串
备注信息 :
**********************************************************/
+ (NSString *)base64StringFromText:(NSString *)text;
/************************************************************
函数名称 : + (NSString *)textFromBase64String:(NSString *)base64
函数描述 : 将base64格式字符串转换为文本
输入参数 : (NSString *)base64 base64格式字符串
输出参数 : N/A
返回参数 : (NSString *) 文本
备注信息 :
**********************************************************/
+ (NSString *)textFromBase64String:(NSString *)base64;
@end
/**********************************************************************************/
.m 中代码
//引入IOS自带密码库#import//空字符串
#define LocalStr_None @""
static const char encodingTable[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
@implementation des
+ (NSString *)base64StringFromText:(NSString *)text
{
if (text && ![text isEqualToString:LocalStr_None]) {
//取项目的bundleIdentifier作为KEY
NSString *key = [[NSBundle mainBundle] bundleIdentifier];
NSData *data = [text dataUsingEncoding:NSUTF8StringEncoding];
//IOS 自带DES加密 Begin
data = [self DESEncrypt:data WithKey:key];
//IOS 自带DES加密 End
return [self base64EncodedStringFrom:data];
}
else {
return LocalStr_None;
}
}
+ (NSString *)textFromBase64String:(NSString *)base64
{
if (base64 && ![base64 isEqualToString:LocalStr_None]) {
//取项目的bundleIdentifier作为KEY
NSString *key = [[NSBundle mainBundle] bundleIdentifier];
NSData *data = [self dataWithBase64EncodedString:base64];
//IOS 自带DES解密 Begin
data = [self DESDecrypt:data WithKey:key];
//IOS 自带DES加密 End
return [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
}
else {
return LocalStr_None;
}
}
/************************************************************
函数名称 : + (NSData *)DESEncrypt:(NSData *)data WithKey:(NSString *)key
函数描述 : 文本数据进行DES加密
输入参数 : (NSData *)data
(NSString *)key
输出参数 : N/A
返回参数 : (NSData *)
备注信息 : 此函数不可用于过长文本
**********************************************************/
+ (NSData *)DESEncrypt:(NSData *)data WithKey:(NSString *)key
{
char keyPtr[kCCKeySizeAES256+1];
bzero(keyPtr, sizeof(keyPtr));
[key getCString:keyPtr maxLength:sizeof(keyPtr) encoding:NSUTF8StringEncoding];
NSUInteger dataLength = [data length];
size_t bufferSize = dataLength + kCCBlockSizeAES128;
void *buffer = malloc(bufferSize);
size_t numBytesEncrypted = 0;
CCCryptorStatus cryptStatus = CCCrypt(kCCEncrypt, kCCAlgorithmDES,
kCCOptionPKCS7Padding | kCCOptionECBMode,
keyPtr, kCCBlockSizeDES,
NULL,
[data bytes], dataLength,
buffer, bufferSize,
&numBytesEncrypted);
if (cryptStatus == kCCSuccess) {
return [NSData dataWithBytesNoCopy:buffer length:numBytesEncrypted];
}
free(buffer);
return nil;
}
/************************************************************
函数名称 : + (NSData *)DESEncrypt:(NSData *)data WithKey:(NSString *)key
函数描述 : 文本数据进行DES解密
输入参数 : (NSData *)data
(NSString *)key
输出参数 : N/A
返回参数 : (NSData *)
备注信息 : 此函数不可用于过长文本
**********************************************************/
+ (NSData *)DESDecrypt:(NSData *)data WithKey:(NSString *)key
{
char keyPtr[kCCKeySizeAES256+1];
bzero(keyPtr, sizeof(keyPtr));
[key getCString:keyPtr maxLength:sizeof(keyPtr) encoding:NSUTF8StringEncoding];
NSUInteger dataLength = [data length];
size_t bufferSize = dataLength + kCCBlockSizeAES128;
void *buffer = malloc(bufferSize);
size_t numBytesDecrypted = 0;
CCCryptorStatus cryptStatus = CCCrypt(kCCDecrypt, kCCAlgorithmDES,
kCCOptionPKCS7Padding | kCCOptionECBMode,
keyPtr, kCCBlockSizeDES,
NULL,
[data bytes], dataLength,
buffer, bufferSize,
&numBytesDecrypted);
if (cryptStatus == kCCSuccess) {
return [NSData dataWithBytesNoCopy:buffer length:numBytesDecrypted];
}
free(buffer);
return nil;
}
/************************************************************
函数名称 : + (NSData *)dataWithBase64EncodedString:(NSString *)string
函数描述 : base64格式字符串转换为文本数据
输入参数 : (NSString *)string
输出参数 : N/A
返回参数 : (NSData *)
备注信息 :
**********************************************************/
+ (NSData *)dataWithBase64EncodedString:(NSString *)string
{
if (string == nil)
[NSException raise:NSInvalidArgumentException format:nil];
if ([string length] == 0)
return [NSData data];
static char *decodingTable = NULL;
if (decodingTable == NULL)
{
decodingTable = malloc(256);
if (decodingTable == NULL)
return nil;
memset(decodingTable, CHAR_MAX, 256);
NSUInteger i;
for (i = 0; i < 64; i++)
decodingTable[(short)encodingTable[i]] = i;
}
const char *characters = [string cStringUsingEncoding:NSASCIIStringEncoding];
if (characters == NULL) // Not an ASCII string!
return nil;
char *bytes = malloc((([string length] + 3) / 4) * 3);
if (bytes == NULL)
return nil;
NSUInteger length = 0;
NSUInteger i = 0;
while (YES)
{
char buffer[4];
short bufferLength;
for (bufferLength = 0; bufferLength < 4; i++)
{
if (characters[i] == '\0')
break;
if (isspace(characters[i]) || characters[i] == '=')
continue;
buffer[bufferLength] = decodingTable[(short)characters[i]];
if (buffer[bufferLength++] == CHAR_MAX) // Illegal character!
{
free(bytes);
return nil;
}
}
if (bufferLength == 0)
break;
if (bufferLength == 1) // At least two characters are needed to produce one byte!
{
free(bytes);
return nil;
}
// Decode the characters in the buffer to bytes.
bytes[length++] = (buffer[0] << 2) | (buffer[1] >> 4);
if (bufferLength > 2)
bytes[length++] = (buffer[1] << 4) | (buffer[2] >> 2);
if (bufferLength > 3)
bytes[length++] = (buffer[2] << 6) | buffer[3];
}
bytes = realloc(bytes, length);
return [NSData dataWithBytesNoCopy:bytes length:length];
}
/************************************************************
函数名称 : + (NSString *)base64EncodedStringFrom:(NSData *)data
函数描述 : 文本数据转换为base64格式字符串
输入参数 : (NSData *)data
输出参数 : N/A
返回参数 : (NSString *)
备注信息 :
**********************************************************/
+ (NSString *)base64EncodedStringFrom:(NSData *)data
{
if ([data length] == 0)
return @"";
char *characters = malloc((([data length] + 2) / 3) * 4);
if (characters == NULL)
return nil;
NSUInteger length = 0;
NSUInteger i = 0;
while (i < [data length])
{
char buffer[3] = {0,0,0};
short bufferLength = 0;
while (bufferLength < 3 && i < [data length])
buffer[bufferLength++] = ((char *)[data bytes])[i++];
// Encode the bytes in the buffer to four characters, including padding "=" characters if necessary.
characters[length++] = encodingTable[(buffer[0] & 0xFC) >> 2];
characters[length++] = encodingTable[((buffer[0] & 0x03) << 4) | ((buffer[1] & 0xF0) >> 4)];
if (bufferLength > 1)
characters[length++] = encodingTable[((buffer[1] & 0x0F) << 2) | ((buffer[2] & 0xC0) >> 6)];
else characters[length++] = '=';
if (bufferLength > 2)
characters[length++] = encodingTable[buffer[2] & 0x3F];
else characters[length++] = '=';
}
return [[NSString alloc] initWithBytesNoCopy:characters length:length encoding:NSASCIIStringEncoding freeWhenDone:YES];
}
@end
//之后引入这对文件就可以了