iOS 中 AES256 的实现 - 更喜欢 C 实现,OC 封装的风格
2014-01-11 21:10
429 查看
iOS 中 AES256 的实现 - 更喜欢 C 实现,OC 封装的风格
太阳火神的美丽人生 (http://blog.csdn.net/opengl_es)
本文遵循“署名-非商业用途-保持一致”创作公用协议
转载请保留此句:太阳火神的美丽人生 - 本博客专注于 敏捷开发及移动和物联设备研究:iOS、Android、Html5、Arduino、pcDuino,否则,出自本博客的文章拒绝转载或再转载,谢谢合作。
这段话摘自维基百科:
高级加密标准(Advanced
Encryption Standard,AES),在密码学中又称Rijndael加密法,是美国联邦政府采用的一种区块加密标准。这个标准用来替代原先的DES,已经被多方分析且广为全世界所使用。经过五年的甄选流程,高级加密标准由美国国家标准与技术研究院(NIST)于2001年11月26日发布于FIPS
PUB 197,并在2002年5月26日成为有效的标准。2006年,高级加密标准已然成为对称密钥加密中最流行的算法之一。
该算法为比利时密码学家Joan Daemen和Vincent Rijmen所设计,结合两位作者的名字,以Rijndael为名投稿高级加密标准的甄选流程。(Rijndael的发音近于 "Rhine doll")
[隐藏]
1 沿革
2 密码说明
2.1 AddRoundKey
步骤
2.2 SubBytes
步骤
2.3 ShiftRows
步骤
2.4 MixColumns
步骤
2.5 加密算法优化
3 安全性
3.1 旁道攻击
4 另见
5 注释和参考资料
6 外部链接
6.1 实现
在网上搜了一下AES256,居然有这么一篇“闲来无事,写个AES(256位)加解密程序”,是Java中的实现,有空转载一下。
待我用iOS中的加解密和JAVA中的这篇互相验证确实可互逆时,那么我就可以 “原创” 一篇 “整理iOS和Android与C#.NET服务器之间AES256的加解密研究整理”,一定很受欢迎。
完全是乔布斯的理论??@@@!!!
素材已经是现成的了,在2011年时就已经搜集整理好,以备后用。只不过那个实现是使用的iOS的框架加强功能,并未与C#调通,这个确需进一步验证。
下面还是直接贴源代码吧,至于源代码哪儿来的,俺也不太清楚了,至少有一点可以明确,不是我写的,我是个上层建筑师,并不关心,也不愿意花这些时间去研究土质是什么样的,处于什么样的地层带,我关心的是建筑的风格(其实,更多是因为不懂土质学问题吧?!哈哈)。
声明部分:
实现部分:
太阳火神的美丽人生 (http://blog.csdn.net/opengl_es)
本文遵循“署名-非商业用途-保持一致”创作公用协议
转载请保留此句:太阳火神的美丽人生 - 本博客专注于 敏捷开发及移动和物联设备研究:iOS、Android、Html5、Arduino、pcDuino,否则,出自本博客的文章拒绝转载或再转载,谢谢合作。
这段话摘自维基百科:
高级加密标准(Advanced
Encryption Standard,AES),在密码学中又称Rijndael加密法,是美国联邦政府采用的一种区块加密标准。这个标准用来替代原先的DES,已经被多方分析且广为全世界所使用。经过五年的甄选流程,高级加密标准由美国国家标准与技术研究院(NIST)于2001年11月26日发布于FIPS
PUB 197,并在2002年5月26日成为有效的标准。2006年,高级加密标准已然成为对称密钥加密中最流行的算法之一。
该算法为比利时密码学家Joan Daemen和Vincent Rijmen所设计,结合两位作者的名字,以Rijndael为名投稿高级加密标准的甄选流程。(Rijndael的发音近于 "Rhine doll")
目录
[隐藏] 1 沿革
2 密码说明
2.1 AddRoundKey
步骤
2.2 SubBytes
步骤
2.3 ShiftRows
步骤
2.4 MixColumns
步骤
2.5 加密算法优化
3 安全性
3.1 旁道攻击
4 另见
5 注释和参考资料
6 外部链接
6.1 实现
在网上搜了一下AES256,居然有这么一篇“闲来无事,写个AES(256位)加解密程序”,是Java中的实现,有空转载一下。
待我用iOS中的加解密和JAVA中的这篇互相验证确实可互逆时,那么我就可以 “原创” 一篇 “整理iOS和Android与C#.NET服务器之间AES256的加解密研究整理”,一定很受欢迎。
完全是乔布斯的理论??@@@!!!
素材已经是现成的了,在2011年时就已经搜集整理好,以备后用。只不过那个实现是使用的iOS的框架加强功能,并未与C#调通,这个确需进一步验证。
下面还是直接贴源代码吧,至于源代码哪儿来的,俺也不太清楚了,至少有一点可以明确,不是我写的,我是个上层建筑师,并不关心,也不愿意花这些时间去研究土质是什么样的,处于什么样的地层带,我关心的是建筑的风格(其实,更多是因为不懂土质学问题吧?!哈哈)。
声明部分:
#import <Foundation/Foundation.h> #import <CommonCrypto/CommonCryptor.h> @interface NSData (AES256) - (NSData *)AES256EncryptWithKey:(NSString *)key; - (NSData *)AES256DecryptWithKey: (NSString *)key; @end
实现部分:
#import "NSData+AES256.h" @implementation NSData (AES256) - (NSData *)AES256EncryptWithKey:(NSString *)key { // 'key' should be 32 bytes for AES256, will be null-padded otherwise char keyPtr[kCCKeySizeAES256+1]; // room for terminator (unused) bzero(keyPtr, sizeof(keyPtr)); // fill with zeroes (for padding) // fetch key data [key getCString:keyPtr maxLength:sizeof(keyPtr) encoding:NSUTF8StringEncoding]; NSUInteger dataLength = [self length]; //See the doc: For block ciphers, the output size will always be less than or //equal to the input size plus the size of one block. //That's why we need to add the size of one block here size_t bufferSize = dataLength + kCCBlockSizeAES128; void *buffer = malloc(bufferSize); size_t numBytesEncrypted = 0; CCCryptorStatus cryptStatus = CCCrypt(kCCEncrypt, kCCAlgorithmAES128, kCCOptionPKCS7Padding, keyPtr, kCCKeySizeAES256, NULL /* initialization vector (optional) */, [self bytes], dataLength, /* input */ buffer, bufferSize, /* output */ &numBytesEncrypted); if (cryptStatus == kCCSuccess) { //the returned NSData takes ownership of the buffer and will free it on deallocation return [NSData dataWithBytesNoCopy:buffer length:numBytesEncrypted]; } free(buffer); //free the buffer; return nil; } - (NSData *)AES256DecryptWithKey: (NSString *)key { // 'key' should be 32 bytes for AES256, will be null-padded otherwise char keyPtr[kCCKeySizeAES256+1]; // room for terminator (unused) bzero(keyPtr, sizeof(keyPtr)); // fill with zeroes (for padding) // fetch key data [key getCString:keyPtr maxLength:sizeof(keyPtr) encoding:NSUTF8StringEncoding]; NSUInteger dataLength = [self length]; //See the doc: For block ciphers, the output size will always be less than or //equal to the input size plus the size of one block. //That's why we need to add the size of one block here size_t bufferSize = dataLength + kCCBlockSizeAES128; void *buffer = malloc(bufferSize); size_t numBytesDecrypted = 0; CCCryptorStatus cryptStatus = CCCrypt(kCCDecrypt, kCCAlgorithmAES128, kCCOptionPKCS7Padding, keyPtr, kCCKeySizeAES256, NULL /* initialization vector (optional) */, [self bytes], dataLength, /* input */ buffer, bufferSize, /* output */ &numBytesDecrypted); if (cryptStatus == kCCSuccess) { //the returned NSData takes ownership of the buffer and will free it on deallocation return [NSData dataWithBytesNoCopy:buffer length:numBytesDecrypted]; } free(buffer); //free the buffer; return nil; } @end
相关文章推荐
- 自定义实现简单的ios风格的弹窗加载进度框,一步步走向封装
- Android实现类似iOS风格的对话框实例代码
- iOS开发 - OC - 实现本地数据存储的几种方式二(直接使用sqlite)
- iOS中HTML与OC的交互实现
- ios开发级联菜单(利用父子控制器--两个菜单封装为两个子控制器来实现)
- iOS实现无限循环图片轮播器的封装
- iOS组件封装的思路和实现
- IOS实现自动循环滚动广告--ScrollView的优化和封装
- IOS中 使用JavaScriptCore 实现OC与JS的交互
- iOS 9 新特性之实现 3D Touch 就是 So easy -- OC版
- iOS OC中分类Category实现原理
- 简单封装sqlite3 实现实体对象与关系型数据库的相关操作【IOS】
- [iOS] 使用WebViewJavascriptBridge实现OC与JS交互
- iOS - UICollectionView 自定义布局之风火轮[译] (原版为swift,我这里的实现改为OC)
- iOS开发,音效的播放简单实现以及音效播放的简单封装
- iOS开发之OC与swift开发混编教程,代理的相互调用,block的实现。OC调用Swift中的代理, OC调用Swift中的Block 闭包,swift 3.0
- iOS算法笔记-快速排序-OC实现
- iOS兑换码框的实现(OC)
- IOS 纯代码实现视图控制器的封装
- ios 弹幕实现及封装