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

Python3.7 基于 pycryptodome 的AES加密解密、RSA加密解密、加簽驗(yàn)簽

 更新時(shí)間:2019年12月04日 10:47:21   投稿:mrr  
這篇文章主要介紹了Python3.7 基于 pycryptodome 的AES加密解密、RSA加密解密、加簽驗(yàn)簽,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),需要的朋友可以參考下

Python3.7 基于 pycryptodome 的AES加密解密、RSA加密解密、加簽驗(yàn)簽,具體代碼如下所示:

#!/usr/bin/env python
# -*- coding: utf8 -*-
import os
import rsa
import json
import hashlib
import base64
from Crypto.Cipher import AES
from ..settings_manager import settings
class RSAEncrypter(object):
  """RSA加密解密
  參考 https://stuvel.eu/python-rsa-doc/index.html
  對(duì)應(yīng)JavaScript版本參考 https://github.com/travist/jsencrypt
  [description]
  """
  @classmethod
  def encrypt(cls, plaintext, keydata):
    #明文編碼格式
    content = plaintext.encode('utf8')
    if os.path.isfile(keydata):
      with open(keydata) as publicfile:
        keydata = publicfile.read()
    pubkey = rsa.PublicKey.load_pkcs1_openssl_pem(keydata)
    #公鑰加密
    crypto = rsa.encrypt(content, pubkey)
    return base64.b64encode(crypto).decode('utf8')
  @classmethod
  def decrypt(cls, ciphertext, keydata):
    if os.path.isfile(keydata):
      with open(keydata) as privatefile:
        keydata = privatefile.read()
    try:
      ciphertext = base64.b64decode(ciphertext)
      privkey = rsa.PrivateKey.load_pkcs1(keydata, format='PEM')
      con = rsa.decrypt(ciphertext, privkey)
      return con.decode('utf8')
    except Exception as e:
      pass
    return False
  @classmethod
  def signing(cls, message, privkey):
    """ 簽名
      https://legrandin.github.io/pycryptodome/Doc/3.2/Crypto.Signature.pkcs1_15-module.html
    """
    from Crypto.Signature import pkcs1_15
    from Crypto.Hash import SHA256
    from Crypto.PublicKey import RSA
    if os.path.isfile(privkey):
      with open(privkey) as privatefile:
        privkey = privatefile.read()
    try:
      key = RSA.import_key(privkey)
      h = SHA256.new(message.encode('utf8'))
      sign = pkcs1_15.new(key).sign(h)
      sign = base64.b64encode(sign).decode('utf8')
      return sign
    except Exception as e:
      raise e
  @classmethod
  def verify(cls, message, sign, pubkey):
    """ 驗(yàn)證簽名
      https://legrandin.github.io/pycryptodome/Doc/3.2/Crypto.Signature.pkcs1_15-module.html
    """
    from Crypto.Signature import pkcs1_15
    from Crypto.Hash import SHA256
    from Crypto.PublicKey import RSA
    res = False
    sign = base64.b64decode(sign)
    # print('sign', type(sign), sign)
    try:
      key = RSA.importKey(pubkey)
      h = SHA256.new(message.encode('utf8'))
      pkcs1_15.new(key).verify(h, sign)
      res = True
    except (ValueError, TypeError) as e:
      raise e
      pass
    except Exception as e:
      raise e
      pass
    return res
class AESEncrypter(object):
  def __init__(self, key, iv=None):
    self.key = key.encode('utf8')
    self.iv = iv if iv else bytes(key[0:16], 'utf8')
  def _pad(self, text):
    text_length = len(text)
    padding_len = AES.block_size - int(text_length % AES.block_size)
    if padding_len == 0:
      padding_len = AES.block_size
    t2 = chr(padding_len) * padding_len
    t2 = t2.encode('utf8')
    # print('text ', type(text), text)
    # print('t2 ', type(t2), t2)
    t3 = text + t2
    return t3
  def _unpad(self, text):
    pad = ord(text[-1])
    return text[:-pad]
  def encrypt(self, raw):
    raw = raw.encode('utf8')
    raw = self._pad(raw)
    cipher = AES.new(self.key, AES.MODE_CBC, self.iv)
    encrypted = cipher.encrypt(raw)
    return base64.b64encode(encrypted).decode('utf8')
  def decrypt(self, enc):
    enc = enc.encode('utf8')
    enc = base64.b64decode(enc)
    cipher = AES.new(self.key, AES.MODE_CBC, self.iv)
    decrypted = cipher.decrypt(enc)
    return self._unpad(decrypted.decode('utf8'))
class AESSkyPay:
  """
  Tested under Python 3.7 and pycryptodome
  """
  BLOCK_SIZE = 16
  def __init__(self, key):
    #菲律賓支付通道 SkyPay Payment Specification.lending.v1.16.pdf
    # SkyPay 對(duì)密碼做了如下處理
    s1 = hashlib.sha1(bytes(key, encoding='utf-8')).digest()
    s2 = hashlib.sha1(s1).digest()
    self.key = s2[0:16]
    self.mode = AES.MODE_ECB
  def pkcs5_pad(self,s):
    """
    padding to blocksize according to PKCS #5
    calculates the number of missing chars to BLOCK_SIZE and pads with
    ord(number of missing chars)
    @see: http://www.di-mgt.com.au/cryptopad.html
    @param s: string to pad
    @type s: string
    @rtype: string
    """
    BS = self.BLOCK_SIZE
    return s + ((BS - len(s) % BS) * chr(BS - len(s) % BS)).encode('utf8')
  def pkcs5_unpad(self,s):
    """
    unpadding according to PKCS #5
    @param s: string to unpad
    @type s: string
    @rtype: string
    """
    return s[:-ord(s[len(s) - 1:])]
  # 加密函數(shù),如果text不足16位就用空格補(bǔ)足為16位,
  # 如果大于16當(dāng)時(shí)不是16的倍數(shù),那就補(bǔ)足為16的倍數(shù)。
  # 補(bǔ)足方法:PKCS5
  def encrypt(self, text):
    cryptor = AES.new(self.key, self.mode)
    # 這里密鑰key 長(zhǎng)度必須為16(AES-128),
    # 24(AES-192),或者32 (AES-256)Bytes 長(zhǎng)度
    # 目前AES-128 足夠目前使用
    ciphertext = cryptor.encrypt(self.pkcs5_pad(text.encode('utf8')))
    # 因?yàn)锳ES加密時(shí)候得到的字符串不一定是ascii字符集的,輸出到終端或者保存時(shí)候可能存在問(wèn)題
    # 所以這里將加密的字符串進(jìn)行base64編碼
    return base64.b64encode(ciphertext).decode()
  def decrypt(self, text):
    cryptor = AES.new(self.key, self.mode)
    plain_text = cryptor.decrypt(base64.b64decode(text))
    return bytes.decode(self.pkcs5_unpad(plain_text))
def aes_decrypt(ciphertext, secret=None, prefix='aes:::'):
  secret = secret if secret else settings.default_aes_secret
  cipher = AESEncrypter(secret)
  prefix_len = len(prefix)
  if ciphertext[0:prefix_len]==prefix:
    return cipher.decrypt(ciphertext[prefix_len:])
  else:
    return ciphertext
def aes_encrypt(plaintext, secret=None, prefix='aes:::'):
  secret = secret if secret else settings.default_aes_secret
  cipher = AESEncrypter(secret)
  encrypted = cipher.encrypt(plaintext)
  return '%s%s' % (prefix, encrypted)
if __name__ == "__main__":
  try:
    # for RSA test
    ciphertext = 'Qa2EU2EF4Eq4w75TnA1IUw+ir9l/nSdW3pMV+a6FkzV9bld259DxM1M4RxYkpPaVXhQFol04yFjuxzkRg12e76i6pkDM1itQSOy5hwmrud5PQvfnBf7OmHpOpS6oh6OQo72CA0LEzas+OANmRXKfn5CMN14GsmfWAn/F6j4Azhs='
    public_key = '/Users/leeyi/workspace/joywin_staff/joywin_staff_api/datas/public.pem'
    private_key = '/Users/leeyi/workspace/joywin_staff/joywin_staff_api/datas/private.pem'
    ciphertext = RSAEncrypter.encrypt('admin888中國(guó)', public_key)
    print("ciphertext: ", ciphertext)
    plaintext = RSAEncrypter.decrypt(ciphertext, private_key)
    print("plaintext: ", type(plaintext))
    print("plaintext: ", plaintext)
    # for AES test
    key = 'abc20304050607081q2w3e4r*1K|j!ta'
    cipher = AESEncrypter(key)
    plaintext = '542#1504'
    encrypted = cipher.encrypt(plaintext)
    print('Encrypted: %s' % encrypted)
    ciphertext = 'EPLtushldq9E1U8vG/sL3g=='
    assert encrypted == ciphertext
    plaintext = '542#1504你好'
    encrypted = '+YGDvnakKi77SBD6GXmThw=='
    decrypted = cipher.decrypt(encrypted)
    print('Decrypted: %s' % decrypted)
    assert decrypted == plaintext
  except KeyboardInterrupt:
    sys.exit(0)

ps:Python3 RSA加密解密加簽驗(yàn)簽示例代碼

本代碼引入Pycryptodome基于Python3.50版本編譯庫(kù)

#!/usr/bin/env python3
# coding=utf-8
# Author: Luosu201803
"""
create_rsa_key() - 創(chuàng)建RSA密鑰
my_encrypt_and_decrypt() - 測(cè)試加密解密功能
rsa_sign() & rsa_signverify() - 測(cè)試簽名與驗(yàn)簽功能
"""
from binascii import unhexlify
from Crypto.PublicKey import RSA
from Crypto.Cipher import PKCS1_OAEP, PKCS1_v1_5
import base64
from Crypto.Hash import SHA1
from Crypto.Signature import pkcs1_15
def create_rsa_key(password="123456"):
  """
  創(chuàng)建RSA密鑰,步驟說(shuō)明:
  1、從 Crypto.PublicKey 包中導(dǎo)入 RSA,創(chuàng)建一個(gè)密碼(此密碼不是RSA秘鑰對(duì))
  2、生成 1024/2048 位的 RSA 密鑰對(duì)(存儲(chǔ)在私鑰文件和公鑰文件)
  3、調(diào)用 RSA 密鑰實(shí)例的 exportKey 方法(傳入"密碼"、"使用的 PKCS 標(biāo)準(zhǔn)"、"加密方案"這三個(gè)參數(shù))得到私鑰。
  4、將私鑰寫(xiě)入磁盤(pán)的文件。
  5、使用方法鏈調(diào)用 publickey 和 exportKey 方法生成公鑰,寫(xiě)入磁盤(pán)上的文件。
  """
  key = RSA.generate(1024)
  encrypted_key = key.exportKey(passphrase=password, pkcs=8,protection="scryptAndAES128-CBC")
  # encrypted_key = key.exportKey(pkcs=1)
  print('encrypted_key:',encrypted_key)
  with open("my_private_rsa_key.pem", "wb") as f:
    f.write(encrypted_key)
  with open("my_rsa_public.pem", "wb") as f:
    f.write(key.publickey().exportKey())
def encrypt_and_decrypt_test(password="123456"):
  # 加載私鑰用于加密
  recipient_key = RSA.import_key(
    open("my_rsa_public.pem").read()
  )
  cipher_rsa = PKCS1_v1_5.new(recipient_key)
  #使用base64編碼保存數(shù)據(jù)方便查看,同樣解密需要base64解碼
  en_data = base64.b64encode(cipher_rsa.encrypt(b"123456,abcdesd"))
  print("加密數(shù)據(jù)信息:",type(en_data),'\n',len(en_data),'\n',en_data)
  # 加載公鑰用于解密
  encoded_key = open("my_private_rsa_key.pem").read()
  private_key = RSA.import_key(encoded_key,passphrase=password)
  cipher_rsa = PKCS1_v1_5.new(private_key)
  data = cipher_rsa.decrypt(base64.b64decode(en_data), None)
  print(data)
def rsa_sign(message,password="123456"):
  #讀取私鑰信息用于加簽
  private_key = RSA.importKey(open("my_private_rsa_key.pem").read(),passphrase=password)
  hash_obj = SHA1.new(message)
  # print(pkcs1_15.new(private_key).can_sign()) #check wheather object of pkcs1_15 can be signed
  #base64編碼打印可視化
  signature = base64.b64encode(pkcs1_15.new(private_key).sign(hash_obj))
  return signature
def rsa_signverify(message,signature):
  #讀取公鑰信息用于驗(yàn)簽
  public_key = RSA.importKey(open("my_rsa_public.pem").read())
  #message做“哈?!碧幚?,RSA簽名這么要求的
  hash_obj = SHA1.new(message)
  try:
    #因?yàn)楹灻籦ase64編碼,所以這里先解碼,再驗(yàn)簽
    pkcs1_15.new(public_key).verify(hash_obj,base64.b64decode(signature))
    print('The signature is valid.')
    return True
  except (ValueError,TypeError):
    print('The signature is invalid.')
if __name__ == '__main__':
  # create_rsa_key()
  encrypt_and_decrypt_test()
  # message = b'Luosu is a Middle-aged uncle.'
  # signature = rsa_sign(message)
  # print('signature:',signature)
  # print(rsa_signverify(message,signature))

總結(jié)

以上所述是小編給大家介紹的Python3.7 基于 pycryptodome 的AES加密解密、RSA加密解密、加簽驗(yàn)簽,希望對(duì)大家有所幫助,如果大家有任何疑問(wèn)請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)腳本之家網(wǎng)站的支持!
如果你覺(jué)得本文對(duì)你有幫助,歡迎轉(zhuǎn)載,煩請(qǐng)注明出處,謝謝!

相關(guān)文章

  • Python進(jìn)階語(yǔ)法之類的繼承

    Python進(jìn)階語(yǔ)法之類的繼承

    這篇文章主要為大家介紹了Python類的繼承,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下,希望能夠給你帶來(lái)幫助
    2021-12-12
  • 關(guān)于如何使用python的logging庫(kù)

    關(guān)于如何使用python的logging庫(kù)

    這篇文章主要介紹了關(guān)于如何使用python的logging庫(kù),logging是Python標(biāo)準(zhǔn)庫(kù)中用于記錄日志的模塊。它提供了一種簡(jiǎn)單但靈活的方法來(lái)記錄程序中的事件,以便稍后進(jìn)行調(diào)試和分析,需要的朋友可以參考下
    2023-04-04
  • 基于YUV 數(shù)據(jù)格式詳解及python實(shí)現(xiàn)方式

    基于YUV 數(shù)據(jù)格式詳解及python實(shí)現(xiàn)方式

    今天小編就為大家分享一篇基于YUV 數(shù)據(jù)格式詳解及python實(shí)現(xiàn)方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2019-12-12
  • Python itertools庫(kù)高效迭代藝術(shù)實(shí)例探索

    Python itertools庫(kù)高效迭代藝術(shù)實(shí)例探索

    Python 中的?itertools?庫(kù)為迭代器操作提供了豐富的工具集,使得處理迭代對(duì)象變得更加高效和靈活,本篇文章將深入討itertools庫(kù)的常用方法,通過(guò)詳實(shí)的示例代碼演示其在解決各種問(wèn)題中的應(yīng)用
    2024-01-01
  • Python文件基本操作open函數(shù)應(yīng)用與示例詳解

    Python文件基本操作open函數(shù)應(yīng)用與示例詳解

    這篇文章主要為大家介紹了Python文件基本操作open函數(shù)應(yīng)用與示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2023-12-12
  • 一文詳解Python如何處理函數(shù)調(diào)用超時(shí)問(wèn)題

    一文詳解Python如何處理函數(shù)調(diào)用超時(shí)問(wèn)題

    在Python開(kāi)發(fā)中,我們經(jīng)常會(huì)遇到需要控制函數(shù)執(zhí)行時(shí)間的場(chǎng)景,本文將深入探討Python中處理函數(shù)調(diào)用超時(shí)的幾種方法,感興趣的小伙伴可以參考一下
    2025-04-04
  • PyTorch 中的 torch.utils.data 解析(推薦)

    PyTorch 中的 torch.utils.data 解析(推薦)

    這篇文章主要介紹了PyTorch?torch.utils.data.Dataset概述案例詳解,主要介紹對(duì)?torch.utils.data.Dataset?的理解,需要的朋友可以參考下
    2023-02-02
  • 一文詳解PyCharm中如何安裝第三方庫(kù)

    一文詳解PyCharm中如何安裝第三方庫(kù)

    在下載安裝好Pycharm后,一個(gè)在實(shí)際編程開(kāi)發(fā)過(guò)程中非常重要的問(wèn)題是第三方庫(kù)添加,這篇文章主要給大家介紹了關(guān)于PyCharm中如何安裝第三方庫(kù)的相關(guān)資料,需要的朋友可以參考下
    2023-12-12
  • 基于Python繪制世界疫情地圖詳解

    基于Python繪制世界疫情地圖詳解

    這篇文章主要介紹了如何使用Python繪制世界疫情地圖,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2022-03-03
  • python 字典有序并寫(xiě)入json文件過(guò)程解析

    python 字典有序并寫(xiě)入json文件過(guò)程解析

    這篇文章主要介紹了python 字典有序并寫(xiě)入json文件過(guò)程解析,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2019-09-09

最新評(píng)論

鄂托克前旗| 汤原县| 兴业县| 桐柏县| 元朗区| 大田县| 徐州市| 恩施市| 景德镇市| 邯郸县| 宜丰县| 襄城县| 东阿县| 玛曲县| 丰原市| 临泽县| 涟源市| 怀远县| 泰兴市| 泰顺县| 宣武区| 石林| 湖南省| 峡江县| 个旧市| 白玉县| 高阳县| 清镇市| 扎鲁特旗| 嘉荫县| 沈丘县| 大邑县| 浙江省| 平乐县| 航空| 天峻县| 长岛县| 石渠县| 荥经县| 赫章县| 楚雄市|