123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390 |
- //
- // DesUtil.m
- // jiaPei
- //
- // Created by apple on 2016/12/27.
- // Copyright © 2016年 JCZ. All rights reserved.
- //
-
- #import "DesUtil.h"
- #import <CommonCrypto/CommonCryptor.h>
- //空字符串
- #define LocalStr_None @""
- static const char encodingTable[] = "fujianxzz";
- @implementation DesUtil
- + (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:@""];
- 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];
- }
- //加密
- +(NSString *) encryptUseDES:(NSString *)plainText key:(NSString *)key;
- {
- NSString *ciphertext = nil;
- const char *textBytes = [plainText UTF8String];
- NSUInteger dataLength = [plainText length];
- unsigned char buffer[1024];
- memset(buffer, 0, sizeof(char));
- size_t numBytesEncrypted = 0;
- CCCryptorStatus cryptStatus = CCCrypt(kCCEncrypt, kCCAlgorithmDES,
- kCCOptionPKCS7Padding,
- [key UTF8String], kCCKeySizeDES,
- NULL,
- textBytes, dataLength,
- buffer, 1024,
- &numBytesEncrypted);
- if (cryptStatus == kCCSuccess) {
- NSData *data = [NSData dataWithBytes:buffer length:(NSUInteger)numBytesEncrypted];
- ciphertext = [self dataTohexString:data];
- }
-
- return ciphertext;
- }
- //解密
- +(NSString *) decryptUseDES:(NSString *)plainText key:(NSString *)key
- {
- NSString *cleartext = nil;
- NSData *textData = [self hexStringToData:plainText];
- NSUInteger dataLength = [textData length];
- unsigned char buffer[1024];
- memset(buffer, 0, sizeof(char));
- size_t numBytesEncrypted = 0;
-
- CCCryptorStatus cryptStatus = CCCrypt(kCCDecrypt, kCCAlgorithmDES,kCCOptionPKCS7Padding,
- [key UTF8String], kCCKeySizeDES,
- NULL,
- [textData bytes] , dataLength,
- buffer, 1024,
- &numBytesEncrypted);
- if (cryptStatus == kCCSuccess) {
- //NSLog(@"DES解密成功");
- NSData *data = [NSData dataWithBytes:buffer length:(NSUInteger)numBytesEncrypted];
- cleartext = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
- }else{
- //NSLog(@"DES解密失败");
- }
- return cleartext;
- }
- + (NSString *)dataTohexString:(NSData*)data
- {
- Byte *bytes = (Byte *)[data bytes];
- NSString *hexStr=@"";
- for(int i=0;i<[data length];i++)
- {
-
- int intTmp = bytes[i];
- // 把负数转换为正数
- if (intTmp < 0) {
- intTmp = intTmp + 256;
- }
-
-
- NSString *newHexStr = [NSString stringWithFormat:@"%x",intTmp&0xff];//16进制数
-
- // 小于0F的数需要在前面补0
- if (intTmp < 16) {
- hexStr = [NSString stringWithFormat:@"%@0%@",hexStr,newHexStr];
- }else{
- hexStr = [NSString stringWithFormat:@"%@%@",hexStr,newHexStr];
- }
-
-
-
- // NSString *newHexStr = [NSString stringWithFormat:@"%x",bytes[i]&0xff];//16进制数
-
- // int intTmp = [newHexStr intValue];
- //
- // if (intTmp < 0) {
- // intTmp = intTmp + 256;
- // }
- // NSLog(@"---->%@---->%d",newHexStr,intTmp);
- //
- // newHexStr = [NSString stringWithFormat:@"%d",intTmp];
- //
- // if([newHexStr length]==1)
- // hexStr = [NSString stringWithFormat:@"%@0%@",hexStr,newHexStr];
- // else
- // hexStr = [NSString stringWithFormat:@"%@%@",hexStr,newHexStr];
- }
- return hexStr;
- }
- + (NSData*)hexStringToData:(NSString*)hexString
- {
- //NSString *hexString = @"3e435fab9c34891f"; //16进制字符串
- int j=0;
- Byte bytes[hexString.length]; ///3ds key的Byte 数组, 128位
- for(int i=0;i<[hexString length];i++)
- {
- int int_ch; /// 两位16进制数转化后的10进制数
-
- unichar hex_char1 = [hexString characterAtIndex:i]; ////两位16进制数中的第一位(高位*16)
- int int_ch1;
- if(hex_char1 >= '0' && hex_char1 <='9')
- int_ch1 = (hex_char1-48)*16; //// 0 的Ascll - 48
- else if(hex_char1 >= 'A' && hex_char1 <='F')
- int_ch1 = (hex_char1-55)*16; //// A 的Ascll - 65
- else
- int_ch1 = (hex_char1-87)*16; //// a 的Ascll - 97
- i++;
-
- unichar hex_char2 = [hexString characterAtIndex:i]; ///两位16进制数中的第二位(低位)
- int int_ch2;
- if(hex_char2 >= '0' && hex_char2 <='9')
- int_ch2 = (hex_char2-48); //// 0 的Ascll - 48
- else if(hex_char1 >= 'A' && hex_char1 <='F')
- int_ch2 = hex_char2-55; //// A 的Ascll - 65
- else
- int_ch2 = hex_char2-87; //// a 的Ascll - 97
-
- int_ch = int_ch1+int_ch2;
- //NSLog(@"int_ch=%x",int_ch);
- bytes[j] = int_ch; ///将转化后的数放入Byte数组里
- j++;
- }
- // NSData *newData = [[NSData alloc] initWithBytes:bytes length:j];
- NSData *newData = [[NSData alloc] initWithBytes:bytes length:j];
- //NSLog(@"newData=%@",newData);
- return newData;
- }
- @end
|