Blowfish encryption is implemented using PHP and C ++, but the results are different.

Source: Internet
Author: User
Tags php online
Blowfish encryption is implemented using PHP and C ++, but the results are different. First, the MD5 experiment has the same results, but the Blowfish experiment cannot be used.
The call is as follows:
 % S

", Strtoupper (bin2hex ($ dtext);} mcrypt_module_close ($ cipher );

C ++ is like this:
    MD5_CTX md5;    unsigned char str[16];    md5.MD5String(strSource.c_str() ,str);    BlockCipher *bf;    char key[] = "strkey11";              //Key    bf = new BlowFish();    bf->setKey((void *)key, 8*8);    bf->encrypt((void *)str, 8);      //unsigned char str[16];    bf->encrypt((void *)(str+8), 8);    char temp1[4] = {0};    char buff1[128] = {0};    for(int i = 0;i<16;i++)    {        sprintf(temp1,"%02x",str[i]);        strcat(buff1,temp1);    }    AnsiString strResult = String(buff1).UpperCase();    delete bf;

Reply to discussion (solution)

$ Iv = '000000 ';???
Press bf-> setKey (void *) key, 8*8 );
It should be
$ Iv = "\ x00 \ x00 \ x00 \ x00 \ x00 \ x00 \ x00 \ x00 ";

IV is ignored in ECB. iv must exist in CFB, CBC, STREAM, nOFB and OFB modes.
In MCRYPT_MODE_ECB mode, $ iv is ignored and should not be the problem.

It seems that padding is required before encryption. try again.
$ Size = mcrypt_get_block_size (MCRYPT_BLOWFISH, MCRYPT_MODE_ECB );
$ Input = pkcs5_pad ($ input, $ size );

Function pkcs5_pad ($ text, $ blocksize)
$ Pad = $ blocksize-(strlen ($ text) % $ blocksize );
Return $ text. str_repeat (chr ($ pad), $ pad );

Function pkcs5_unpad ($ text)
$ Pad = ord ($ text {strlen ($ text)-1 });
If ($ pad> strlen ($ text) return false;
If (strspn ($ text, chr ($ pad), strlen ($ text)-$ pad )! = $ Pad) return false;
Return substr ($ text, 0,-1 * $ pad );

Well, you cannot use $ iv.

Is the upstairs method used in the mcrypt module of PHP?

I checked the information. when using the BF encryption class of Pear, we need to first padding

Why not?
If (mcrypt_generic_init ($ cipher, $ key, $ iv )...
$ Iv must be pure binary data, which is the easiest to ignore

Haha, thank you upstairs. I don't know the encryption principle very well, but in this case, obviously $ iv is a ignored parameter. you can test to fill $ iv with a random string to see the encryption result, it will not change.

In addition, this is Blowfish encryption. you can think of it as another algorithm.

Correct the typos: Yes-> Yes

It doesn't matter if the test fails.
Because there is no c ++ environment, the test is useless.

To determine whether the php code is correct, you must provide at least three sets of C ++ encryption results (plaintext and ciphertext)

Thank you. do I still need the C ++ blowfish code? I can email

C ++'s blowfish code is not required. even if there is a problem, it is not a problem discussed by php.
The result is used to check the php running result.

Results of C ++:
Original 1: 38A0E9312DDA8F7C16B9A12159168C76
Password 1: c58bf3eb9ae841_d90ee7020ac7cecb

Original 2: ckgvrgebtsmtklqldqxerbnoxzrefol
Password 2: 0fa050cc105e92ebae4641728561d74

Password 3: 2E49406676B5C4E28057D22EB5D5AF73

Original 4: c58bf3eb9ae841_d90ee7020ac7cecb
Password 4: 5A4D282F291F98A5C0748834925EBCFC

To 9 floor:

I want to provide the C ++ code. I just thought, you may not have the library at hand. I want to test it for you.

Suddenly, it makes no sense to write it like this.
All $ stext = md5 ($ stext, true );
Is decryption still valid?
Since no plaintext can be decrypted, no error is found.

This is obviously irreversible encryption. you don't know how to see it now? In addition, do you still insist that $ iv is necessary?

Please refer to my most initial code ....

My problem is: PHP and C ++ have different results after processing the same plaintext.

Again, I have explained on the first floor that after MD5, the results of C ++ and PHP are the same, but they are different in the Blowfish stage.

In this case, we can avoid the MD5 issue.

That is to say, you only need to look at the second half of the paragraph. after the MD5 operation, the result is okay and there is a problem in the back.

Is there a problem with the result of c ++? here there is an online blowfish encrypted, you can verify it

Bin2hex (md5 ('38a0e9312dda8f7c16b9a12159168c76 ', true ))

= Df22a2ca739c2c2735e3bf4543a5206c

Input this, key is strkey11

The result is:

Different from your results

Thank you. you have processed bin2hex one more step. Will this process affect the final result? I think the input is different, isn't it?

In addition, this token

I have already posted all the C ++ code. I lost a C ++ BF class library. it seems that I used Google to publish it at the time. after using it for a long time, I cannot remember it, but there should be no error. can you leave an Email to help me check it?

I was eager to help you solve the problem, but I was not interested in finding it was irreversible.

Blowfish is a reversible encryption algorithm. the ciphertext length changes with the plaintext length.

First, you need to find out whether PHP is wrong or C ++ is wrong. I used bin2hex before, but md5 is followed by binary data and cannot be verified,
Can you not use the md5 direct character abcdefgh? what is it after encryption?

The webpage result is
Plaintext abcdefgh
Ciphertext 5B4148819C51DCB5

First of all, the encryption results of C ++ are reversible. I think there is no such problem as who is right or who is wrong. I believe the results of PHP are reversible, they only have different formats of ciphertext in the intermediate state of encryption and decryption, and we just use ciphertext for comparison...

Well, C ++ processes abcdefgh without MD5, and the result is 12DB6214F5EAB031, where KEY is "strkey11" and the displayed format is hexadecimal, it is generated by converting each BYTE of the blowfish processing result into a hexadecimal letter.

The reason why I use MD5 in PHP to output binary data is to match C ++. in C ++, Blowfish only receives two groups of bytes for processing, saving conversion and filling, the error process is also missing.

I used PHP online decoding.

5B4148819C51DCB5 can be decrypted, but 12DB6214F5EAB031 cannot be decrypted

Is there anything different between C ++ programs? As long as the data can be encrypted and decrypted, the same algorithm must be followed for mutual decryption.

The same symmetric algorithm, the same key, is unlikely to have different ciphertext values, except for some random interference. But from the blowfish algorithm,
There is no random interference. Is it possible that pbox and sbox are different definitions?

You have two questions about the C ++ code.
1) bf-> setKey (void *) key, 8*8); the second parameter should be the key length. why is it 8*8?
2) PHP has the ECB mode, but c ++ does not. Is it the ECB by default?

I found the reason. your algorithm is different from the blowfish algorithm in PHP. The c ++ algorithm is blowfish-compat.

So change to $ cipher = mcrypt_module_open ('blowfish-compat', '', MCRYPT_MODE_ECB ,'');
This should be done.

Praise upstairs! The problem is solved by 99.9%. this is the algorithm. after the PHP result is displayed, I handle it like this:

But the result is twice longer than expected. In fact, it is now possible to end, because although it is twice longer, the first half of the paragraph is the result I want and can be used.

But there is a small problem. I don't understand the PHP hexadecimal conversion method. how can it be doubled? can it be converted to an equivalent length directly?

Look at the conversion of C ++, the code is like this
I used to look for the principle of blowfish. I didn't see Daming Bai, but I think it should be a position replacement and it won't get longer.

It's perfect to solve this problem.

I know what the problem is. the C ++ program used for verification is removed from the original program and changed. when I process the plaintext as an array, cut half (the data processed in the original program is half the length ...), so it's half-length. you have the right full length.

The post is closed. I have time to sort out this topic and post it on the BLOG. now I cannot find the practice of connecting C ++ and PHP results on the Internet.

Obviously, it is the right choice to make mistakes for you.

I have encountered this problem because the moderator is unreliable. thanks to Meteorlet for its enthusiasm.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.