python linux脚本加密解密,python/shell字符串加解密

#!/usr/bin/env python

#coding: utf-8

'''

+-----------------------------------------------------------------------+

|Author: Cheng Wenfeng <277546922@qq.com>                               |

+-----------------------------------------------------------------------+

'''

import sys

import base64

import getopt

from Crypto.Cipher import AES

if len(sys.argv) < 2:

print 'Syntax: %s handle[encode|decode] str \n \

eg: %s  encode  277546922@qq.com' % (sys.argv[0],sys.argv[0])

sys.exit()

opts,args=getopt.getopt(sys.argv[1:], "h", ['help'])

for opt,arg in  opts:

if opt == '--help' or opt == '-h'      :

print 'Syntax: %s handle[encode|decode] str \n \

eg: %s  encode  277546922@qq.com' % (sys.argv[0],sys.argv[0])

sys.exit()

Formatkey = '\0'

FormatStr = lambda s: s+(16 - len(s)%16)*Formatkey

key='qq277546922qqqqq'

data=str(sys.argv[2])

def encode(key,data):

keyIV=key

obj = AES.new(key, AES.MODE_CBC,keyIV)

ciphertext = base64.b64encode(obj.encrypt(FormatStr(data)))

print ciphertext

def decode(key,data):

keyIV=key

obj2 = AES.new(key, AES.MODE_CBC,keyIV)

try:

newmesg = obj2.decrypt(base64.b64decode(data))

print newmesg.replace('\0','')

except  TypeError:

print 'Decode Fail'

if sys.argv[1]=='encode':

encode(key,data)

elif sys.argv[1]=='decode':

decode(key,data)

else :

print 'handle error'

sys.exit()

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值