DES加密
2016-06-27 16:50
337 查看
创建一个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
相关文章推荐
- UITextField监控输入文字变化方法
- RecyclerView添加点击事件与效果
- 练习四 1022
- 广告计算——平滑CTR
- 什么是互联网时代的新雇佣关系?
- 阿里面经之解答by cmershen(1)——Java的基本特性,面向对象的六大特征等
- Ubuntu 14.04快速搭建SVN服务器及日常使用
- 发布本地项目到Git@OS
- centos 安装 rabbitmq
- sql server 两表联合更新
- 总结网络IO模型与select模型的Python实例讲解
- Linux防火墙iptables详解(二)--参数指令
- iOS开发值NSUserDefaults类概述
- [置顶] iOS常用的公共方法
- centos7挂载U盘笔记
- 临时表空间概念
- 程序是怎样跑起来的 —— 预处理
- 剑指offer题解 数组中的逆序对
- c++之类与对象
- delphi数据库存储图片