xcode 如何在 iOS 中实现 Blowfish 算法

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13507384/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 02:09:50  来源:igfitidea点击:

How to implement Blowfish algorithm in iOS

iphoneiosxcodeipadblowfish

提问by Ratikanta Patra

What is the best way to implement BlowFish ECBencryption in iOS??? I have been googling a lot and found the library here. But there are no documentation of this library. Not sure how to use it.

iOS 中实现BlowFish ECB加密的最佳方法是什么???我一直在谷歌上搜索了很多,在这里找到了图书馆。但是没有这个库的文档。不确定如何使用它。

采纳答案by Stas

I got Paul Kocher implementation from Bruce Schneier's website. And here is how an encryption method may look like:

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

#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];
}

I am not really good in C, but it seems that my implementation works correctly. To decrypt you need just repeat same steps, but instead of Blowfish_Encryptyou need to call Blowfish_Decrypt.
Here is a source code for that (I assume that you just decrypt the cipher text, but don't deal with padding here):

我不太擅长 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;
}

You might want to return pure bytes or Base64 string. For the last case I have a category, which adds an initialiser, which initialises NSData object with Base64 string and a method, which allows to get Base64 string from NSData.

您可能想要返回纯字节或 Base64 字符串。对于最后一种情况,我有一个类别,它添加了一个初始化程序,它使用 Base64 字符串和一个方法初始化 NSData 对象,该方法允许从 NSData 获取 Base64 字符串。

You should also think about playing with PADDING_PHRASE, for example, what if you want to add not just several spaces, but some random bytes? In this case you should send a padding length somehow.

您还应该考虑使用PADDING_PHRASE,例如,如果您不仅要添加几个空格,还要添加一些随机字节怎么办?在这种情况下,您应该以某种方式发送填充长度。

Update:Actually, you should notuse PADDING_PRASE in your process. Instead, you should use one of the standard algorithms for block ciphers described on Wikipedia page

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

回答by Peter Hosey

Apple's own CommonCrypto API provides (among other things) a Blowfish implementation. You can encrypt and decrypt in either CBC (the default) or ECB modes.

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

See CommonCrypto.h, CommonCryptor.h, and the CommonCrypto manpage for documentation.

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

回答by Can Tecim

I've written a native implementation for blowfish algorithm since there was no implementation to fit my needs some time ago

我已经为河豚算法编写了一个本地实现,因为前段时间没有满足我需要的实现

Maybe this is an old question but i want to help someone who needs a native class for blowfish algorithm.

也许这是一个老问题,但我想帮助那些需要河豚算法本地类的人。

Its works fully compatible with PHP

它的作品与 PHP 完全兼容

An Objective-C Blowfish Implementation

  • Supports EBC and CBC mode
  • Supports padding RFC and Zero padding
  • Works compatible with PHP's Mcrypt
  • Originally coded for iOS SDK. It may work also for OS X SDK

Objective-C Blowfish 实现

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

More on github;

更多关于 github;

https://github.com/cantecim/FclBlowfish

https://github.com/cantecim/FclBlowfish