【问题标题】:decrypt value from blowfish in Objective-C code在 Objective-C 代码中解密河豚的值
【发布时间】:2013-10-02 15:13:22
【问题描述】:

我正在通过服务器(BLOWFISH ALGORITHM)接收加密数据,我必须使用 IOS 中的河豚算法对其进行解密。

你可以从这里下载我的代码:https://www.dropbox.com/s/nswsm7des7isgd5/BlowfishTest-4.zip

我已经为这个任务苦苦挣扎了 2 天,我尝试了很多链接并发现一些有用的链接:

  1. Blowfish Source code
  2. How to implement Blowfish algorithm in iOS
  3. http://www.codeding.com/articles/blowfish-encryption-algorithm-for-iphone

在第三个链接中,我得到了 ECB(我必须使用 ECB 解密)。但是这段代码在解密后也没有给出正确的输出。

我正在使用在线工具进行测试,这显示了正确的输出:http://www.tools4noobs.com/online_tools/decrypt/

Key = 20zE1E47BE57$51
Input value is = aed5c110d793f850521a4dd3a56a70d9
Algorithm = BLOWFISH
Mode = ECB
Decode the input using= Hexa

output = aYzY1380188405  ( this is correct output which i want)

我得到:¹àÀhÒ¢º¹iÂF

这是我的代码:

//Mode selected by default in nib: “ECB”
NSString *modeString = [encryptionModeControl titleForSegmentAtIndex:encryptionModeControl.selectedSegmentIndex];
BlowfishAlgorithm *blowFish = [BlowfishAlgorithm new];
[blowFish setMode:[BlowfishAlgorithm buildModeEnum:modeString]];
[blowFish setKey:key];
[blowFish setInitVector:initVector];
[blowFish setupKey];

NSString *cipherText = cipherTextView.text;
NSString *plainText = [blowFish decrypt:cipherText];

NSLog(@"cipher-text: %@", cipherText);
NSLog(@"plain-text: %@", plainText);

注意:服务器端数据在 ECB 模式下使用 BLOWFISH 加密,并转换为十六进制表示法。

【问题讨论】:

  • 请显示一些代码。没有正确处理编码和填充是最常见的错误。
  • @MarcusAdams 你可以从这里下载我的代码:dropbox.com/s/nswsm7des7isgd5/BlowfishTest-4.zip
  • 您是否有理由使用第三方的 Blowfish 实现而不是 Apple 的 CommonCrypto API?
  • @QueueOverFlow:你应该问一个单独的问题,显示你的 CommonCrypto 代码,以及你给它的输入和你期望的输出。
  • 您确定输出正确吗?在找到 Pandora api 实现后,我尝试管理示例应用程序并得到以下输出: ---使用此代码行--- NSLog(@"%@", [self PandoraDecrypt:@"aed5c110d793f850521a4dd3a56a70d9"] );

标签: ios objective-c blowfish


【解决方案1】:

1) David Madore 的 Blowfish 例程来源:ftp://quatramaran.ens.fr/pub/madore/misc/blowfish.c

请注意,在此源代码中 .h 部分应与 .c 文件分开。

2) 要使用 Pandora API,我们必须在此处使用其 wiki 页面提供的密码: http://pan-do-ra-api.wikia.com/wiki/Json/5/partners

目前解密密码为:20zE1E47BE57$51

3) 使用这段代码 sn-p(站在伟大程序员的肩膀上)- 原始 Pandora API 实现在这里:https://github.com/alexcrichton/hermes

在 AppDelegate.h 中(为简单起见)

#define PARTNER_DECRYPT  "20zE1E47BE57$51"
...
-(NSData*) PandoraDecrypt:(NSString*) string;

在 AppDelegate.m 中

static char h2i[256] = {
    ['0'] = 0, ['1'] = 1, ['2'] = 2, ['3'] = 3, ['4'] = 4, ['5'] = 5, ['6'] = 6,
    ['7'] = 7, ['8'] = 8, ['9'] = 9, ['a'] = 10, ['b'] = 11, ['c'] = 12,
    ['d'] = 13, ['e'] = 14, ['f'] = 15
};

static void appendByte(unsigned char byte, void *_data) {
    NSMutableData *data = (__bridge NSMutableData*) _data;
    NSLog(@"pre: %@", data);
    [data appendBytes:&byte length:1];
    NSLog(@"post: %@", data);
}

-(NSData*) PandoraDecrypt:(NSString*) string {
    struct blf_ecb_ctx ctx;
    NSMutableData *mut = [[NSMutableData alloc] init];

    Blowfish_ecb_start(&ctx, FALSE, (unsigned char*) PARTNER_DECRYPT,
                       sizeof(PARTNER_DECRYPT) - 1, appendByte,
                       (__bridge void*) mut);

    const char *bytes = [string cStringUsingEncoding:NSASCIIStringEncoding];
    int len = [string lengthOfBytesUsingEncoding:NSASCIIStringEncoding];
    int i;
    for (i = 0; i < len; i += 2) {
        NSLog(@"%c, %c, %d, %d", bytes[i], bytes[i+1], h2i[(int) bytes[i]] * 16, h2i[(int) bytes[i + 1]]);
        Blowfish_ecb_feed(&ctx, h2i[(int) bytes[i]] * 16 + h2i[(int) bytes[i + 1]]);
    }
    Blowfish_ecb_stop(&ctx);

    return mut;
}

你可以这样使用:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    NSLog(@"%@", [NSString stringWithCString:[
                  [self PandoraDecrypt:@"aed5c110d793f850521a4dd3a56a70d9"] bytes]
                           encoding:NSASCIIStringEncoding]);
    return YES;
}

所以这主要是我的一项研究,请感谢 Blowfish api 和 pandora api 的实施者;-) 另外我的 NSLogs 用于研究目的,它突出了解密的工作原理。

【讨论】:

  • 感谢您的代码对我有很大帮助。只是一个小问题。最后我得到“aYzY1380188405 ~Ixÿ”几个特殊字符。我们可以从最后删除这些特殊字符吗?输出应该是“aYzY1380188405”。
  • 我必须深入研究河豚代码,因为它来自第二个 8 字符块的解密。
  • 我尝试使用blowfish.online-domain-tools.com 测试您的输入,它返回的结果与我的代码相同。我们能否找到不同的输入来进一步验证实施?
  • 也许你用来获取返回值的工具(aYzY1380188405)只是剪切了返回字符串的不可读部分......?这个返回值的来源是什么?
  • 您可以使用这些输入。 352dd4320d80f2c1f884cadfaaa5f475 ee892f7be4053057aee4872b29772ea7 6a6bb864b6b8e63749f3cdfd00c16d4f 注意:使用 R=U!LH$O2B# 密钥进行解密。
猜你喜欢
  • 2016-02-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-04-30
  • 1970-01-01
  • 2012-01-27
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多