ios平台下的DES加密
?
?
?
今天終于把ios平臺下的DES加密算法調(diào)通了,在這里記錄一下。說一下我遇到的問題吧。
?
第一,關(guān)于傳參,Objective-C和C,C++一樣,不能把值類型數(shù)組做為參數(shù),傳給另一個方法,方法的返回值的類型也一樣不可以是值類型數(shù)組。一旦這么做了,接受參數(shù)的方法只能獲取數(shù)組中的首元素。
?
? 解決方法是用 NSMutableData 把值類型的數(shù)組包一層,代碼如下:
?
NSMutableData* bufkeyData =[NSMutableData?dataWithLength:2];
int* bufkey = bufkeyData.mutableBytes;
?
? ??傳參 ? ??[self?make_key:bufkeyData?number:j];?? ? ? ? ? ? ? ? ? ??
?取值 ?? int* bufkey = bufkeyData.mutableBytes;
?
?
第二,NSData 與 Byte[]之間的轉(zhuǎn)換
?
?
?NSData?*btsData = http://www.cnblogs.com/upwifi/archive/2011/09/05/[in_strdataUsingEncoding:NSUTF8StringEncoding];
?
?
Byte?*byteData = http://www.cnblogs.com/upwifi/archive/2011/09/05/(Byte*)malloc(len);
memcpy(byteData, [btsData?bytes], len);
?
?
NSMutableData?*bts2Data = http://www.cnblogs.com/upwifi/archive/2011/09/05/[NSMutableDatadataWithLength:len];
?
[bts2Data?initWithBytes:byteData?length:len];
?
第三,需要用一下 Base64?給加密前的和加密后的字串做Encod和Decode處理,我用了第三方的NSData+Base64.m
?
?
?? ? 下面是頭文件:
?
//
//? DesEncrypt.h
//? DesEncryptDemo
//
//? Created by fred yu on 9/2/11.
//? Copyright 2011 __MyCompanyName__. All rights reserved.
//
?
#import?<Foundation/Foundation.h>
?
enum?DesStrategy?{
? ??DesSimple?=?1,
? ??Des3?=?2,
?? ?DesCBC?=?3,
?? ?DesTwoKeys?=?4
};
typedef?enum?DesStrategy DesStrategy;
?
?
?
@interface?DesEncrypt : NSObject {
?
?? ?NSMutableArray?*keyArray;
?
}
?
?
?
-(NSData?*)des:(NSData?*)input_data key:(NSString?*)key encrypt:(bool)encrypt;
?
//Encrypt
-(NSString*)encrypt:(NSString?*)in_str key:(NSString?*)key desMode:(DesStrategy)desMode str2:(NSString?*)str2;
?
//Decrypt
-(NSString*)decrypt:(NSString?*)in_str key:(NSString?*)key desMode:(DesStrategy)desMode str2:(NSString?*)str2;
?
//DesCBC
-(NSMutableData?*)desCBC:(NSMutableData?*)input_data key:(NSString?*)key_str iv:(NSString?*)iv encrypt:(bool)encrypt;
?
-(NSMutableData?*)des:(NSMutableData?*)data key:(NSMutableData?*)key iv:(NSMutableData?*)iv encrypt:(bool)encrypt;
?
//DesCreateKeys
-(void)desCreateKeys:(NSMutableData?*)key;
?
//make_data
-(void)make_data:(NSMutableData?*)data number:(int)number;
?
//make_key
-(NSMutableData?*)make_key:(NSMutableData?*)in_key number:(int)number;
?
//handle_data
-(NSMutableData?*)handle_data:(NSMutableData?*)data encrypt:(bool)encrypt;
?
//change_data
-(NSMutableData?*)change_data:(NSMutableData?*)olddata change_tbType:(int)change_tbType;
?
?
?
@end
?
?
?
?? ?測試代碼及輸出結(jié)果:
?
?
#import?<Foundation/Foundation.h>
#import?"DesEncrypt.h"
?
int?main (int?argc,?const?char?* argv[]) {
? ??NSAutoreleasePool?* pool = [[NSAutoreleasePool?alloc]?init];
?
? ??// insert code here...
?
? ??DesEncrypt?* desEncrypt = [[DesEncrypt?alloc]?init];
?
NSString?*key1 =?@"12345678";
NSString?*key2 =?@"45678923";
?
NSString?*msg =?@"abcdefghijk";
?
NSLog(@"消息: %@",msg);
?
NSString?*s = [desEncrypt?encrypt:msg?key:key1?desMode:DesCBC?str2:key2];
?
NSLog(@"經(jīng)過加密處理: %@",s);
?
NSString?*ss = [desEncrypt?decrypt:s?key:key1?desMode:DesCBC?str2:key2];
?
NSLog(@"經(jīng)過解密處理: %@",ss);
?
? ? [pool?drain];
? ??return?0;
}
?
?
?
fred-yus-MacBook-Pro:~ fred$ /Users/fred/Documents/DesEncryptDemo/build/Debug/DesEncryptDemo ; exit;
2011-09-05 13:05:08.552 DesEncryptDemo[6085:903] 消息: abcdefghijk
2011-09-05 13:05:08.555 DesEncryptDemo[6085:903] 經(jīng)過加密處理: vL5Am3KcszqpJdB594OrkA==
2011-09-05 13:05:08.555 DesEncryptDemo[6085:903] 經(jīng)過解密處理: abcdefghijk
logout
總結(jié)
以上是生活随笔為你收集整理的ios平台下的DES加密的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: [转]PowerDesigner使用教程
- 下一篇: HINSTANCE和HANDLE的区别