iOS DES 加密转base64

 
//用法 加密转base 64
NSString *str = [self base64StringFromText:@"qingjoin" withKey:@"key"]; //转base 64 解密
NSString *stred = [self textFromBase64String:str withKey:@"key"];


//文本先进行DES加密。然后再转成base64
+ (NSString *)base64StringFromText:(NSString *)text withKey:(NSString*)key
{
if (text && ![text isEqualToString:@""]) {
//取项目的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 @"";
}
} //先把base64转为文本。然后再DES解密
+ (NSString *)textFromBase64String:(NSString *)base64 withKey:(NSString*)key
{
if (base64 && ![base64 isEqualToString:@""]) {
//取项目的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 @"";
}
} /******************************************************************************
函数名称 : + (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+];
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 = ;
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+];
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 = ;
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 *)
备注信息 :
******************************************************************************/
static const char encodingTable[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; + (NSData *)dataWithBase64EncodedString:(NSString *)string
{
if (string == nil)
[NSException raise:NSInvalidArgumentException format:nil];
if ([string length] == )
return [NSData data]; static char *decodingTable = NULL;
if (decodingTable == NULL)
{
decodingTable = malloc();
if (decodingTable == NULL)
return nil;
memset(decodingTable, CHAR_MAX, );
NSUInteger i;
for (i = ; i < ; 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] + ) / ) * );
if (bytes == NULL)
return nil;
NSUInteger length = ; NSUInteger i = ;
while (YES)
{
char buffer[];
short bufferLength;
for (bufferLength = ; bufferLength < ; 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 == )
break;
if (bufferLength == ) // 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[] << ) | (buffer[] >> );
if (bufferLength > )
bytes[length++] = (buffer[] << ) | (buffer[] >> );
if (bufferLength > )
bytes[length++] = (buffer[] << ) | buffer[];
} 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] == )
return @""; char *characters = malloc((([data length] + ) / ) * );
if (characters == NULL)
return nil;
NSUInteger length = ; NSUInteger i = ;
while (i < [data length])
{
char buffer[] = {,,};
short bufferLength = ;
while (bufferLength < && 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[] & 0xFC) >> ];
characters[length++] = encodingTable[((buffer[] & 0x03) << ) | ((buffer[] & 0xF0) >> )];
if (bufferLength > )
characters[length++] = encodingTable[((buffer[] & 0x0F) << ) | ((buffer[] & 0xC0) >> )];
else characters[length++] = '=';
if (bufferLength > )
characters[length++] = encodingTable[buffer[] & 0x3F];
else characters[length++] = '=';
} return [[NSString alloc] initWithBytesNoCopy:characters length:length encoding:NSASCIIStringEncoding freeWhenDone:YES];
}

原文地址: http://www.cnblogs.com/thefeelingofsimple/archive/2012/11/29/2794724.html

demo下载地址:https://github.com/qingjoin/iOSDESEncryptDemo

上一篇:c#中的常用ToString()方法总结


下一篇:沫沫金::如果使用了ireport的crosstab组建并且选择了其他样式报错了怎么办,看这里