Python AES Decryption script code

Source: Internet
Author: 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 # encryption operation
Decrypt_op = 0 # decryption operation
#print Crypto.Random.OSRNG.posix.new (). Read (Aes.block_size)
IV = ' 16 # initialization variable, useless for AES_128_ECB algorithm
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 ' # key

def Encrypt (data):
' Encrypt data using the AES_128_ECB algorithm '
cipher = cipher (ALG = ' AES_128_CBC ', key = Private_key, IV = IV, OP = encrypt_op)
BUF = cipher.update (data)
BUF = buf + cipher.final ()
del cipher
# transfer plaintext from byte to 16
Output = '
For I in BUF:
Output + + '%02x '% (ord (i))
Return output

def Decrypt (data):
' Decrypt data using the AES_128_ECB algorithm '
# Convert ciphertext from 16 to byte stream
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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.