Python AES解密指令碼代碼

來源:互聯網
上載者:User


# coding=utf-8
from M2Crypto.EVP import Cipher
from M2Crypto import m2 
from M2Crypto import util 
import urllib
import sys
import base64
import binascii
import Crypto
import Crypto.Random
import array
from Crypto.Cipher import AES 
ENCRYPT_OP = 1 # 加密操作 
DECRYPT_OP = 0 # 解密操作 
#print Crypto.Random.OSRNG.posix.new().read(AES.block_size)
iv = '\0' * 16 # 初始設定變數,對於aes_128_ecb演算法無用 
iv_arr=b'\x01\x01\x0b\x05\x04\x0f\x07\x09\x17\x03\x01\x06\x08\x0c\x0d\x5b'
iv=iv_arr
PRIVATE_KEY = 'mymiyao' # 密鑰
   
def Encrypt(data): 
  '使用aes_128_ecb演算法對資料加密' 
  cipher = Cipher(alg = 'aes_128_cbc', key = PRIVATE_KEY, iv = iv, op = ENCRYPT_OP) 
  buf = cipher.update(data) 
  buf = buf + cipher.final() 
  del cipher 
  # 將明文從位元組流轉為16進位 
  output = '' 
  for i in buf: 
    output += '%02X' % (ord(i)) 
  return output 
   
def Decrypt(data): 
  '使用aes_128_ecb演算法對資料解密' 
  # 將密文從16進位轉為位元組流 
  data = util.h2b(data) 
  cipher = Cipher(alg = 'aes_128_cbc', key = PRIVATE_KEY, iv = iv, op = DECRYPT_OP) 
  buf = cipher.update(data) 
  buf = buf + cipher.final() 
  del cipher 
  return buf
data = sys.argv[1]
data = urllib.unquote(data)
data = base64.decodestring(data)
data = data.encode('hex')
print Decrypt(data)
 
#encrypt_data= Encrypt(data)
#encrypt_data=data.decode('hex')
#encrypt_data=base64.encodestring(encrypt_data)
#print encrypt_data

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.