1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799
|
<HTML
><HEAD
><TITLE
>Mcrypt Encryption Functions</TITLE
><META
NAME="GENERATOR"
CONTENT="Modular DocBook HTML Stylesheet Version 1.57"><LINK
REL="HOME"
TITLE="PHP Manual"
HREF="manual.html"><LINK
REL="UP"
TITLE="Function Reference"
HREF="funcref.html"><LINK
REL="PREVIOUS"
TITLE="mcal_expunge"
HREF="function.mcal-expunge.html"><LINK
REL="NEXT"
TITLE="mcrypt_get_cipher_name"
HREF="function.mcrypt-get-cipher-name.html"><META
NAME="HTTP_EQUIV"
CONTENT="text/html; charset=ISO-8859-1"></HEAD
><BODY
CLASS="reference"
BGCOLOR="#FFFFFF"
TEXT="#000000"
LINK="#0000FF"
VLINK="#840084"
ALINK="#0000FF"
><DIV
CLASS="NAVHEADER"
><TABLE
WIDTH="100%"
BORDER="0"
CELLPADDING="0"
CELLSPACING="0"
><TR
><TH
COLSPAN="3"
ALIGN="center"
>PHP Manual</TH
></TR
><TR
><TD
WIDTH="10%"
ALIGN="left"
VALIGN="bottom"
><A
HREF="function.mcal-expunge.html"
>Prev</A
></TD
><TD
WIDTH="80%"
ALIGN="center"
VALIGN="bottom"
></TD
><TD
WIDTH="10%"
ALIGN="right"
VALIGN="bottom"
><A
HREF="function.mcrypt-get-cipher-name.html"
>Next</A
></TD
></TR
></TABLE
><HR
ALIGN="LEFT"
WIDTH="100%"></DIV
><DIV
CLASS="reference"
><A
NAME="ref.mcrypt"
></A
><DIV
CLASS="TITLEPAGE"
><H1
CLASS="title"
>XXXVI. Mcrypt Encryption Functions</H1
><DIV
CLASS="PARTINTRO"
><A
NAME="AEN21198"
></A
><P
> These functions work using <A
HREF="ftp://argeas.cs-net.gr/pub/unix/mcrypt/"
TARGET="_top"
>mcrypt</A
>.
</P
><P
> This is an interface to the mcrypt library, which supports a wide
variety of block algorithms such as DES, TripleDES, Blowfish
(default), 3-WAY, SAFER-SK64, SAFER-SK128, TWOFISH, TEA, RC2 and
GOST in CBC, OFB, CFB and ECB cipher modes. Additionally, it
supports RC6 and IDEA which are considered "non-free".
</P
><P
> If you linked against libmcrypt 2.4.x, the following additional
block algorithms are supported: CAST, LOKI97, RIJNDAEL, SAFERPLUS,
SERPENT and the following stream ciphers: ENIGMA (crypt), PANAMA,
RC4 and WAKE. With libmcrypt 2.4.x another cipher mode is also
available; nOFB.
</P
><P
> To use it, download libmcrypt-x.x.tar.gz from <A
HREF="ftp://argeas.cs-net.gr/pub/unix/mcrypt/"
TARGET="_top"
>here</A
> and follow the included
installation instructions. You need to compile PHP with the
<TT
CLASS="option"
>--with-mcrypt</TT
> parameter to
enable this extension. Make sure you compile libmcrypt with the
option <TT
CLASS="option"
>--disable-posix-threads</TT
>.
</P
><P
> Mcrypt can be used to encrypt and decrypt using the above
mentioned ciphers. If you linked against libmcrypt-2.2.x, the
four important mcrypt commands (<A
HREF="function.mcrypt-cfb.html"
><B
CLASS="function"
>mcrypt_cfb()</B
></A
>,
<A
HREF="function.mcrypt-cbc.html"
><B
CLASS="function"
>mcrypt_cbc()</B
></A
>, <A
HREF="function.mcrypt-ecb.html"
><B
CLASS="function"
>mcrypt_ecb()</B
></A
>,
and <A
HREF="function.mcrypt-ofb.html"
><B
CLASS="function"
>mcrypt_ofb()</B
></A
>) can operate in both modes
which are named MCRYPT_ENCRYPT and MCRYPT_DECRYPT, respectively.
<TABLE
WIDTH="100%"
BORDER="0"
CELLPADDING="0"
CELLSPACING="0"
CLASS="EXAMPLE"
><TR
><TD
><DIV
CLASS="example"
><A
NAME="AEN21212"
></A
><P
><B
>Example 1. Encrypt an input value with TripleDES in ECB mode</B
></P
><TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
WIDTH="100%"
><TR
><TD
><PRE
CLASS="programlisting"
> <?php
$key = "this is a very secret key";
$input = "Let us meet at 9 o'clock at the secret place.";
$encrypted_data = mcrypt_ecb (MCRYPT_TripleDES, $key, $input, MCRYPT_ENCRYPT);
?>
</PRE
></TD
></TR
></TABLE
></DIV
></TD
></TR
></TABLE
>
This example will give you the encrypted data as a string in
<TT
CLASS="literal"
>$encrypted_data</TT
>.
</P
><P
> If you linked against libmcrypt 2.4.x, these functions are still
available, but it is recommended that you use the advanced functions.
<TABLE
WIDTH="100%"
BORDER="0"
CELLPADDING="0"
CELLSPACING="0"
CLASS="EXAMPLE"
><TR
><TD
><DIV
CLASS="example"
><A
NAME="AEN21217"
></A
><P
><B
>Example 2. Encrypt an input value with TripleDES in ECB mode</B
></P
><TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
WIDTH="100%"
><TR
><TD
><PRE
CLASS="programlisting"
> <?php
$key = "this is a very secret key";
$input = "Let us meet at 9 o'clock at the secret place.";
$td = mcrypt_module_open (MCRYPT_TripleDES, "", MCRYPT_MODE_ECB, "");
$iv = mcrypt_create_iv (mcrypt_enc_get_iv_size ($td), MCRYPT_RAND);
mcrypt_generic_init ($td, $key, $iv);
$encrypted_data = mcrypt_generic ($td, $input);
mcrypt_generic_end ($td);
?>
</PRE
></TD
></TR
></TABLE
></DIV
></TD
></TR
></TABLE
>
This example will give you the encrypted data as a string in
<TT
CLASS="literal"
>$encrypted_data</TT
>.
</P
><P
> Mcrypt can operate in four block cipher modes (CBC, OFB, CFB, and
ECB). If linked against libmcrypt-2.4.x mcrypt can also operate
in the block cipher mode nOFB and in STREAM mode. Then there are
also constants in the form MCRYPT_MODE_mode for use with several
functions. We will outline the normal use for each of these modes.
For a more complete reference and discussion see
Applied Cryptography by Schneier (ISBN 0-471-11709-9).
<P
></P
><UL
><LI
><P
> ECB (electronic codebook) is suitable for random data, such as
encrypting other keys. Since data there is short and random,
the disadvantages of ECB have a favorable negative
effect.
</P
></LI
><LI
><P
> CBC (cipher block chaining) is especially suitable for
encrypting files where the security is increased over ECB
significantly.
</P
></LI
><LI
><P
> CFB (cipher feedback) is the best mode for encrypting byte
streams where single bytes must be encrypted.
</P
></LI
><LI
><P
> OFB (output feedback, in 8bit) is comparable to CFB, but
can be used in applications where error propagation cannot
be tolerated. It's insecure (because it operates in 8bit
mode) so it is not recommended to use it.
</P
></LI
><LI
><P
> nOFB (output feedback, in nbit) is comparable to OFB, but
more secure because it operates on the block size of the
algorithm.
</P
></LI
><LI
><P
> STREAM is an extra mode to include some stream algorithms
like WAKE or RC4.
</P
></LI
></UL
>
</P
><P
> PHP does not support encrypting/decrypting bit streams
currently. As of now, PHP only supports handling of strings.
</P
><P
> For a complete list of supported ciphers, see the defines at the
end of <TT
CLASS="filename"
>mcrypt.h</TT
>. The general rule with the
mcrypt-2.2.x API is that you can access the cipher from PHP with
MCRYPT_ciphername. With the mcrypt-2.4.x API these constants also
work, but it is possible to specify the name of the cipher as
a string with a call to <A
HREF="function.mcrypt-module-open.html"
><B
CLASS="function"
>mcrypt_module_open()</B
></A
>.
</P
><P
> Here is a short list of ciphers which are currently supported by
the mcrypt extension. If a cipher is not listed here, but is
listed by mcrypt as supported, you can safely assume that this
documentation is outdated.
<P
></P
><UL
><LI
><P
> MCRYPT_ARCFOUR_IV (libmcrypt 2.4.x only)
</P
></LI
><LI
><P
> MCRYPT_ARCFOUR (libmcrypt 2.4.x only)
</P
></LI
><LI
><P
> MCRYPT_BLOWFISH
</P
></LI
><LI
><P
> MCRYPT_CAST_128
</P
></LI
><LI
><P
> MCRYPT_CAST_256
</P
></LI
><LI
><P
> MCRYPT_CRYPT
</P
></LI
><LI
><P
> MCRYPT_DES
</P
></LI
><LI
><P
> MCRYPT_DES_COMPAT (libmcrypt 2.2.x only)
</P
></LI
><LI
><P
> MCRYPT_ENIGMA (libmcrypt 2.4.x only, alias for MCRYPT_CRYPT)
</P
></LI
><LI
><P
> MCRYPT_GOST
</P
></LI
><LI
><P
> MCRYPT_IDEA (non-free)
</P
></LI
><LI
><P
> MCRYPT_LOKI97 (libmcrypt 2.4.x only)
</P
></LI
><LI
><P
> MCRYPT_MARS (libmcrypt 2.4.x only, non-free)
</P
></LI
><LI
><P
> MCRYPT_PANAMA (libmcrypt 2.4.x only)
</P
></LI
><LI
><P
> MCRYPT_RIJNDAEL_128 (libmcrypt 2.4.x only)
</P
></LI
><LI
><P
> MCRYPT_RIJNDAEL_192 (libmcrypt 2.4.x only)
</P
></LI
><LI
><P
> MCRYPT_RIJNDAEL_256 (libmcrypt 2.4.x only)
</P
></LI
><LI
><P
> MCRYPT_RC2
</P
></LI
><LI
><P
> MCRYPT_RC4 (libmcrypt 2.2.x only)
</P
></LI
><LI
><P
> MCRYPT_RC6 (libmcrypt 2.4.x only)
</P
></LI
><LI
><P
> MCRYPT_RC6_128 (libmcrypt 2.2.x only)
</P
></LI
><LI
><P
> MCRYPT_RC6_192 (libmcrypt 2.2.x only)
</P
></LI
><LI
><P
> MCRYPT_RC6_256 (libmcrypt 2.2.x only)
</P
></LI
><LI
><P
> MCRYPT_SAFER64
</P
></LI
><LI
><P
> MCRYPT_SAFER128
</P
></LI
><LI
><P
> MCRYPT_SAFERPLUS (libmcrypt 2.4.x only)
</P
></LI
><LI
><P
> MCRYPT_SERPENT (libmcrypt 2.4.x only)
</P
></LI
><LI
><P
> MCRYPT_SERPENT_128 (libmcrypt 2.2.x only)
</P
></LI
><LI
><P
> MCRYPT_SERPENT_192 (libmcrypt 2.2.x only)
</P
></LI
><LI
><P
> MCRYPT_SERPENT_256 (libmcrypt 2.2.x only)
</P
></LI
><LI
><P
> MCRYPT_SKIPJACK (libmcrypt 2.4.x only)
</P
></LI
><LI
><P
> MCRYPT_TEAN (libmcrypt 2.2.x only)
</P
></LI
><LI
><P
> MCRYPT_THREEWAY
</P
></LI
><LI
><P
> MCRYPT_TRIPLEDES
</P
></LI
><LI
><P
> MCRYPT_TWOFISH (for older mcrypt 2.x versions, or mcrypt 2.4.x )
</P
></LI
><LI
><P
> MCRYPT_TWOFISH128 (TWOFISHxxx are available in newer 2.x versions, but not in the 2.4.x versions)
</P
></LI
><LI
><P
> MCRYPT_TWOFISH192
</P
></LI
><LI
><P
> MCRYPT_TWOFISH256
</P
></LI
><LI
><P
> MCRYPT_WAKE (libmcrypt 2.4.x only)
</P
></LI
><LI
><P
> MCRYPT_XTEA (libmcrypt 2.4.x only)
</P
></LI
></UL
>
</P
><P
> You must (in CFB and OFB mode) or can (in CBC mode) supply an
initialization vector (IV) to the respective cipher function. The
IV must be unique and must be the same when
decrypting/encrypting. With data which is stored encrypted, you
can take the output of a function of the index under which the
data is stored (e.g. the MD5 key of the filename).
Alternatively, you can transmit the IV together with the encrypted
data (see chapter 9.3 of Applied Cryptography by Schneier (ISBN 0-471-11709-9) for a
discussion of this topic).
</P
></DIV
><DIV
CLASS="TOC"
><DL
><DT
><B
>Table of Contents</B
></DT
><DT
><A
HREF="function.mcrypt-get-cipher-name.html"
>mcrypt_get_cipher_name</A
> — Get the name of the specified cipher</DT
><DT
><A
HREF="function.mcrypt-get-block-size.html"
>mcrypt_get_block_size</A
> — Get the block size of the specified cipher</DT
><DT
><A
HREF="function.mcrypt-get-key-size.html"
>mcrypt_get_key_size</A
> — Get the key size of the specified cipher</DT
><DT
><A
HREF="function.mcrypt-create-iv.html"
>mcrypt_create_iv</A
> —
Create an initialization vector (IV) from a random source
</DT
><DT
><A
HREF="function.mcrypt-cbc.html"
>mcrypt_cbc</A
> — Encrypt/decrypt data in CBC mode</DT
><DT
><A
HREF="function.mcrypt-cfb.html"
>mcrypt_cfb</A
> — Encrypt/decrypt data in CFB mode</DT
><DT
><A
HREF="function.mcrypt-ecb.html"
>mcrypt_ecb</A
> — Encrypt/decrypt data in ECB mode</DT
><DT
><A
HREF="function.mcrypt-ofb.html"
>mcrypt_ofb</A
> — Encrypt/decrypt data in OFB mode</DT
><DT
><A
HREF="function.mcrypt-list-algorithms.html"
>mcrypt_list_algorithms</A
> — Get an array of all supported ciphers</DT
><DT
><A
HREF="function.mcrypt-list-modes.html"
>mcrypt_list_modes</A
> — Get an array of all supported modes</DT
><DT
><A
HREF="function.mcrypt-get-iv-size.html"
>mcrypt_get_iv_size</A
> — Returns the size of the IV belonging to a specific cipher/mode combination</DT
><DT
><A
HREF="function.mcrypt-encrypt.html"
>mcrypt_encrypt</A
> — Encrypts plaintext with given parameters</DT
><DT
><A
HREF="function.mcrypt-decrypt.html"
>mcrypt_decrypt</A
> — Decrypts crypttext with given parameters</DT
><DT
><A
HREF="function.mcrypt-module-open.html"
>mcrypt_module_open</A
> — This function opens the module of the algorithm and the mode to be used</DT
><DT
><A
HREF="function.mcrypt-generic-init.html"
>mcrypt_generic_init</A
> — This function initializes all buffers needed for encryption</DT
><DT
><A
HREF="function.mcrypt-generic.html"
>mcrypt_generic</A
> — This function encrypts data</DT
><DT
><A
HREF="function.mdecrypt-generic.html"
>mdecrypt_generic</A
> — This function decrypts data</DT
><DT
><A
HREF="function.mcrypt-generic-end.html"
>mcrypt_generic_end</A
> — This function terminates encryption</DT
><DT
><A
HREF="function.mcrypt-enc-self-test.html"
>mcrypt_enc_self_test</A
> — This function runs a self test on the opened module</DT
><DT
><A
HREF="function.mcrypt-enc-is-block-algorithm-mode.html"
>mcrypt_enc_is_block_algorithm_mode</A
> — Checks whether the encryption of the opened mode works on blocks</DT
><DT
><A
HREF="function.mcrypt-enc-is-block-algorithm.html"
>mcrypt_enc_is_block_algorithm</A
> — Checks whether the algorithm of the opened mode is a block algorithm</DT
><DT
><A
HREF="function.mcrypt-enc-is-block-mode.html"
>mcrypt_enc_is_block_mode</A
> — Checks whether the opened mode outputs blocks</DT
><DT
><A
HREF="function.mcrypt-enc-get-block-size.html"
>mcrypt_enc_get_block_size</A
> — Returns the blocksize of the opened algorithm</DT
><DT
><A
HREF="function.mcrypt-enc-get-key-size.html"
>mcrypt_enc_get_key_size</A
> — Returns the maximum supported keysize of the opened mode</DT
><DT
><A
HREF="function.mcrypt-enc-get-supported-key-sizes.html"
>mcrypt_enc_get_supported_key_sizes</A
> — Returns an array with the supported keysizes of the opened algorithm</DT
><DT
><A
HREF="function.mcrypt-enc-get-iv-size.html"
>mcrypt_enc_get_iv_size</A
> — Returns the size of the IV of the opened algorithm</DT
><DT
><A
HREF="function.mcrypt-enc-get-algorithms-name.html"
>mcrypt_enc_get_algorithms_name</A
> — Returns the name of the opened algorithm</DT
><DT
><A
HREF="function.mcrypt-enc-get-modes-name.html"
>mcrypt_enc_get_modes_name</A
> — Returns the name of the opened mode</DT
><DT
><A
HREF="function.mcrypt-module-self-test.html"
>mcrypt_module_self_test</A
> — This function runs a self test on the specified module</DT
><DT
><A
HREF="function.mcrypt-module-is-block-algorithm-mode.html"
>mcrypt_module_is_block_algorithm_mode</A
> — This function returns if the the specified module is a block algorithm or not</DT
><DT
><A
HREF="function.mcrypt-module-is-block-algorithm.html"
>mcrypt_module_is_block_algorithm</A
> — This function checks whether the specified algorithm is a block algorithm</DT
><DT
><A
HREF="function.mcrypt-module-is-block-mode.html"
>mcrypt_module_is_block_mode</A
> — This function returns if the the specified mode outputs blocks or not</DT
><DT
><A
HREF="function.mcrypt-module-get-algo-block-size.html"
>mcrypt_module_get_algo_block_size</A
> — Returns the blocksize of the specified algorithm</DT
><DT
><A
HREF="function.mcrypt-module-get-algo-key-size.html"
>mcrypt_module_get_algo_key_size</A
> — Returns the maximum supported keysize of the opened mode</DT
><DT
><A
HREF="function.mcrypt-module-get-algo-supported-key-sizes.html"
>mcrypt_module_get_algo_supported_key_sizes</A
> — Returns an array with the supported keysizes of the opened algorithm</DT
></DL
></DIV
></DIV
></DIV
><DIV
CLASS="NAVFOOTER"
><HR
ALIGN="LEFT"
WIDTH="100%"><TABLE
WIDTH="100%"
BORDER="0"
CELLPADDING="0"
CELLSPACING="0"
><TR
><TD
WIDTH="33%"
ALIGN="left"
VALIGN="top"
><A
HREF="function.mcal-expunge.html"
>Prev</A
></TD
><TD
WIDTH="34%"
ALIGN="center"
VALIGN="top"
><A
HREF="manual.html"
>Home</A
></TD
><TD
WIDTH="33%"
ALIGN="right"
VALIGN="top"
><A
HREF="function.mcrypt-get-cipher-name.html"
>Next</A
></TD
></TR
><TR
><TD
WIDTH="33%"
ALIGN="left"
VALIGN="top"
>mcal_expunge</TD
><TD
WIDTH="34%"
ALIGN="center"
VALIGN="top"
><A
HREF="funcref.html"
>Up</A
></TD
><TD
WIDTH="33%"
ALIGN="right"
VALIGN="top"
>mcrypt_get_cipher_name</TD
></TR
></TABLE
></DIV
></BODY
></HTML
>
|