dqhnp44220 2015-11-05 08:48
浏览 59
已采纳

随机生成的PHP IV在IOS中无法正常工作

The problem is the base 64 encoded iv(in PHP) doesn't match when decoded in IOS

The scenario is I am using Blowfish Algorithm CBC mode . Simply server(PHP) generate a random cryptographic IV and encoded base64 format , send to IOS . The Problem is here when-time I try to decode , the decoded results are not correct always . Sometime it generates correct and rest of time error.

Here is my PHP code.

public function ivGenerator()
{
    $ivSize = mcrypt_get_iv_size(MCRYPT_BLOWFISH, MCRYPT_MODE_CBC);
    $iv = mcrypt_create_iv($ivSize, MCRYPT_RAND);
    echo base64_encode($iv);

}

Here is two result set of ivGenerator which i will use later for decoding

bTpkpLxMbNo=
aIvJeujxW7w=

Here is my IOS code .

NSString *ecodedIVString = @"bTpkpLxMbNo=";    
NSData *ecodedIVData= [[NSData alloc] initWithBase64EncodedString:ecodedIVString options:0];
NSString *decodeIVString = [[NSString alloc] initWithData:ecodedIVData encoding:NSASCIIStringEncoding];
NSLog(@"decodeIVString  %@" , decodeIVString); // m:d¤¼LlÚ   //its correct 

If I use ,

NSString *ecodedIVString = @"aIvJeujxW7w="; 

Then the result is hÉzèñ[¼ , but the actual result should be h‹Ézèñ[¼

I have tried

  NSWindowsCP1252StringEncoding , NSISOLatin1StringEncoding ,      NSUTF8StringEncoding 

But all of them are unable to generate accurate result (approx. 10 test I have found it).

Thanks in advance !

  • 写回答

1条回答 默认 最新

  • doumiang0597 2015-11-05 13:12
    关注

    You should print out the result in hexadecimals and then perform a comparison.

    It seems that the encoding on both platforms is the same (something you should not take for granted) so you get the same characters back. However, it isn't sure that the font or font version you are using shows the characters. It could also be that one platform shows an character for unknown encoding sequences, and the other doesn't show anything at all.

    This brings us to the basic issue: if you decode random byte values then you may encounter byte sequences that do not translate to a character. So comparing binary strings by converting them to a printable string is no good. Instead you need to directly compare the bytes (e.g. by saving the IV to iv.bin files) or by translating them into hexadecimals.

    It's extremely likely that the IV's are identical, despite the fact that the printout is different.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 各位请问平行检验趋势图这样要怎么调整?说标准差差异太大了
  • ¥15 delphi webbrowser组件网页下拉菜单自动选择问题
  • ¥15 wpf界面一直接收PLC给过来的信号,导致UI界面操作起来会卡顿
  • ¥15 init i2c:2 freq:100000[MAIXPY]: find ov2640[MAIXPY]: find ov sensor是main文件哪里有问题吗
  • ¥15 运动想象脑电信号数据集.vhdr
  • ¥15 三因素重复测量数据R语句编写,不存在交互作用
  • ¥15 微信会员卡等级和折扣规则
  • ¥15 微信公众平台自制会员卡可以通过收款码收款码收款进行自动积分吗
  • ¥15 随身WiFi网络灯亮但是没有网络,如何解决?
  • ¥15 gdf格式的脑电数据如何处理matlab