小木木 发表于 2018-8-27 10:07:57

python/shell字符串加解密

  #!/usr/bin/env python
  #coding: utf-8
  '''
  +-----------------------------------------------------------------------+
  |Author: Cheng Wenfeng                              |
  +-----------------------------------------------------------------------+
  '''
  import sys
  import base64
  import getopt
  from Crypto.Cipher import AES
  if len(sys.argv) < 2:
  print 'Syntax: %s handle str \n \
  eg: %sencode277546922@qq.com' % (sys.argv,sys.argv)
  sys.exit()
  opts,args=getopt.getopt(sys.argv, "h", ['help'])
  for opt,arg inopts:
  if opt == '--help' or opt == '-h'      :
  print 'Syntax: %s handle str \n \
  eg: %sencode277546922@qq.com' % (sys.argv,sys.argv)
  sys.exit()
  Formatkey = '\0'
  FormatStr = lambda s: s+(16 - len(s)%16)*Formatkey
  key='qq277546922qqqqq'
  data=str(sys.argv)
  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','')
  exceptTypeError:
  print 'Decode Fail'
  if sys.argv=='encode':
  encode(key,data)
  elif sys.argv=='decode':
  decode(key,data)
  else :
  print 'handle error'
  sys.exit()

页: [1]
查看完整版本: python/shell字符串加解密