如何在iOS中实现Blowfishalgorithm

什么是在iOS中实施BlowFish ECBencryption的最佳方式? 我一直在googlesearch,并在这里find了图书馆。 但是这个库没有文档。 不知道如何使用它。

我从Bruce Schneier的网站上获得了Paul Kocher的实施。 以下是encryption方法的样子:

#define PADDING_PHRASE @" " #import "CryptoUtilities.h" #import "blowfish.h" #import "NSData+Base64Utilities.h" @implementation CryptoUtilities + (NSString *)blowfishEncrypt:(NSData *)messageData usingKey:(NSData *)secretKey { NSMutableData *dataToEncrypt = [messageData mutableCopy]; if ([dataToEncrypt length] % 8) { NSMutableData *emptyData = [[PADDING_PHRASE dataUsingEncoding:NSUTF8StringEncoding] mutableCopy]; emptyData.length = 8 - [dataToEncrypt length] % 8; [dataToEncrypt appendData:emptyData]; } // Here we have data ready to encipher BLOWFISH_CTX ctx; Blowfish_Init (&ctx, (unsigned char*)[secretKey bytes], [secretKey length]); NSRange aLeftRange, aRightRange; NSData *aLeftBox, *aRightBox; unsigned long dl = 0, dr = 0; for (int i = 0; i < [dataToEncrypt length]; i += 8) { // Divide data into octets... // …and then into quartets aLeftRange = NSMakeRange(i, 4); aRightRange = NSMakeRange(i + 4, 4); aLeftBox = [dataToEncrypt subdataWithRange:aLeftRange]; aRightBox = [dataToEncrypt subdataWithRange:aRightRange]; // Convert bytes into unsigned long [aLeftBox getBytes:&dl length:sizeof(unsigned long)]; [aRightBox getBytes:&dr length:sizeof(unsigned long)]; // Encipher Blowfish_Encrypt(&ctx, &dl, &dr); // Put bytes back [dataToEncrypt replaceBytesInRange:aLeftRange withBytes:&dl]; [dataToEncrypt replaceBytesInRange:aRightRange withBytes:&dr]; } return [dataToEncrypt getBase64String]; } 

我在C中并不是很好,但似乎我的实现正常工作。 解密你只需要重复相同的步骤,而不是Blowfish_Encrypt你需要调用Blowfish_Decrypt
这里是一个源代码(我假设你只是解密密文,但不在这里处理填充):

 + (NSData *)blowfishDecrypt:(NSData *)messageData usingKey:(NSData *)secretKeyData { NSMutableData *decryptedData = [messageData mutableCopy]; BLOWFISH_CTX ctx; Blowfish_Init (&ctx, (unsigned char*)[secretKeyData bytes], [secretKeyData length]); NSRange aLeftRange, aRightRange; NSData *aLeftBox, *aRightBox; unsigned long dl = 0, dr = 0; for (int i = 0; i< [decryptedData length]; i += 8) { // Divide data into octets... // …and then into quartets aLeftRange = NSMakeRange(i, 4); aRightRange = NSMakeRange(i + 4, 4); aLeftBox = [decryptedData subdataWithRange:aLeftRange]; aRightBox = [decryptedData subdataWithRange:aRightRange]; // Convert bytes into unsigned long [aLeftBox getBytes:&dl length:sizeof(unsigned long)]; [aRightBox getBytes:&dr length:sizeof(unsigned long)]; // Decipher Blowfish_Decrypt(&ctx, &dl, &dr); // Put bytes back [decryptedData replaceBytesInRange:aLeftRange withBytes:&dl]; [decryptedData replaceBytesInRange:aRightRange withBytes:&dr]; } return decryptedData; } 

您可能想要返回纯字节或Base64string。 对于最后一种情况,我有一个类,它添加了一个初始化,它初始化NSData对象与Base64string和一个方法,它允许从NSData Base64string。

你也应该考虑玩PADDING_PHRASE,例如,如果你想添加不只是几个空格,而是一些随机字节? 在这种情况下,你应该发送一个填充长度。

更新:其实,你不应该在你的过程中使用PADDING_PRASE。 相反,您应该使用维基百科页面上描述的分组密码的标准algorithm之一

苹果自己的CommonCrypto API提供了(除其他外)Blowfish实现。 您可以在CBC(默认)或ECB模式下进行encryption和解密。

有关文档,请参见CommonCrypto.h,CommonCryptor.h和CommonCrypto联机帮助页。

我已经编写了一个河豚algorithm的本地实现,因为前一段时间没有适合我的需求的实现

也许这是一个古老的问题,但我想帮助需要一个本地类的人blowfishalgorithm。

它的作品完全兼容PHP

一个Objective-C的Blowfish实现

  • 支持EBC和CBC模式
  • 支持填充RFC和零填充
  • 适用于PHP的Mcrypt
  • 最初编码为iOS SDK。 它也可以用于OS X SDK

更多关于github;

https://github.com/cantecim/FclBlowfish