Achieve MD5, sha256, sha384, sha512, base64 encryption in python


Upon completion of the mall recent cases of job encountered you submit a registration password is a string of numbers and letters, do not feel safe, then study up encryption.
The encryption algorithm, and the corresponding ciphertext
Many encryption algorithms, simply learning a few common (MD5, sha256, base64).

A, hashlib Introduction

Python's hashlib provides a common digest algorithm, such as MD5, SHA1, and so on.

Digest algorithm, also known as hash algorithm, hash algorithm. It is through a function to convert data for any length of a fixed length data string (typically represented by a string of 16 hexadecimal).

In hashlib python3 in the standard library can call SHA1, SHA224, SHA256, SHA384, SHA512, and MD5 algorithms.

Second, the use hashlib library md5 encryption

1, obfuscated code is not encrypted

import hashlib

def jm_md5(password):
    m = hashlib.md5()  # 构建MD5对象
    m.update(password.encode(encoding='utf-8')) #设置编码格式 并将字符串添加到MD5对象中
    password_md5 = m.hexdigest()  # hexdigest()将加密字符串 生成十六进制数据字符串值
    return password, password_md5

g = jm_md5('123456')
print(g)

Results :( '123456', 'e10adc3949ba59abbe56e057f20f883e')

2, encrypted code confuse

We know that update can increase the value to the collection (dictionary) or key-value pairs.
In MD5 encryption, it can play the role of string concatenation, which is able to reach confuse the role of encryption.

import hashlib
def hxjm_md5(password,value):
    '''
    :param password: 加密字符串
    :param value: 混淆字符串
    :return: 将混淆后的加密结果转成16进制字符串形式返回
    '''

    m = hashlib.md5(password.encode("utf-8"))
    m.update(value.encode('utf-8'))
    pwd_md5 = m.hexdigest()
    return password,value,pwd_md5

j = hxjm_md5('123456','wuhan')
print(j)

Results: ( '123456', 'Wuhan', '8ebbaf4714f033d20a0fef5f25ed63de')

Common encryption method: add the current time stamp string to generate a unique MD5 value and returns

import time
import hashlib

def hxjm_md5(password):

    value = str(time.time()) # 获取当前时间戳,转化为字符串并赋值给value
    print("当前时间::" + time.strftime("%H:%M:%S", time.localtime(time.time())))
    m = hashlib.md5(password.encode('utf-8'))
    m.update(value.encode('utf-8'))
    pwd_md5 = m.hexdigest()
    return password,value,pwd_md5

g = hxjm_md5('123456')
print(g)

Results :( '123456', '1581344237.6187823', '4612dadda102056fcdbce06799b70940')

1,581,344,237.6187823 timestamp may time.localtime (time.time ()) function conversion.

Third, the use hashlib library sha256, sha384, sha512 encryption

sha256 and sha512 use basically the same in python, different digest algorithm just returned code only demonstrates sha256.

def jm_sha256(key, value='wuhan'):
    '''
    :param key: 明文密码
    :param value: 混淆字符串
    :return:混淆后的加密结果
    '''
    hsobj = hashlib.sha256(key.encode("utf-8"))
    hsobj.update(value.encode("utf-8"))
    return hsobj.hexdigest()
    
print(jm_sha256('123456'))

结果:6fecd7d60a883872ab0b74656f9de08b3b7b396e0a52846069bf8610982e6990

四、多重加密(加盐加密)

一次MD5或者sha256很容易被人反编译或者通过穷举法给编译出来,这个时候就需要通过加盐加密、多重加密来保障密码安全。
加盐加密:就是在字符串中加入固定的或者随机的字符串,将拼接后的结果进行加密。
多重加密:就是对一个字符串进行多个算法混合加密,如:sha256(md5($pass))等

'''
  密码加密
 version:01
 author:jasn
 Date:2020-02-10
'''
import hashlib,random,string,time


def jm_sha256(key):
    '''
    :param key: 加密字符串
    :return:返回混淆后加密密问
    '''
    # value 10位数的随机盐
    value = ''.join(random.sample(string.ascii_letters +str(time.time()),10))
    hsobj = hashlib.sha256(key.encode("utf-8"))
    hsobj.update(value.encode("utf-8"))
    return hsobj.hexdigest()


def jm_md5(key, value='china'):
    '''
    :param key: 加密字符串
    :param value: 固定的盐,用来混淆
    :return: 返回混淆后加密密问
    '''
    hsobj = hashlib.md5(key.encode("utf-8"))
    hsobj.update(value.encode("utf-8"))
    return hsobj.hexdigest()


if __name__ == '__main__':
    g = jm_md5(jm_md5((jm_sha256('123456'))))
    print(g)

加盐多重混淆后的加密结果:200b86a5b31c12c2801d0109562048e4

五、base64加密

差一点忘记了,想对比其他几种加密方式,base64起不到加密的作用,但是为了学习还是写一写。

Base64编码之所以称为Base64,是因为其使用64个字符来对任意数据进行编码,同理有Base32、Base16编码。标准Base64编码使用的64个字符为:
Here Insert Picture Description
Base64编码本质上是一种将二进制数据转成文本数据的方案。对于非二进制数据,是先将其转换成二进制形式,然后每连续6比特(2的6次方=64)计算其十进制值,根据该值在上面的索引表中找到对应的字符,最终得到一个文本字符串。
Here Insert Picture Description
可知 Hello! 的Base64编码结果为== SGVsbG8h ==,原始字符串长度为6个字符,编码后长度为8个字符,每3个原始字符经Base64编码成4个字符,编码前后长度比4/3,这个长度比很重要 - 比原始字符串长度短,则需要使用更大的编码字符集,这并不我们想要的;长度比越大,则需要传输越多的字符,传输时间越长。Base64应用广泛的原因是在字符集大小与长度比之间取得一个较好的平衡,适用于各种场景。

上代码敲一下

import base64

a = "this is a test"
bs64 = base64.b64encode(a.encode('utf-8')) # 对字符串编码
print(bs64)
'''
结果:
>>>: b'dGhpcyBpcyBhIHRlc3Q='
'''
debs64 = base64.b64decode(bs64)   # 对base64编码进行解码
print(debs64)

'''
结果:
>>>: b'this is a test'
'''
Published 46 original articles · won praise 37 · views 4528

Guess you like

Origin blog.csdn.net/weixin_42444693/article/details/104255330