python3.x实现base64加密和解密

yipeiwu_com6年前Python基础

用python3.x实现base64加密和解密,供大家参考,具体内容如下

加密

base64_encrypt.py

#!/usr/bin/python3
#encoding:utf-8
import base64
var = 1
while var==1:
  str_encrypt=input("输入要加密的字符串:\n"); 
  base64_encrypt = base64.b64encode(str_encrypt.encode('utf-8'))
  print("BASE64加密串:\n"+str(base64_encrypt,'utf-8'))
  print("按ctrl+c退出程序")

打开windows命令窗口>把加密代码文件拖入黑窗口>回车>输入要加密的字符串>回车完成加密

解密

base64_decrypt.py

#!/usr/bin/python3
#encoding:utf-8
import base64
import logging
import sys
'''
方式一:简单的logger配置
log_file = "E:\pythonwork/basic_logger.log"
logging.basicConfig(filename = log_file, level = logging.INFO)
'''

#方式二
#用base64_decrypt创建日志记录器
logger = logging.getLogger('base64_decrypt')
logger.setLevel(logging.DEBUG)
#创建甚至记录调试消息的文件处理程序
fh = logging.FileHandler("E:\pythonwork/basic_logger.log")
fh.setLevel(logging.DEBUG)
#创建具有较高日志级别的控制台处理程序
ch = logging.StreamHandler()
ch.setLevel(logging.ERROR)
#创建格式化程序并将其添加到处理程序中
formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
fh.setFormatter(formatter)
ch.setFormatter(formatter)
#将处理程序添加到记录器
logger.addHandler(fh)
logger.addHandler(ch)


while True :
  global base64_decrypt
  try:
    str_decrypt=input("输入BASE64加密串:\n")
    base64_decrypt = base64.b64decode(str_decrypt.encode('utf-8'))
    print("BASE64解密串(UTF-8):\n",str(base64_decrypt,'utf-8'))
    logger.info("BASE64解密串:\n"+str(base64_decrypt,'utf-8'))
  except Exception as e:
    print ("BASE64解密串(UTF-8)异常:", e)
    print("BASE64解密串(默认字符集):\n",str(base64_decrypt))
    base64_decrypt=""
    logger.info("e:"+ str(e))
  finally: 
    print("按ctrl+c退出程序")

打开windows命令窗口>把解密代码文件拖入黑窗口>回车>输入要解密的字符串>回车完成解密

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持【听图阁-专注于Python设计】。

相关文章

Python实现获取某天是某个月中的第几周

找了半天竟然没找到,如何在Python的datetime处理上,获取某年某月某日,是属于这个月的第几周。 无奈之下求助同学,同学给写了一个模块。【如果你知道Python有这个原生的库,请...

闭包在python中的应用之translate和maketrans用法详解

相对来说python对字符串的处理是比较高效的,方法也有很多。其中maketrans和translate两个方法被应用的很多,本文就针对这两个方法的用法做一总结整理。 首先让我们先回顾下...

python的文件操作方法汇总

文件的读操作 示例: print("->文件句柄的获取,读操作:") f = open('无题','r',encoding='utf8') d = f.read()...

django admin添加数据自动记录user到表中的实现方法

1.需求:在后台添加一条数据的同时要把添加者记录到表中。 2.models.py class Setting(models.Model): ... user =...

深入分析在Python模块顶层运行的代码引起的一个Bug

然后我们在Interactive Python prompt中测试了一下: >>> import subprocess >>> subproc...