----------------------------iOS本地加密备忘php+java+perl+javas+C#+object-c 解决方案http://www.cnblogs.com/yipu/p/3913774.html----------------------------使用NSKeyedArchiver创建一个NSData从字典(NSKeyedArchiverarchivedDataWithRootObject :)。然后加密的NSData的AES和写入到文件中。读取相反:首先,阅读NSData的,通过从链路进行解密,然后把解密后的NSData到NSKeyedUnarchiver(NSKeyedUnarchiverunarchiveObjectWithData :),会得回字典。
----------------------------
Encryption.h文件
[plain]view plaincopy- #import < Foundation/Foundation.h>
- @classNSString;
- @interfaceNSData(Encryption)
- -(NSData*)AES256EncryptWithKey:(NSString*)key;//加密
- -(NSData*)AES256DecryptWithKey:(NSString*)key;//解密
- -(NSString*)newStringInBase64FromData;//追加64编码
- +(NSString*)base64encode:(NSString*)str;//同上64编码
- @end
Encryption.m文件
[plain]view plaincopy- #import"Encryption.h"
- #import< CommonCrypto/CommonCryptor.h>
- staticcharbase64[]="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
- @implementationNSData(Encryption)
- -(NSData*)AES256EncryptWithKey:(NSString*)key//加密
- {
- charkeyPtr[kCCKeySizeAES256+1];
- bzero(keyPtr,sizeof(keyPtr));
- [keygetCString:keyPtrmaxLength:sizeof(keyPtr)encoding:NSUTF8StringEncoding];
- NSUIntegerdataLength=[selflength];
- size_tbufferSize=dataLength+kCCBlockSizeAES128;
- void*buffer=malloc(bufferSize);
- size_tnumBytesEncrypted=0;
- CCCryptorStatuscryptStatus=CCCrypt(kCCEncrypt,kCCAlgorithmAES128,
- kCCOptionPKCS7Padding|kCCOptionECBMode,
- keyPtr,kCCBlockSizeAES128,
- NULL,
- [selfbytes],dataLength,
- buffer,bufferSize,
- &numBytesEncrypted);
- if(cryptStatus==kCCSuccess){
- return[NSDatadataWithBytesNoCopy:bufferlength:numBytesEncrypted];
- }
- free(buffer);
- returnnil;
- }
- -(NSData*)AES256DecryptWithKey:(NSString*)key//解密
- {
- charkeyPtr[kCCKeySizeAES256+1];
- bzero(keyPtr,sizeof(keyPtr));
- [keygetCString:keyPtrmaxLength:sizeof(keyPtr)encoding:NSUTF8StringEncoding];
- NSUIntegerdataLength=[selflength];
- size_tbufferSize=dataLength+kCCBlockSizeAES128;
- void*buffer=malloc(bufferSize);
- size_tnumBytesDecrypted=0;
- CCCryptorStatuscryptStatus=CCCrypt(kCCDecrypt,kCCAlgorithmAES128,
- kCCOptionPKCS7Padding|kCCOptionECBMode,
- keyPtr,kCCBlockSizeAES128,
- NULL,
- [selfbytes],dataLength,
- buffer,bufferSize,
- &numBytesDecrypted);
- if(cryptStatus==kCCSuccess){
- return[NSDatadataWithBytesNoCopy:bufferlength:numBytesDecrypted];
- }
- free(buffer);
- returnnil;
- }
- -(NSString*)newStringInBase64FromData//追加64编码
- {
- NSMutableString*dest=[[NSMutableStringalloc]initWithString:@""];
- unsignedchar*working=(unsignedchar*)[selfbytes];
- intsrcLen=[selflength];
- for(int i=0;i< srcLen; i += 3)
- for(intnib=0;nib<4;nib++){
- intbyt=(nib==0)?0:nib-1;
- intix=(nib+1)*2;
- if(i+byt>=srcLen)break;
- unsignedcharcurr=((working[i+byt]<<(8-ix))&0x3F);
- if(i+nib<srcLen)curr|=((working[i+nib]>>ix)&0x3F);
- [destappendFormat:@"%c",base64[curr]];
- }
- }
- returndest;
- }
- +(NSString*)base64encode:(NSString*)str
- {
- if([strlength]==0)
- return@"";
- constchar*source=[strUTF8String];
- intstrlength=strlen(source);
- char*characters=malloc(((strlength+2)/3)*4);
- if(characters==NULL)
- returnnil;
- NSUIntegerlength=0;
- NSUIntegeri=0;
- while(i<strlength){
- charbuffer[3]={0,0,0};
- shortbufferLength=0;
- while(bufferLength<3&&i<strlength)
- buffer[bufferLength++]=source[i++];
- characters[length++]=base64[(buffer[0]&0xFC)>>2];
- characters[length++]=base64[((buffer[0]&0x03)<<4)|((buffer[1]&0xF0)>>4)];
- if(bufferLength>1)
- characters[length++]=base64[((buffer[1]&0x0F)<<2)|((buffer[2]&0xC0)>>6)];
- elsecharacters[length++]='=';
- if(bufferLength>2)
- characters[length++]=base64[buffer[2]&0x3F];
- elsecharacters[length++]='=';
- }
- NSString*g=[[[NSStringalloc]initWithBytesNoCopy:characterslength:lengthencoding:NSASCIIStringEncodingfreeWhenDone:YES]autorelease];
- returng;
- }
- @end
- ------------------------------------------------------------------------------------------------
- 测试代码
- #import"Encryption.h"
- NSString*key=@"mypassword";
- NSString*secret=@"texttoencrypt";
- //加密
- NSData*plain=[secretdataUsingEncoding:NSUTF8StringEncoding];
- NSData*cipher=[plainAES256EncryptWithKey:key];
- NSLog(@"%@",[[ciphernewStringInBase64FromData]autorelease]);
- printf("%sn",[[cipherdescription]UTF8String]);
- NSLog(@"%@",[[[NSStringalloc]initWithData:cipherencoding:NSUTF8StringEncoding]autorelease]);//打印出null,这是因为没有解密。
- //解密
- plain=[cipherAES256DecryptWithKey:key];
- printf("%sn",[[plaindescription]UTF8String]);
- NSLog(@"%@",[[[NSStringalloc]initWithData:plainencoding:NSUTF8StringEncoding]autorelease]);
- //打印出secret的内容,用密码解密过了。如果使用错误的密码,则打印null