在Objective-C代码中解密来自blowfish的值 [英] decrypt value from blowfish in Objective-C code

查看:137
本文介绍了在Objective-C代码中解密来自blowfish的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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



你可以从这里下载我的代码:



请注意,在此源.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_DECRYPT20zE1E47BE57 $ 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 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)应用程序:(UIApplication *)应用程序didFinishLaunchingWithOptions:(NSDictionary *)launchOptions 
{
NSLog(@%@, [NSString stringWithCString:[
[self PandoraDecrypt:@aed5c110d793f850521a4dd3a56a70d9] bytes]
encoding:NSASCIIStringEncoding]);
返回YES;
}

所以这主要是我方面的一项研究,请给予实施者的信任。 Blowfish api和pandora api ;-)
我的NSLog也用于研究目的,它突出了解密的工作原理。


I am recieving the Encrypted data by server (BLOWFISH ALGORITHM) , I have to decrypt it by using blowfish algorithm in IOS.

you can donwload my code from here : https://www.dropbox.com/s/nswsm7des7isgd5/BlowfishTest-4.zip

I am struggling from 2 days with this task , I try lot of links and find few useful :

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

In third link, i got ECB ( I have to decrypt using ECB). but this code also not gives correct output after decryption.

I am using a online tool for testing and this shows correct output : 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)

and I am getting : ¹àÀhÒ¢º¹iÂF

Here is my code :

//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);

Note : Server side data is Encrypted using BLOWFISH in ECB mode, and converted to hexadecimal notation.

解决方案

1) Source of Blowfish routines from David Madore: ftp://quatramaran.ens.fr/pub/madore/misc/blowfish.c

Pls note that in this source .h part should be separated from the .c file.

2) To use Pandora API we have to use the passwords given by its wiki page here: http://pan-do-ra-api.wikia.com/wiki/Json/5/partners

Currently decrypt password is: 20zE1E47BE57$51

3) Use this code snippet (standing on great programmers' shoulders) - original Pandora API implementation is here: https://github.com/alexcrichton/hermes

In AppDelegate.h (for simplicity)

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

In 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;
}

And you can use this like:

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

So it was mainly a research from my side, pls give credit to implementers of the Blowfish api and the pandora api ;-) Also my NSLogs are for research purpose, it highlights how the decryption works.

这篇关于在Objective-C代码中解密来自blowfish的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆