iOS 對稱算法DES加解密

//

//? NSString+DES.m

//? test

//

//? Created by 范云飛 on 2018/5/4.

//? Copyright ? 2018年 范云飛. All rights reserved.

//

#import "NSString+DES.h"

#import <CommonCrypto/CommonDigest.h>

#import <CommonCrypto/CommonCryptor.h>

@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

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容