iOS开发 AES加密和解密实现 aes加密解密java实现

【iOS开发】AES加密和解密实现 aes加密解密java实现
----------------------------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
  1. #import < Foundation/Foundation.h>
  2. @classNSString;
  3. @interfaceNSData(Encryption)
  4. -(NSData*)AES256EncryptWithKey:(NSString*)key;//加密
  5. -(NSData*)AES256DecryptWithKey:(NSString*)key;//解密
  6. -(NSString*)newStringInBase64FromData;//追加64编码
  7. +(NSString*)base64encode:(NSString*)str;//同上64编码
  8. @end

Encryption.m文件

[plain]view plaincopy
  1. #import"Encryption.h"
  2. #import< CommonCrypto/CommonCryptor.h>
  3. staticcharbase64[]="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
  4. @implementationNSData(Encryption)
  5. -(NSData*)AES256EncryptWithKey:(NSString*)key//加密
  6. {
  7. charkeyPtr[kCCKeySizeAES256+1];
  8. bzero(keyPtr,sizeof(keyPtr));
  9. [keygetCString:keyPtrmaxLength:sizeof(keyPtr)encoding:NSUTF8StringEncoding];
  10. NSUIntegerdataLength=[selflength];
  11. size_tbufferSize=dataLength+kCCBlockSizeAES128;
  12. void*buffer=malloc(bufferSize);
  13. size_tnumBytesEncrypted=0;
  14. CCCryptorStatuscryptStatus=CCCrypt(kCCEncrypt,kCCAlgorithmAES128,
  15. kCCOptionPKCS7Padding|kCCOptionECBMode,
  16. keyPtr,kCCBlockSizeAES128,
  17. NULL,
  18. [selfbytes],dataLength,
  19. buffer,bufferSize,
  20. &numBytesEncrypted);
  21. if(cryptStatus==kCCSuccess){
  22. return[NSDatadataWithBytesNoCopy:bufferlength:numBytesEncrypted];
  23. }
  24. free(buffer);
  25. returnnil;
  26. }
  27. -(NSData*)AES256DecryptWithKey:(NSString*)key//解密
  28. {
  29. charkeyPtr[kCCKeySizeAES256+1];
  30. bzero(keyPtr,sizeof(keyPtr));
  31. [keygetCString:keyPtrmaxLength:sizeof(keyPtr)encoding:NSUTF8StringEncoding];
  32. NSUIntegerdataLength=[selflength];
  33. size_tbufferSize=dataLength+kCCBlockSizeAES128;
  34. void*buffer=malloc(bufferSize);
  35. size_tnumBytesDecrypted=0;
  36. CCCryptorStatuscryptStatus=CCCrypt(kCCDecrypt,kCCAlgorithmAES128,
  37. kCCOptionPKCS7Padding|kCCOptionECBMode,
  38. keyPtr,kCCBlockSizeAES128,
  39. NULL,
  40. [selfbytes],dataLength,
  41. buffer,bufferSize,
  42. &numBytesDecrypted);
  43. if(cryptStatus==kCCSuccess){
  44. return[NSDatadataWithBytesNoCopy:bufferlength:numBytesDecrypted];
  45. }
  46. free(buffer);
  47. returnnil;
  48. }
  49. -(NSString*)newStringInBase64FromData//追加64编码
  50. {
  51. NSMutableString*dest=[[NSMutableStringalloc]initWithString:@""];
  52. unsignedchar*working=(unsignedchar*)[selfbytes];
  53. intsrcLen=[selflength];
  54. for(int i=0;i< srcLen; i += 3)
  55. for(intnib=0;nib<4;nib++){
  56. intbyt=(nib==0)?0:nib-1;
  57. intix=(nib+1)*2;
  58. if(i+byt>=srcLen)break;
  59. unsignedcharcurr=((working[i+byt]<<(8-ix))&0x3F);
  60. if(i+nib<srcLen)curr|=((working[i+nib]>>ix)&0x3F);
  61. [destappendFormat:@"%c",base64[curr]];
  62. }
  63. }
  64. returndest;
  65. }
  66. +(NSString*)base64encode:(NSString*)str
  67. {
  68. if([strlength]==0)
  69. return@"";
  70. constchar*source=[strUTF8String];
  71. intstrlength=strlen(source);
  72. char*characters=malloc(((strlength+2)/3)*4);
  73. if(characters==NULL)
  74. returnnil;
  75. NSUIntegerlength=0;
  76. NSUIntegeri=0;
  77. while(i<strlength){
  78. charbuffer[3]={0,0,0};
  79. shortbufferLength=0;
  80. while(bufferLength<3&&i<strlength)
  81. buffer[bufferLength++]=source[i++];
  82. characters[length++]=base64[(buffer[0]&0xFC)>>2];
  83. characters[length++]=base64[((buffer[0]&0x03)<<4)|((buffer[1]&0xF0)>>4)];
  84. if(bufferLength>1)
  85. characters[length++]=base64[((buffer[1]&0x0F)<<2)|((buffer[2]&0xC0)>>6)];
  86. elsecharacters[length++]='=';
  87. if(bufferLength>2)
  88. characters[length++]=base64[buffer[2]&0x3F];
  89. elsecharacters[length++]='=';
  90. }
  91. NSString*g=[[[NSStringalloc]initWithBytesNoCopy:characterslength:lengthencoding:NSASCIIStringEncodingfreeWhenDone:YES]autorelease];
  92. returng;
  93. }
  94. @end
  95. ------------------------------------------------------------------------------------------------
  96. 测试代码
  97. #import"Encryption.h"
  98. NSString*key=@"mypassword";
  99. NSString*secret=@"texttoencrypt";
  100. //加密
  101. NSData*plain=[secretdataUsingEncoding:NSUTF8StringEncoding];
  102. NSData*cipher=[plainAES256EncryptWithKey:key];
  103. NSLog(@"%@",[[ciphernewStringInBase64FromData]autorelease]);
  104. printf("%sn",[[cipherdescription]UTF8String]);
  105. NSLog(@"%@",[[[NSStringalloc]initWithData:cipherencoding:NSUTF8StringEncoding]autorelease]);//打印出null,这是因为没有解密。
  106. //解密
  107. plain=[cipherAES256DecryptWithKey:key];
  108. printf("%sn",[[plaindescription]UTF8String]);
  109. NSLog(@"%@",[[[NSStringalloc]initWithData:plainencoding:NSUTF8StringEncoding]autorelease]);
  110. //打印出secret的内容,用密码解密过了。如果使用错误的密码,则打印null
----------------------------

  

爱华网本文地址 » http://www.aihuau.com/a/25101014/216784.html

更多阅读

怎么破解ChinaNet和CMCC实现免费上网 chinanet无线上网密码

怎么破解ChinaNet和CMCC实现免费上网——简介怎么免费接入移动CMCC或电信ChinaNet实现上网功能呢?相信大家都明白,自己的周围明明存在ChinaNet或CMCC信号,但就是无法“为我所用”,今天小编就与各位分享一下破解这类网络实现免费接入网络

iOS开发:用xcode5开发第一个helloworld程序 精 火线精英

iOS开发:用xcode5开发第一个helloworld程序 精——简介任何程序开发的学习都是从helloworld开始的,那么如何建立ios开发的第一个helloworld程序,网上也有很多教程,但是很多是老版本的xcode来写的教程,随着ios7的普及,xcode也升级到了新的

IOS开发系列之json解析阿堂教程 json解析视频教程

在前两篇文章中,阿堂分享了关于ios中xml常用的解析方式的实例,这篇文章,阿堂将会介绍json的解析方式的实例。同样,在介绍之前,阿堂先对常见的json解析框架作下简单总结。 SBJson:它是比较老的json编码/解码框架,原名是json-framework。这

IOS开发百度地图API 百度地图开发者api

IOS百度地图API开发自定义气泡,点击气泡自动生成路线,以及拖拽IOS百度地图开发POISearch搜索附近停车场,附近加油站IOS百度地图视角跳到用户当前位置IOS百度地图开发实时路况IOS开发百度地图自动导航IOS开发百度地图在大头钉上加文字和

声明:《iOS开发 AES加密和解密实现 aes加密解密java实现》为网友爱在风雨里分享!如侵犯到您的合法权益请联系我们删除