PHP crypt in Python

Source: Internet
Author: User
Tags crypt

I have not updated my blog for more than half a year, and I am very sorry for my comments and questions on my blog. I thought I was about to leave the IT industry. I have been working on outsourcing for nearly two years. Every day, I am catching up with the progress and feeling exhausted, I began to doubt whether I had enough enthusiasm for the industry and whether I had enough talent. But I know that I still like challenges. Every programmer should feel comfortable with the joy of solving problems. I am not sure how long I will continue on this road, but now, it will be good to go when I am willing to go.

Let's start with the question. If you have a problem today, record it.

There are a batch of user names and passwords in hand. The passwords are encrypted using the crypt () method in PHP. Now, I want to verify the password in the python environment to see if the password submitted by the user matches the existing encrypted password. What should I do? First, you must know how to encrypt and verify the password in PHP.

Encryption: $ Pwd = crypt ($ password) Verification: If (crypt ($ password, $ PWD) = $ PWD)

In PHP, the encryption algorithm (des or MD5) used by the crypt method varies depending on the operating system environment. There are also crypt methods in Python. I use this method in the same way, but it does not work. In the existing system, do we use des, MD5, or others? I did not go into this judgment method. On the Internet, I can see that the encryption results of different encryption algorithms are in different formats, as shown below:

<?phpif (CRYPT_STD_DES == 1) {    echo 'Standard DES: ' . crypt('rasmuslerdorf', 'rl') . "\n";}if (CRYPT_EXT_DES == 1) {    echo 'Extended DES: ' . crypt('rasmuslerdorf', '_J9..rasm') . "\n";}if (CRYPT_MD5 == 1) {    echo 'MD5:          ' . crypt('rasmuslerdorf', '$1$rasmusle$') . "\n";}if (CRYPT_BLOWFISH == 1) {    echo 'Blowfish:     ' . crypt('rasmuslerdorf', '$2a$07$rasmuslerd...........$') . "\n";}?>

My password is in the format of $1 $ 7f... c32. $ fld0lxck. ea3nheukctip0. I decided based on the format, and now the system uses the MD5 algorithm.

The keyword of the problem becomes Python crypt MD5. I found the following method:

from passlib.hash import md5_cryptverifyed = md5_crypt.verify(passwd, cryptedpwd)

Passwd is the plaintext password submitted by the user, and cryptedpwd is the encrypted password in PHP. If the verification succeeds, verifyed will be true.

To use the passlib. Hash module, install passlib first. I usually use easy_install to install: easy_install pass lib

OK.

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: info-contact@alibabacloud.com 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.