Current location - Education and Training Encyclopedia - Graduation thesis - How does php get different ciphertext after encrypting the same plaintext? And these ciphertexts are all the same plaintext after decryption!
How does php get different ciphertext after encrypting the same plaintext? And these ciphertexts are all the same plaintext after decryption!
Then you can only use different keys to obtain different ciphertext when encrypting, and you have to use the previous key when decrypting, so if you want to store ciphertext, the key should also be stored, otherwise you can't decrypt it!

Look at the code I wrote. Encryption and decryption functions can be found on the Internet. When refreshing the page, there will be a chance of garbled code! You can change it yourself, but it must be reversible.

& lt? Server-side programming language (abbreviation of professional hypertext preprocessor)

Ming? =? People's Republic of China (PRC)';

$iv? =? Rand (1000,? 9999); //key

$mi? =? Encryption ($iv,? $ Ming);

$ Jie Mi? =? Decryption ($iv,? $ mi);

Echo? Original:'. Ming. ”& ltbr/>; ;

Echo? Encryption:'. $ mi。

Echo? Decryption: "". $ Jie Mi;

//? Encryption function

Function? Encryption ($ key,? $plain_text)? {

$plain_text? =? trim($ plain _ text);

$iv? =? substr(md5($key),? 0,mcrypt_get_iv_size? (MCRYPT_CAST_256,MCRYPT _ MODE _ CFB));

$c_t? =? mcrypt_cfb? (MCRYPT_CAST_256,? $key,? $plain_text,? MCRYPT_ENCRYPT,? $ iv);

Return? trim(chop(base64 _ encode($ c _ t)));

}

//? Decryption function

Function? Decrypt ($key,? $c_t)? {

$c_t? =? trim(chop(base64 _ decode($ c _ t)));

$iv? =? substr(md5($key),? 0,mcrypt_get_iv_size? (MCRYPT_CAST_256,MCRYPT _ MODE _ CFB));

$p_t? =? mcrypt_cfb? (MCRYPT_CAST_256,? $key,? $c_t,? MCRYPT_DECRYPT,? $ iv);

Return? trim(chop($ p _ t));

}

& gt reference: /springload/detail/67