怎么在python中使用des、aes、rsa算法進行加解密-創(chuàng)新互聯(lián)

這篇文章將為大家詳細講解有關怎么在python中使用des、aes、rsa算法進行加解密,文章內容質量較高,因此小編分享給大家做個參考,希望大家閱讀完這篇文章后對相關知識有一定的了解。

成都創(chuàng)新互聯(lián)公司從2013年成立,先為黔江等服務建站,黔江等地企業(yè),進行企業(yè)商務咨詢服務。為黔江企業(yè)網站制作PC+手機+微官網三網同步一站式服務解決您的所有建站問題。

AES加解密

AES 只是個基本算法,實現(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加密時候得到的字符串不一定是ascii字符集的,輸出到終端或者保存時候可能存在問題,使用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):
  """校驗RSA加密 使用公鑰進行加密"""
  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):
  """校驗RSA加密 使用私鑰進行解密"""
  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長度必須為16/24, ,偏移量ivs
    self.key = key
    self.mode = CBC
    self.iv = iv

  # 加密函數(shù),如果text不是16的倍數(shù)【加密文本text必須為16的倍數(shù)!】,那就補足為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')

  # 解密后,去掉補足的空格用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算法進行加解密就分享到這里了,希望以上內容可以對大家有一定的幫助,可以學到更多知識。如果覺得文章不錯,可以把它分享出去讓更多的人看到。

網頁標題:怎么在python中使用des、aes、rsa算法進行加解密-創(chuàng)新互聯(lián)
瀏覽地址:http://www.bm7419.com/article46/cdjihg.html

成都網站建設公司_創(chuàng)新互聯(lián),為您提供企業(yè)建站、自適應網站、做網站品牌網站建設、微信小程序、網站排名

廣告

聲明:本網站發(fā)布的內容(圖片、視頻和文字)以用戶投稿、用戶轉載內容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內容未經允許不得轉載,或轉載時需注明來源: 創(chuàng)新互聯(lián)

成都seo排名網站優(yōu)化