Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to work with PHP #1

Closed
gmogames opened this issue Mar 5, 2015 · 3 comments
Closed

How to work with PHP #1

gmogames opened this issue Mar 5, 2015 · 3 comments

Comments

@gmogames
Copy link

gmogames commented Mar 5, 2015

Hi There,

I have the encryption and decryption code in PHP working fine, but I cannot decrypt or encrypt in iOS using this lib.

My encryption and decryption code in PHP are:

Decrypt

$initializationVectorSize = mcrypt_get_iv_size(MCRYPT_BLOWFISH, 'ecb');
$initializationVector = mcrypt_create_iv($initializationVectorSize, MCRYPT_RAND);

$valor = $_GET['v'];
$valor = base64_decode($valor);
$valor = mcrypt_decrypt(MCRYPT_BLOWFISH, 'mYk3yS3c', $valor, 'ecb', $initializationVector);
$valor = trim($valor, " \x00..\x1F");
print 'Valor: ' . $valor;

Encrypt

$initializationVectorSize = mcrypt_get_iv_size(MCRYPT_BLOWFISH, 'ecb');
$initializationVector = mcrypt_create_iv($initializationVectorSize, MCRYPT_RAND);

$v = mcrypt_encrypt(MCRYPT_BLOWFISH, 'mYk3yS3c', $v, 'ecb', $initializationVector);
print base64_encode($v);

one example is the following encrypted string:
cCVGRSYKtjQhyaLfn+lumQ==

In my PHP code it decrypts fines, with the result "21988882121" (without quotes).

But in my decryption code on iOS, I don't know how to properly initialize the vector following the PHP standard.

FclBlowfish *bf = [[FclBlowfish alloc] init];
bf.Key = @"mYk3yS3c";
bf.IV = @"";
[bf prepare];

NSLog(@"decrypt %@", [bf decrypt:@"d5NXWox77nXb0InI+zP98Q==" withMode:modeEBC withPadding:paddingZero]);

Any help is much appreciated!

Thanks!

@gmogames
Copy link
Author

gmogames commented Mar 6, 2015

Just tought you should know that I was able to make it work. The encrypted string FclBlowfish was generating was missing some special unicode characters, so the decryption never worked.

Once I opened the .m file and changed from NSISOLatin1StringEncoding in the decrypt method and from NSASCIIStringEncoding in the encrypt method both to NSWindowsCP1252StringEncoding it worked like a charm.

So, maybe using Fcl only inside iOS may work well, but with PHP it needed a tweak.

I posted a question and answer here by the way:
http://stackoverflow.com/questions/28891658/php-and-objective-c-blowfish-encryption-encoding-different/28891960#28891960

Thanks

@gmogames gmogames closed this as completed Mar 6, 2015
@cantecim
Copy link
Owner

cantecim commented Mar 6, 2015

You can use random ascii coded string to create iv in php
IV must have ascii chars
Simple work around;

$initializationVectorSize = mcrypt_get_iv_size(MCRYPT_BLOWFISH, 'ecb');
$initializationVector = mcrypt_create_iv($initializationVectorSize, MCRYPT_RAND);
$initializationVector = substr(base64_encode($initializationVector), 0, $initializationVectorSize);

You got the idea. In order to work properly with encoding in ios i done it this way

Thanks for your contributions
Feel free to suggest correct way using encoding in ios to work this lib properly
Any pull request is always welcome

@gmogames
Copy link
Author

gmogames commented Mar 6, 2015

The problem is I don't have access to the PHP code since it's hosted and ran by a 3rd party company. They were only kind enought to share with me the methods to encrypt/decrypt and I had to make the iOS code match the encryption/decryption.

So, changing the Encoding you were using in the encrypt and decrypt method to the * NSWindowsCP1252StringEncoding* made the encrypted strings have the same characters as the one in PHP. Using Latin or Ascii limits the symbols used by the encryption and they were not matching.

But thank you for the awesome Lib, I wouldn't have patience to do that own my own!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants