本文共 1826 字,大约阅读时间需要 6 分钟。
DES加密(支持ARC与MRC)
源文件:
YXCrypto.h 与 YXCrypto.m
//// YXCrypto.h// 用秘钥给字符串加密或者解密//// Created by YouXian on 14-11-16.// Copyright (c) 2014年 YouXian. All rights reserved.//#import@interface YXCrypto : NSObject/*! * 给字符串加密 */+ (NSString *)DesEncryptString:(NSString*)src WithKey:(NSString *)key;/*! * 给字符串解密 */+ (NSString *)DesDecryptString:(NSString*)src WithKey:(NSString *)key;@end
//// YXCrypto.m// 用秘钥给字符串加密或者解密//// Created by YouXian on 14-11-16.// Copyright (c) 2014年 YouXian. All rights reserved.//#import "YXCrypto.h"#import#if __has_feature(objc_arc)// ARC#define Auto_Release(obj)#define Safe_Release(obj)#else// 非ARC#define Auto_Release(obj) [obj autorelease]#define Safe_Release(obj) [obj release]; obj = nil#endifstatic YXCrypto *shareInstance = nil;@implementation YXCrypto/*! * 给字符串加密 */+ (NSString *)DesEncryptString:(NSString*)src WithKey:(NSString *)key { NSString* strRet = @""; if (shareInstance == nil) { shareInstance = [[YXCrypto alloc] init]; } // encrypt source content NSData* bytes = [src dataUsingEncoding:NSUTF8StringEncoding]; NSData* data = [shareInstance DesCryptWithOperation:kCCEncrypt bytes:bytes key:key]; // format bytes to visible string char* pBuff = (char*)[data bytes]; for (int i=0; i , ?, @, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F, }; // decode source content to bytes unsigned char* bytes = (unsigned char*)malloc((src.length+1)*sizeof(unsigned char)); [[src uppercaseString] getCString:(char*)bytes maxLength:src.length+1 encoding:NSUTF8StringEncoding]; unsigned char *p1 = bytes, *p2 = bytes; unsigned long n = src.length/2; for (int i=0; i
此类是用类方法,使用非常便利:
转载地址:http://enato.baihongyu.com/