iOS下的DES加密问题汇总

以下是用DES加密会遇到的长见问题: 

PS: 想看DES加密详解大请到这里: http://bbs.9ria.com/thread-242572-1-1.html

1.关于传参,Objective-C和C,C++一样,不能把值类型数组做为参数,传给另一个方法,方法的返回值的类型也一样不可以是值类型数组。一旦这么做了,接受参数的方法只能获取数组中的首元素。    

解决方法是用 NSMutableData 把值类型的数组包一层,代码如下:    
   

  1. NSMutableData* bufkeyData = [NSMutableDatadataWithLength:2];
  2.    int* bufkey = bufkeyData.mutableBytes;    
  3.     [self make_key:bufkeyData number:j]; // 传参
  4.          int* bufkey = bufkeyData.mutableBytes;// 取值
复制代码

2.NSData 与 Byte[]之间的转换

  1. NSData *btsData = [in_str dataUsingEncoding:NSUTF8StringEncoding];


  2.     Byte *byteData = (Byte*)malloc(len);
  3.     memcpy(byteData, [btsData bytes], len);


  4.     NSMutableData *bts2Data = [NSMutableDatadataWithLength:len];

  5.     [bts2Data initWithBytes:byteData length:len];  
复制代码

3.i386架构下定义值类型变量需赋初值     

  1. //下面是头文件赋初值
  2. // DesEncrypt.h
  3. // DesEncryptDemo
  4. //
  5. // Created by fred yu on 9/2/11.
  6. // Copyright 2011 __MyCompanyName__. All rights reserved.
  7. //

  8. #import <Foundation/Foundation.h>

  9. enumDesStrategy {
  10. DesSimple = 1,
  11. Des3 = 2,
  12. DesCBC = 3,
  13. DesTwoKeys = 4
  14. };
  15. typedef enum DesStrategy DesStrategy;



  16. @interface DesEncrypt : NSObject {

  17. NSMutableArray *keyArray;

  18. }



  19. -(NSData *)des:(NSData *)input_data key:(NSString *)key encrypt:(bool)encrypt;

  20. //Encrypt
  21. -(NSString*)encrypt:(NSString *)in_str key:(NSString *)key desMode:(DesStrategy)desMode str2:(NSString *)str2;

  22. //Decrypt
  23. -(NSString*)decrypt:(NSString *)in_str key:(NSString *)key desMode:(DesStrategy)desMode str2:(NSString *)str2;

  24. //DesCBC
  25. -(NSMutableData *)desCBC:(NSMutableData *)input_data key:(NSString *)key_str iv:(NSString *)iv encrypt:(bool)encrypt;

  26. -(NSMutableData *)des:(NSMutableData *)data key:(NSMutableData *)key iv:(NSMutableData *)iv encrypt:(bool)encrypt;

  27. //DesCreateKeys
  28. -(void)desCreateKeys:(NSMutableData *)key;

  29. //make_data
  30. -(void)make_data:(NSMutableData *)data number:(int)number;

  31. //make_key
  32. -(NSMutableData *)make_key:(NSMutableData *)in_key number:(int)number;

  33. //handle_data
  34. -(NSMutableData *)handle_data:(NSMutableData *)data encrypt:(bool)encrypt;

  35. //change_data
  36. -(NSMutableData *)change_data:(NSMutableData *)olddata change_tbType:(int)change_tbType;



  37. @end    



  38. 测试代码及输出结果:    


  39. #import <Foundation/Foundation.h>
  40. #import "DesEncrypt.h"

  41. int main (int argc, const char * argv[]) {
  42. NSAutoreleasePool * pool = [[NSAutoreleasePoolalloc] init];

  43. // insert code here...

  44. DesEncrypt * desEncrypt = [[DesEncrypt alloc] init];

  45.    NSString *key1 = @"23245321";
  46.    NSString *key2 = @"77585210";

  47.    NSString *msg = @"abcdefghijk";

  48.    NSLog(@"消息: %@",msg);

  49.    NSString *s = [desEncrypt encrypt:msg key:key1 desMode:DesCBC str2:key2];

  50.    NSLog(@"经过加密处理: %@",s);

  51.    NSString *ss = [desEncrypt decrypt:s key:key1 desMode:DesCBC str2:key2];

  52.    NSLog(@"经过解密处理: %@",ss);

  53. [pool drain];
  54. return 0;
  55. } 
复制代码


结果:  

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] 经过加密处理: vL5Am3KcszqpJdB594OrkA==
2011-09-05 13:05:08.555 DesEncryptDemo[6085:903] 经过解密处理: abcdefghijk
logout 

 

原文地址:http://bbs.9ria.com/thread-242599-1-1.html

你可能感兴趣的:(ios,加密,解密)