python 常见加密算法使用

1、对称加密算法(AES、DES、3DES)
对称加密算法是指加密和解密采用相同的密钥,是可逆的(即可解密)。
AES数学原理详解

AES加密解密简例:

from Crypto.Cipher import AES
key = b'1234567812345678' #秘钥
text = b'abcdefghijklmnhi' #需要加密的内容,bytes类型
aes = AES.new(key,AES.MODE_ECB) #ECB模式
en_text = aes.encrypt(text) #加密
print("密文:",en_text)
den_text = aes.decrypt(en_text) # 解密
print("明文:",den_text)

DES加密解密简例:

import pyDes,base64
key = b'1234567812345678' #秘钥
text = b'abcdefghijklmnhi' #需要加密的内容,bytes类型
des = pyDes.des(key, pyDes.CBC, key, pad=None, padmode=pyDes.PAD_PKCS5) 
ecryptdata = des.encrypt(text) # 加密
en_text = bytes.decode(base64.b64encode(ecryptdata))
print("密文:",en_text)
data = des.decrypt(base64.b64decode(en_text)) # 解密
den_text = bytes.decode(data) 
print("明文:",den_text)

2、非对称加密算法(RSA、DSA、ECC)
非对称加密算法是指加密和解密采用不同的密钥(公钥和私钥),因此非对称加密也叫公钥加密,是可逆的(即可解密)。公钥密码体制根据其所依据的难题一般分为三类:大素数分解问题类、离散对数问题类、椭圆曲线类。

RSA 加解密简例:

#!/usr/bin/env python
# -*- coding: UTF-8 -*-
# @author: gxq
# @time : 2022/9/21 16:22
import base64
from Crypto import Random
from Crypto.PublicKey import RSA
from Crypto.Cipher import PKCS1_v1_5 as PKCS1_cipher  

random_generator = Random.new().read  # 生成随机偏移量
rsa = RSA.generate(2048, random_generator)  # 生成一个私钥
private_key = rsa.exportKey()  # 导出私钥
public_key = rsa.publickey().exportKey()  # 生成私钥所对应的公钥

def encrypt_data(msg):
    cipher = PKCS1_cipher.new(public_key)  # 生成一个加密的类
    encrypt_text = base64.b64encode(cipher.encrypt(msg.encode()))  # 对数据进行加密
    return encrypt_text.decode()  # 对文本进行解码码

def decrypt_data(encrypt_msg):
    cipher = PKCS1_cipher.new(private_key)  # 生成一个解密的类
    back_text = cipher.decrypt(base64.b64decode(encrypt_msg), 0)  # 进行解密
    return back_text.decode()  # 对文本内容进行解码

msg = "A.L.Kun"
encrypt_text = encrypt_data(msg)  # 加密
decrypt_text = decrypt_data(encrypt_text)  # 解密
print(decrypt_text, encrypt_text)

3、线性散列(HASH)算法(MD5、SHA1、HMAC)
MD5全称是Message-Digest Algorithm 5(信息摘要算法5),单向的算法不可逆(被MD5加密的数据不能被解密)。MD5加密后的数据长度要比加密数据小的多,且长度固定,且加密后的串是唯一的。
SHA-1摘要比MD5摘要长32 位,所以SHA-1对强行攻击有更大的强度,比MD5更安全。使用强行技术,产生任何一个报文使其摘要等于给定报摘要的难度对MD5是2128数量级的操作,而对SHA-1则是2160数量级的操作。

MD5加密简例:

import hashlib
 
password='abc123'
SALE = password[:4]   #取密码的前4位
md = hashlib.md5(password.encode())
print(md.hexdigest()) # 单纯的MD5加密
md_sale = hashlib.md5((password+SALE).encode())  # MD5加盐加密
# md5加盐可以将盐拼接在原密码后,也可以使用jion将盐穿插在原密码间
str(password).join(SALE)  # 将password整体插入SALE的每个元素之间。
print(str(password)+SALE)
md5salepwd = md_sale.hexdigest()
print('加密后为:',md5salepwd)

SHA1加密算法:

import hashlib
 
str = "中国你好"
a = hashlib.sha1(str.encode("utf-8")).hexdigest()
print("sha1加密前后 :",a)

4、混合加密
几种加密算法混合使用。

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Python提供了多种加密算法的库,可以根据不同的需求选择不同的算法。以下是几种常见加密算法及其Python实现: 1. MD5加密算法 Python使用hashlib库实现MD5加密算法,示例代码如下: ```python import hashlib def md5_encrypt(str): m = hashlib.md5() m.update(str.encode('utf-8')) return m.hexdigest() # 示例 print(md5_encrypt('hello world')) ``` 2. SHA加密算法 Python使用hashlib库实现SHA加密算法,示例代码如下: ```python import hashlib def sha_encrypt(str): sha = hashlib.sha256() sha.update(str.encode('utf-8')) return sha.hexdigest() # 示例 print(sha_encrypt('hello world')) ``` 3. Base64加密算法 Python使用base64库实现Base64加密算法,示例代码如下: ```python import base64 def base64_encrypt(str): return base64.b64encode(str.encode('utf-8')).decode('utf-8') # 示例 print(base64_encrypt('hello world')) ``` 4. RSA加密算法 Python使用crypto库实现RSA加密算法,示例代码如下: ```python from Crypto.PublicKey import RSA from Crypto.Cipher import PKCS1_v1_5 import base64 # 生成RSA公钥和私钥 def generate_rsa_key(): rsa = RSA.generate(2048) private_key = rsa.exportKey() public_key = rsa.publickey().exportKey() return private_key, public_key # 使用RSA公钥加密 def rsa_encrypt(str, public_key): rsa_key = RSA.importKey(public_key) cipher = PKCS1_v1_5.new(rsa_key) cipher_text = cipher.encrypt(str.encode('utf-8')) return base64.b64encode(cipher_text).decode('utf-8') # 使用RSA私钥解密 def rsa_decrypt(encrypt_str, private_key): rsa_key = RSA.importKey(private_key) cipher = PKCS1_v1_5.new(rsa_key) cipher_text = base64.b64decode(encrypt_str) return cipher.decrypt(cipher_text, None).decode('utf-8') # 示例 private_key, public_key = generate_rsa_key() encrypt_str = rsa_encrypt('hello world', public_key) decrypt_str = rsa_decrypt(encrypt_str, private_key) print(encrypt_str) print(decrypt_str) ``` 以上是部分加密算法Python实现,具体应用根据需求选择相应的算法即可。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值