在Objective-C代码中解密来自blowfish的值

Que*_*low 3 blowfish objective-c ios

我收到服务器的加密数据(BLOWFISH ALGORITHM),我必须在IOS中使用blowfish算法解密它.

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

我在这项任务的2天内苦苦挣扎,我尝试了很多链接并找到了一些有用的东西:

  1. Blowfish源代码
  2. 如何在iOS中实现Blowfish算法
  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)
Run Code Online (Sandbox Code Playgroud)

我得到了:¹àÀhÒ¢º¹iF

这是我的代码:

//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);
Run Code Online (Sandbox Code Playgroud)

注意:服务器端数据在ECB模式下使用BLOWFISH加密,并转换为十六进制表示法. 在此输入图像描述

nzs*_*nzs 5

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)使用此代码片段(站在优秀程序员的肩膀上) - 原始Pandora API实现在这里:https://github.com/alexcrichton/hermes

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

#define PARTNER_DECRYPT  "20zE1E47BE57$51"
...
-(NSData*) PandoraDecrypt:(NSString*) string;
Run Code Online (Sandbox Code Playgroud)

在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;
}
Run Code Online (Sandbox Code Playgroud)

你可以这样使用:

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

所以它主要是我方面的一项研究,请给Blowfish api和pandora api的实施者一些信誉;-)另外我的NSLogs用于研究目的,它突出了解密的工作原理.