DES加密

来源:互联网 发布:淘宝优惠券jp519 编辑:程序博客网 时间:2024/06/07 11:52

创建一个NSData的类目(category)

#import <Foundation/Foundation.h>@interface NSData (DES)- (NSString *)base64Encoding;+(NSString *) parseByte2HexString:(Byte *) bytes;@end

类目方法的实现

#import "NSData+DES.h"@implementation NSData (DES)static const char encodingTable[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";- (NSString *)base64Encoding;{    if (self.length == 0)        return @"";    char *characters = malloc(self.length*3/2);    if (characters == NULL)        return @"";    int end = self.length - 3;    int index = 0;    int charCount = 0;    int n = 0;    while (index <= end) {        int d = (((int)(((char *)[self bytes])[index]) & 0x0ff) << 16)         | (((int)(((char *)[self bytes])[index + 1]) & 0x0ff) << 8)        | ((int)(((char *)[self bytes])[index + 2]) & 0x0ff);        characters[charCount++] = encodingTable[(d >> 18) & 63];        characters[charCount++] = encodingTable[(d >> 12) & 63];        characters[charCount++] = encodingTable[(d >> 6) & 63];        characters[charCount++] = encodingTable[d & 63];        index += 3;        if(n++ >= 14)        {            n = 0;            characters[charCount++] = ' ';        }    }    if(index == self.length - 2)    {        int d = (((int)(((char *)[self bytes])[index]) & 0x0ff) << 16)         | (((int)(((char *)[self bytes])[index + 1]) & 255) << 8);        characters[charCount++] = encodingTable[(d >> 18) & 63];        characters[charCount++] = encodingTable[(d >> 12) & 63];        characters[charCount++] = encodingTable[(d >> 6) & 63];        characters[charCount++] = '=';    }    else if(index == self.length - 1)    {        int d = ((int)(((char *)[self bytes])[index]) & 0x0ff) << 16;        characters[charCount++] = encodingTable[(d >> 18) & 63];        characters[charCount++] = encodingTable[(d >> 12) & 63];        characters[charCount++] = '=';        characters[charCount++] = '=';    }    NSString * rtnStr = [[NSString alloc] initWithBytesNoCopy:characters length:charCount encoding:NSUTF8StringEncoding freeWhenDone:YES];    return rtnStr;}+(NSString *) parseByte2HexString:(Byte *) bytes{    NSMutableString *hexStr = [[NSMutableString alloc]init];    int i = 0;    if(bytes)    {        while (bytes[i] != '\0')         {            NSString *hexByte = [NSString stringWithFormat:@"%x",bytes[i] & 0xff];///16进制数            if([hexByte length]==1)                [hexStr appendFormat:@"0%@", hexByte];            else                 [hexStr appendFormat:@"%@", hexByte];            i++;        }    }    NSLog(@"bytes 的16进制数为:%@",hexStr);    return hexStr;}+(NSString *) parseByteArray2HexString:(Byte[]) bytes{    NSMutableString *hexStr = [[NSMutableString alloc]init];    int i = 0;    if(bytes)    {        while (bytes[i] != '\0')         {            NSString *hexByte = [NSString stringWithFormat:@"%x",bytes[i] & 0xff];///16进制数            if([hexByte length]==1)                [hexStr appendFormat:@"0%@", hexByte];            else                 [hexStr appendFormat:@"%@", hexByte];            i++;        }    }    NSLog(@"bytes 的16进制数为:%@",hexStr);    return hexStr;}@end

创建一个管理类

#import <Foundation/Foundation.h>#import <CommonCrypto/CommonCrypto.h>@interface MSEncryptUtil : NSObject+(NSString *) encryptUseDES:(NSString *)plainText key:(NSString *)key;@end

方法的实现

#import "NSData+DES.h"#import "MSEncryptUtil.h"static Byte iv[] = {1,2,3,4,5,6,7,8};@implementation MSEncryptUtil+(NSString *) encryptUseDES:(NSString *)plainText key:(NSString *)key{    NSString *ciphertext = nil;    //先编码后再加密    plainText = [plainText stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];    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,                                          iv,                                          textBytes, dataLength,                                          buffer, 1024,                                          &numBytesEncrypted);    if (cryptStatus == kCCSuccess) {        NSData *data = [NSData dataWithBytes:buffer length:(NSUInteger)numBytesEncrypted];        ciphertext = [data base64Encoding];    }    return ciphertext;}@end
2 0