In relation to all of the crypto "advice" seen here, my suggestion is that you ignore most of it. Some of it is good, some of it is bad, but most of it skips the critical issues.
I had hoped to write out a nice long explanation, but PHP's commenting system tells me my essay is too long. Instead I will say this:
You should use CBC, with a randomly chosen IV that is unique per key, and you should transmit that IV in the clear along with your ciphertext. You should also perform an authenticity check of that entire data blob, using something like HMAC-SHA256, with another independent key.
Here's the full-text of what I was going to write: http://pastebin.com/sN6buivY
If you're interested in this stuff, or just want more information, check out the Wikipedia articles around block cipher modes, block ciphers, HMAC, etc.
I also suggest reading Practical Cryptography by Bruce Schneier, as well as Cryptography Engineering by Niels Ferguson, both of which are very easy-to-digest books on practical cryptography.mcrypt_create_iv
Почист и полокален преглед на PHP референцата, со задржана структура од PHP.net и подобра читливост за примери, секции и белешки.
mcrypt_create_iv
Референца за `function.mcrypt-create-iv.php` со подобрена типографија и навигација.
mcrypt_create_iv
(PHP 4, PHP 5, PHP 7 < 7.2.0, PECL mcrypt >= 1.0.0)
mcrypt_create_iv — Создава вектор за иницијализација (IV) од случаен извор
Оваа функција беше DEPRECATED Оваа функција беше REMOVED во PHP 7.1.0, и
Алтернативи на оваа функција вклучуваат:
= NULL
Создава вектор за иницијализација (IV) од случаен извор.
IV е наменет само за да даде алтернативна семе за рутините за шифрирање. Овој IV воопшто не треба да биде таен, иако може да биде пожелно. Можете дури и да го испратите заедно со вашиот шифриран текст без да ја изгубите безбедноста.
Параметри
size- Големината на IV.
source-
Изворот на IV. Изворот може да биде
MCRYPT_RAND(системски генератор на случајни броеви),MCRYPT_DEV_RANDOM(читај податоци од /dev/random) иMCRYPT_DEV_URANDOM(читај податоци од /dev/urandom). Пред 5.3.0,MCRYPT_RANDбеше единствениот поддржан на Windows. Имајте предвид дека стандардната вредност на овој параметар бешеMCRYPT_DEV_RANDOMпред PHP 5.6.0.Забелешка: Имајте предвид дека
MCRYPT_DEV_RANDOMможе да блокира додека не биде достапна повеќе ентропија.
Вратени вредности
Враќа вектор за иницијализација, или false при грешка.
Примери
Пример #1 mcrypt_create_iv() Пример
<?php
$size = mcrypt_get_iv_size(MCRYPT_CAST_256, MCRYPT_MODE_CFB);
$iv = mcrypt_create_iv($size, MCRYPT_DEV_RANDOM);
?>Види Исто така
- » http://www.ciphersbyritter.com/GLOSSARY.HTM#IV
- » http://www.quadibloc.com/crypto/co0409.htm
- Поглавје 9.3 од Применета криптографија од Шнајер (ISBN 0-471-11709-9)
- random_bytes() - Добиј криптографски сигурни случајни бајти
Белешки од корисници 2 забелешки
>First, the IV should be random and variable. The whole >point of it is to ensure that the same plaintext does not >encrypt to the same ciphertext every time. You most >certainly do lose security if the IV is constant or public.
Wrong, Wrong WRONG! The initialization vector is ALLOWED to be PUBLIC! It is generally sent along with the ciphertext, UNENCRYPTED.
>The ciphertext should be E(IV | plaintext, key)
Wrong again! The initialization vector is NOT prepended to the plaintext before encryption. The IV is used to seed the feedback system! (which is why you don't need one in ECB mode - there is no feedback)
>Second, the IV should not be part of the decryption >parameters at all. You should be able to decrypt the cipher >text, throw away the initial vector at the front w/o even >reading it, and have your plaintext:
Nope. You need to seed the feedback mechanism during decryption to the SAME state as it was seeded during encryption. This means using the SAME IV!