//
// NSString+DES.m
// test
//
// Created by 范云飞 on 2018/5/4.
// Copyright © 2018年 范云飞. All rights reserved.
//
#import "NSString+DES.h"
#import
#import
@implementation NSString (DES)
- (NSData*)jm_hexStringConvertToBytesData
{
//异常字符串
if(self.length%2!=0) {
returnnil;
}
Bytebytes[1024*3] = {0};
intbytesIndex =0;
for(inti =0; i < [selflength]; i++)
{
intint_char; /// 两位16进制数转化后的10进制数
unicharhex_charUpper = [selfcharacterAtIndex:i];///两位16进制数中的第一位(高位*16)
intint_charUpper;
if(hex_charUpper >='0'&& hex_charUpper <='9') {
int_charUpper = (hex_charUpper -48) *16; // 0 的Ascll - 48
}elseif(hex_charUpper >='A'&& hex_charUpper <='F') {
int_charUpper = (hex_charUpper -55) *16;/// A 的Ascll - 65
}else{
int_charUpper = (hex_charUpper -87) *16;// a 的Ascll - 97
}
i++;
unicharhex_charLower = [selfcharacterAtIndex:i];///两位16进制数中的第二位(低位)
intint_charLower;
if(hex_charLower >='0'&& hex_charLower <='9') {
int_charLower = (hex_charLower -48);/// 0 的Ascll - 48
}elseif(hex_charUpper >='A'&& hex_charUpper <='F') {
int_charLower = (hex_charLower -55);/// A 的Ascll - 65
}else{
int_charLower = hex_charLower -87;/// a 的Ascll - 97
}
int_char = int_charUpper + int_charLower;
bytes[bytesIndex] = int_char; ///将转化后的数放入Byte数组里
bytesIndex++;
}
NSUIntegerdataLength =self.length/2;
NSData*data = [[NSDataalloc]initWithBytes:byteslength:dataLength];
returndata;
}
- (NSString*)jm_urlDecode {
NSString *decodedString = [self stringByRemovingPercentEncoding];
returndecodedString;
}
- (NSString*)jm_urlEncode {
NSString *encodedString = [self stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet characterSetWithCharactersInString:@"!*'();:@&=+$,/?%#[]"]];
returnencodedString;
}
- (NSString*)jm_encryptUseDESByKey:(NSString*)key iv:(NSString*)iv
{
NSString*ciphertext;
NSString*encode = [selfjm_urlEncode];
// NSLog(@"%s encode::%@", __func__, encode);
NSData *data = [encode dataUsingEncoding:NSUTF8StringEncoding];
NSUIntegerdataLength = data.length;
NSUIntegerbufferLength =1024;
unsignedcharbuffer[bufferLength];
memset(buffer,0,sizeof(char));
size_tnumBytesEncrypted =0;
CCCryptorStatus cryptStatus = CCCrypt(kCCEncrypt,
kCCAlgorithmDES,
kCCOptionPKCS7Padding,
[keyUTF8String],
kCCKeySizeDES,
[ivUTF8String] ,//iv向量
[databytes],
dataLength,
buffer,
bufferLength,
&numBytesEncrypted);
if(cryptStatus ==kCCSuccess) {
NSData*data = [NSDatadataWithBytes:bufferlength:(NSUInteger)numBytesEncrypted];
//NSLog(@"%s buffer::%s", __func__, buffer);
//NSLog(@"%s data::%@", __func__, data);
ciphertext =@"";
for(intindex =0; index < data.length; index++) {
charbyte;
[datagetBytes:&byterange:NSMakeRange(index,1)];
NSString*text = [NSStringstringWithFormat:@"%x", byte&0xff];
//不足两位,前面补0
if([textlength] ==1) {
text = [NSStringstringWithFormat:@"0%@", text];
}
ciphertext = [ciphertextstringByAppendingString:text];
}
}
NSLog(@"%s encryptText::%@", __func__, ciphertext);
returnciphertext;
}
- (NSString*)jm_decryptUseDesByKey:(NSString*)key iv:(NSString*)iv
{
NSString*decryptText;
NSData *encryptData = [self jm_hexStringConvertToBytesData];
constchar*textBytes = [encryptDatabytes];
NSUIntegerdataLength = encryptData.length;
NSUIntegerbufferLength = dataLength +0x8&0xfffffff8;
unsignedcharbuffer[bufferLength];
memset(buffer,0,sizeof(char));
size_tnumBytesEncrypted =0;
//将encryptText转化为bytes
CCCryptorStatus decryptStatus = CCCrypt(kCCDecrypt,
kCCAlgorithmDES,
kCCOptionPKCS7Padding,
[keyUTF8String],
kCCKeySizeDES,
[ivUTF8String] ,//iv向量
textBytes,
dataLength,
buffer,
bufferLength,
&numBytesEncrypted);
if(decryptStatus ==kCCSuccess) {
NSLog(@"%s buffer::%s",__func__, buffer);
NSData*data = [NSDatadataWithBytes:bufferlength:(NSUInteger)numBytesEncrypted];
NSLog(@"%s data::%@",__func__, data);
decryptText = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
NSLog(@"%s decryptText::%@",__func__, decryptText);
decryptText = [decryptTextjm_urlDecode];
NSLog(@"%s decodeUrl::%@",__func__, decryptText);
}
returndecryptText;
}
@end