怎么在python中使用des、aes, rsa算法进行加解密

  介绍

这篇文章将为大家详细讲解有关怎么在python中使用des、aes, rsa算法进行加解密,文章内容质量较高,因此小编分享给大家做个参考,希望大家阅读完这篇文章后对相关知识有一定的了解。

aes加解密

aes只是个基本算法,实现aes有几种模式,主要有央行,CBC,循环流化床和OFB CTR,直接上代码,此处为AES加密中的CBC模式,EBC模式与CBC模式相比,不需要iv。

import  base64from  Crypto.Cipher  import  AES   得到binascii  import  b2a_hex a2b_hex   ,   ,   时间=unpad  lambda 年代:,年代[:奥德(s (len (s),安康;1:]))   class  AES3:   def 才能__init__(自我,,键):   ,,,self.key =,关键   ,,,self.mode =AES.MODE_CBC   ,,,self.iv =self.key      def 才能_pad(自我,文本):   ,,,key_len =, len (self.key)   ,,,pad =, text  +, (key_len 安康;len(文本),%,key_len), *,空空(key_len 安康;len(文本),%,key_len)   ,,,return 垫      def 才能_unpad(自我,文本):   ,,,pad =,奥德(文本(1:))   ,,,return 文本(0:垫)      #,才能加密函数   def 才能;加密(自我,文本):   ,,,length =16   ,,,count =, len(文本)   ,,,if  count  %, length  !=, 0:   ,,,,,add =, length 作用;(count  %,长度)   ,,,:   ,,,,,add =0   ,,,text =, text  +,(& # 39; \ 0 & # 39;, *,添加)   ,,,cryptor =, AES.new (self.key.encode (“utf8"), self.mode,, self.iv.encode (“utf8"))   ,,,self.ciphertext =, cryptor.encrypt(字节(文本,编码=皍tf8"))   ,,,#,AES加密时候得到的字符串不一定是ascii字符集的,输出到终端或者保存时候可能存在问题,使用base64编码   ,,,return  base64.b64encode (b2a_hex (self.ciphertext)) .decode (& # 39; utf - 8 # 39;)      #,才能解密函数   def 才能解密(自我,文本):   ,,,decode =, base64.b64decode(文本)   ,,,cryptor =, AES.new (self.key.encode (“utf8"), self.mode,, self.iv.encode (“utf8"))   ,,,plain_text =, unpad (cryptor.decrypt(解码))   ,,,return  a2b_hex (plain_text), .decode (& # 39; use utf8 # 39;)

RSA公钥加密,私钥解密

得到Crypto.PublicKey  import  RSA   得到Crypto.Cipher  import  PKCS1_v1_5  as  Cipher_pkcs1_v1_5   得到Crypto.Signature  import  PKCS1_v1_5  as  Signature_pkcs1_v1_5   import  base64      #,私钥   时间=private_key  & # 39; & # 39; & # 39;——-BEGIN  RSA  PRIVATE 关键- - - - - -   5353年dfggd   - - - - -最终获得;RSA  PRIVATE 关键- - - - - -   & # 39;& # 39;& # 39;      #,公钥   时间=public_key  & # 39; & # 39; & # 39;——-BEGIN  PUBLIC 关键- - - - - -   hfgghftetet   - - - - -最终获得;PUBLIC 关键- - - - - - - - - - - # 39;& # 39;& # 39;   def  rsa_encrypt(信息):   “““才能校验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(文本):   “““才能校验RSA加密,使用私钥进行解密“““   时间=cipher 才能;Cipher_pkcs1_v1_5.new (RSA.importKey (private_key))   retval 才能=,cipher.decrypt (base64.b64decode(文本),& # 39;错误# 39;).decode (& # 39; utf - 8 # 39;)   return 才能retval

DES加解密

得到pyDes  import  *   import  base64   class  Des3(对象):   def 才能;__init__(自我,,钥匙,,(四):   ,,,#,这里密钥密钥长度必须为16/24,,,偏移量静脉注射   ,,,self.key =,关键   ,,,self.mode =CBC   ,,,self.iv =4      #,才能加密函数,如果文本不是16的倍数【加密文本文字必须为16的倍数!】,那就补足为16的倍数   def 才能;加密(自我,文本):   ,,,des3 =, triple_des (self.mode, self.key,还以为,self.iv,垫=没有,padmode=PAD_PKCS5)   ,,,data =, des3.encrypt(文本)   ,,,data =, base64.b64encode(数据)   ,,,return  data.decode (& # 39; utf - 8 # 39;)      #,才能解密后,去掉补足的空格用带(),去掉   def 才能解密(自我,,数据):   ,,,des3 =, triple_des (self.mode, self.key,还以为,self.iv,垫=没有,padmode=PAD_PKCS5)   ,,,data =, base64.b64decode(数据)   ,,,text =, des3.decrypt(数据)   null

怎么在python中使用des、aes, rsa算法进行加解密