python 异或加密字符串的实例

yipeiwu_com5年前Python基础

做个简单习题:输入明文给定秘钥,密文还原,按位异或处理。

import base64 as b64

def xor_encrypt(tips,key):
 ltips=len(tips)
 lkey=len(key)
 secret=[]
 num=0
 for each in tips:
 if num>=lkey:
  num=num%lkey
 secret.append( chr( ord(each)^ord(key[num]) ) )
 num+=1

 return b64.b64encode( "".join( secret ).encode() ).decode()


def xor_decrypt(secret,key):

 tips = b64.b64decode( secret.encode() ).decode()

 ltips=len(tips)
 lkey=len(key)
 secret=[]
 num=0
 for each in tips:
 if num>=lkey:
  num=num%lkey

 secret.append( chr( ord(each)^ord(key[num]) ) )
 num+=1

 return "".join( secret )


tips= "1234567"
key= "owen"
secret = xor_encrypt(tips,key)
print( "cipher_text:", secret )

plaintxt = xor_decrypt( secret, key )
print( "plain_text:",plaintxt )

以上这篇python 异或加密字符串的实例就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持【听图阁-专注于Python设计】。

相关文章

Python中几种操作字符串的方法的介绍

#! -*- coding:utf-8 -*- import string s = 'Yes! This is a string' print '原字符串:'...

python tkinter控件布局项目实例

python tkinter控件布局项目实例

这篇文章主要介绍了python tkinter控件布局项目实例,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下 代码部分: from...

CentOS中升级Python版本的方法详解

CentOS升级Python2.6到Pythno2.7 最近在Linode上弄Python、出现ValueError: zero length field name in format这...

在VS2017中用C#调用python脚本的实现

情景是这样的:在C#中调用python脚本进行post请求,python脚本中使用了requests包。 Python的开发环境我们有比较多的选择,pycharm、sublime tex...

python下调用pytesseract识别某网站验证码的实现方法

一、pytesseract介绍 1、pytesseract说明 pytesseract最新版本0.1.6,网址:https://pypi.python.org/pypi/pytesser...