字符串異或加密


# coding = utf-8
# 字符串異或加密
import random
choice = input('加密(1)還是解謎(2)?')


def encode(str1, key):
    #設置秘鑰種子
    random.seed(key)
    str2 = ''
    for c in str1:
        str2 += str(ord(c) ^ random.randint(0, 255))+','
    str2 = str2.strip(',')
    return str2


def decode(str2, key):
    random.seed(key)
    str1 = ''
    for i in str2.split(","):
        i = int(i)
        str1 += chr(i ^ random.randint(0, 255))
    return str1



if choice == '1':
    str1 = input("請輸入要加密的明文:")
    key = input('請輸入秘鑰:')
    str2 = encode(str1, key)
    print(str2)
elif choice == '2':
    str2 = input("請輸入密文,數字用逗號分隔:")
    key = input('請輸入秘鑰:')
    str1 = decode(str2, key)
    print(str1)
else:
    print("輸入錯誤!")

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM