最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

python des,aes,rsa加解密的實(shí)現(xiàn)

 更新時(shí)間:2021年01月16日 15:30:51   作者:**綿綿羊**  
這篇文章主要介紹了python des,aes,rsa加解密的實(shí)現(xiàn),幫助大家更好的理解和使用python,感興趣的朋友可以了解下

AES加解密

AES 只是個(gè)基本算法,實(shí)現(xiàn) AES 有幾種模式,主要有 ECB、CBC、CFB 和 OFB  CTR,直接上代碼,此處為AES加密中的CBC模式,EBC模式與CBC模式相比,不需要iv。

import base64from Crypto.Cipher import AES
from binascii import b2a_hex, a2b_hex
 
 
unpad = lambda s: s[:-ord(s[len(s) - 1:])]
class AES3:
  def __init__(self, key):
    self.key = key
    self.mode = AES.MODE_CBC
    self.iv = self.key

  def _pad(self, text):
    key_len = len(self.key)
    pad = text + (key_len - len(text) % key_len) * chr(key_len - len(text) % key_len)
    return pad

  def _unpad(self, text):
    pad = ord(text[-1:])
    return text[0:-pad]

  # 加密函數(shù)
  def encrypt(self, text):
    length = 16
    count = len(text)
    if count % length != 0:
      add = length - (count % length)
    else:
      add = 0
    text = text + ('\0' * add)
    cryptor = AES.new(self.key.encode("utf8"), self.mode, self.iv.encode("utf8"))
    self.ciphertext = cryptor.encrypt(bytes(text, encoding="utf8"))
    # AES加密時(shí)候得到的字符串不一定是ascii字符集的,輸出到終端或者保存時(shí)候可能存在問題,使用base64編碼
    return base64.b64encode(b2a_hex(self.ciphertext)).decode('utf-8')

  # 解密函數(shù)
  def decrypt(self, text):
    decode = base64.b64decode(text)
    cryptor = AES.new(self.key.encode("utf8"), self.mode, self.iv.encode("utf8"))
    plain_text = unpad(cryptor.decrypt(decode))
    return a2b_hex(plain_text) .decode('utf8')

RSA公鑰加密,私鑰解密

from Crypto.PublicKey import RSA
from Crypto.Cipher import PKCS1_v1_5 as Cipher_pkcs1_v1_5
from Crypto.Signature import PKCS1_v1_5 as Signature_pkcs1_v1_5
import base64

# 私鑰
private_key = '''-----BEGIN RSA PRIVATE KEY-----
5353dfggd
-----END RSA PRIVATE KEY-----
'''

# 公鑰
public_key = '''-----BEGIN PUBLIC KEY-----
hfgghftetet
-----END PUBLIC KEY-----'''
def rsa_encrypt(message):
  """校驗(yàn)RSA加密 使用公鑰進(jìn)行加密"""
  cipher = Cipher_pkcs1_v1_5.new(RSA.importKey(public_key))
  cipher_text = base64.b64encode(cipher.encrypt(message.encode())).decode()
  return cipher_text


def rsa_decrypt(text):
  """校驗(yàn)RSA加密 使用私鑰進(jìn)行解密"""
  cipher = Cipher_pkcs1_v1_5.new(RSA.importKey(private_key))
  retval = cipher.decrypt(base64.b64decode(text), 'ERROR').decode('utf-8')
  return retval

DES加解密

from pyDes import *
import base64
class Des3(object):
  def __init__(self, key, iv):
    # 這里密鑰key長(zhǎng)度必須為16/24, ,偏移量ivs
    self.key = key
    self.mode = CBC
    self.iv = iv

  # 加密函數(shù),如果text不是16的倍數(shù)【加密文本text必須為16的倍數(shù)!】,那就補(bǔ)足為16的倍數(shù)
  def encrypt(self, text):
    des3 = triple_des(self.key, self.mode, self.iv, pad=None, padmode=PAD_PKCS5)
    data = des3.encrypt(text)
    data = base64.b64encode(data)
    return data.decode('utf-8')

  # 解密后,去掉補(bǔ)足的空格用strip() 去掉
  def decrypt(self, data):
    des3 = triple_des(self.key, self.mode, self.iv, pad=None, padmode=PAD_PKCS5)
    data = base64.b64decode(data)
    text = des3.decrypt(data)
    return text.decode('hex')

以上就是python des,aes,rsa加解密的實(shí)現(xiàn)的詳細(xì)內(nèi)容,更多關(guān)于python des,aes,rsa加解密的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!

相關(guān)文章

最新評(píng)論

石泉县| 尉氏县| 三门峡市| 珲春市| 股票| 新郑市| 西安市| 台州市| 莎车县| 政和县| 澄城县| 农安县| 英山县| 八宿县| 客服| 岳池县| 犍为县| 台南县| 湖南省| 临猗县| 鄂伦春自治旗| 苏州市| 永春县| 宽甸| 盖州市| 绍兴县| 莒南县| 金山区| 寿阳县| 琼结县| 茌平县| 五寨县| 舟曲县| 德钦县| 若尔盖县| 汕头市| 彭州市| 兰溪市| 侯马市| 什邡市| 唐海县|