Python uses MD5 to encrypt the string, for example, pythonmd5

Source: Internet
Author: User

Python uses MD5 to encrypt the string, for example, pythonmd5

There are several Python encryption modules, But no matter which encryption method you use, you need to first import the corresponding encryption module and then use the module to encrypt the string.

Import the required md5 encryption module first:
Copy codeThe Code is as follows:
Import hashlib

Create an md5 object
Copy codeThe Code is as follows:
M = hashlib. md5 ()

Generate an encrypted string. The password is the string to be encrypted.
Copy codeThe Code is as follows:
M. update ('Password ')

Get encrypted string
Copy codeThe Code is as follows:
Psw = m. hexdigest ()

Output
Copy codeThe Code is as follows:
Print psw

Run:
Copy codeThe Code is as follows:
5f4dcc3b5aa765d61d8327deb882cf99

For convenience, we can write a function and directly input the string to be encrypted for calling.
Copy codeThe Code is as follows:
Def md5 (str ):
Import hashlib
M = hashlib. md5 ()
M. update (str)
Return m. hexdigest ()

Call:
Copy codeThe Code is as follows:
Str = md5 ('Password ')

If the input parameter is not a string, an error is returned.
Copy codeThe Code is as follows:
Str = md5 (['A', 'B'])

Error:
Copy codeThe Code is as follows:
Traceback (most recent call last ):
File "D: \ python \ demo1 \ c. py", line 9, in <module>
Str = md5 (['A', 'B'])
File "D: \ python \ demo1 \ c. py", line 5, in md5
M. update (str)
TypeError: must be string or buffer, not list

We can detect incoming types to avoid errors.
Copy codeThe Code is as follows:
Def md5 (str ):
Import hashlib
Import types
If type (str) is types. StringType:
M = hashlib. md5 ()
M. update (str)
Return m. hexdigest ()
Else:
Return''

If the input parameter is a string, the encrypted string is returned correctly. If the input parameter is of another type, null is returned!


How to encrypt a string with md5? C # implementation

1 public String md5 (String s)
2 {
3 System. Security. Cryptography. MD5 md5 = new System. Security. Cryptography. MD5CryptoServiceProvider ();
4 byte [] bytes = System. Text. Encoding. UTF8.GetBytes (s );
5 bytes = md5.ComputeHash (bytes );
6 md5.Clear ();
7
8 string ret = "";
9 for (int I = 0; I <bytes. Length; I ++)
10 {
11 ret + = Convert. ToString (bytes [I], 16). PadLeft (2, '0 ');
12}
13
14 return ret. PadLeft (32, '0 ');
15}

This function is passed as a string and returns the md5 string.

How to parse the MD5 encrypted string

MD5 is an irreversible encryption method in the program. If you need to use a tool for decryption, it may take several hours or days to decrypt the data.

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.