python 异或加密字符串的实例

yipeiwu_com6年前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中最好用的命令行参数解析工具(argparse)

Python 做为一个脚本语言,可以很方便地写各种工具。当你在服务端要运行一个工具或服务时,输入参数似乎是一种硬需(当然你也可以通过配置文件来实现)。 如果要以命令行执行,那你需要解析...

详解Python中的__new__()方法的使用

先看下object类中对__new__()方法的定义: class object: @staticmethod # known case of __new__...

Python中max函数用法实例分析

本文实例讲述了Python中max函数用法。分享给大家供大家参考。具体如下: 这里max函数是Python内置的函数,不需要导入math模块 # 最简单的 max(1, 2) max...

pytorch permute维度转换方法

permute prediction = input.view(bs, self.num_anchors, self.bbox_attrs, in_h, in_w).permut...

Python利用递归实现文件的复制方法

如下所示: import os import time from collections import deque """ 利用递归实现目录的遍历 @para sourcePath...