BlowFish加密算法在php的使用
1 <?php
2
3 $cipher = mcrypt_module_open(MCRYPT_BLOWFISH, '', MCRYPT_MODE_CBC, '');
4
5 // The block-size of the Blowfish algorithm is 64-bits, therefore our IV
6 // is always 8 bytes:
7 $iv = '12345678';
8
9 $key256 = '1234567890123456ABCDEFGHIJKLMNOP';
10 $key128 = '1234567890123456';
11
12 printf("iv: %s\n",bin2hex($iv));
13 printf("key256: %s\n",bin2hex($key256));
14 printf("key128: %s\n",bin2hex($key128));
15
16 $cleartext = 'The quick brown fox jumped over the lazy dog';
17 printf("clearText: %s\n\n",$cleartext);
18
19 // Do 256-bit blowfish encryption:
20 // The strengh of the encryption is determined by the length of the key
21 // passed to mcrypt_generic_init
22 if (mcrypt_generic_init($cipher, $key256, $iv) != -1)
23 {
24 // PHP pads with NULL bytes if $cleartext is not a multiple of the block size..
25 $cipherText = mcrypt_generic($cipher,$cleartext );
26 mcrypt_generic_deinit($cipher);
27
28 // Display the result in hex.
29 printf("256-bit blowfish encrypted:\n%s\n\n",bin2hex($cipherText));
30 }
31
32 // 128-bit blowfish encryption:
33 if (mcrypt_generic_init($cipher, $key128, $iv) != -1)
34 {
35 // PHP pads with NULL bytes if $cleartext is not a multiple of the block size..
36 $cipherText = mcrypt_generic($cipher,$cleartext );
37 mcrypt_generic_deinit($cipher);
38
39 // Display the result in hex.
40 printf("128-bit blowfish encrypted:\n%s\n\n",bin2hex($cipherText));
41 }
42
43 // -------
44 // Results
45 // -------
46 // You may use these as test vectors for testing your Blowfish implementations...
47 //
48 // iv: 3132333435363738
49 // key256: 313233343536373839303132333435364142434445464748494a4b4c4d4e4f50
50 // key128: 31323334353637383930313233343536
51 // clearText: The quick brown fox jumped over the lazy dog
52 //
53 // 256-bit blowfish encrypted:
54 // 276855ca6c0d60f7d9708210440c1072e05d078e733b34b4198d609dc2fcc2f0c30926cdef3b6d52baf6e345aa03f83e
55 //
56 // 128-bit blowfish encrypted:
57 // d2b5abb73208aea3790621d028afcc74d8dd65fb9ea8e666444a72523f5ecca60df79a424e2c714fa6efbafcc40bdca0
58
59 ?>
60
2
3 $cipher = mcrypt_module_open(MCRYPT_BLOWFISH, '', MCRYPT_MODE_CBC, '');
4
5 // The block-size of the Blowfish algorithm is 64-bits, therefore our IV
6 // is always 8 bytes:
7 $iv = '12345678';
8
9 $key256 = '1234567890123456ABCDEFGHIJKLMNOP';
10 $key128 = '1234567890123456';
11
12 printf("iv: %s\n",bin2hex($iv));
13 printf("key256: %s\n",bin2hex($key256));
14 printf("key128: %s\n",bin2hex($key128));
15
16 $cleartext = 'The quick brown fox jumped over the lazy dog';
17 printf("clearText: %s\n\n",$cleartext);
18
19 // Do 256-bit blowfish encryption:
20 // The strengh of the encryption is determined by the length of the key
21 // passed to mcrypt_generic_init
22 if (mcrypt_generic_init($cipher, $key256, $iv) != -1)
23 {
24 // PHP pads with NULL bytes if $cleartext is not a multiple of the block size..
25 $cipherText = mcrypt_generic($cipher,$cleartext );
26 mcrypt_generic_deinit($cipher);
27
28 // Display the result in hex.
29 printf("256-bit blowfish encrypted:\n%s\n\n",bin2hex($cipherText));
30 }
31
32 // 128-bit blowfish encryption:
33 if (mcrypt_generic_init($cipher, $key128, $iv) != -1)
34 {
35 // PHP pads with NULL bytes if $cleartext is not a multiple of the block size..
36 $cipherText = mcrypt_generic($cipher,$cleartext );
37 mcrypt_generic_deinit($cipher);
38
39 // Display the result in hex.
40 printf("128-bit blowfish encrypted:\n%s\n\n",bin2hex($cipherText));
41 }
42
43 // -------
44 // Results
45 // -------
46 // You may use these as test vectors for testing your Blowfish implementations...
47 //
48 // iv: 3132333435363738
49 // key256: 313233343536373839303132333435364142434445464748494a4b4c4d4e4f50
50 // key128: 31323334353637383930313233343536
51 // clearText: The quick brown fox jumped over the lazy dog
52 //
53 // 256-bit blowfish encrypted:
54 // 276855ca6c0d60f7d9708210440c1072e05d078e733b34b4198d609dc2fcc2f0c30926cdef3b6d52baf6e345aa03f83e
55 //
56 // 128-bit blowfish encrypted:
57 // d2b5abb73208aea3790621d028afcc74d8dd65fb9ea8e666444a72523f5ecca60df79a424e2c714fa6efbafcc40bdca0
58
59 ?>
60