簡體   English   中英

Python如何將字符轉換為字符串

[英]Python how to convert character to string

array = "qwertyuiopasdfghjklzxcvbnm"

m = len(array)
user_input= input("Enter plaintext: ")
a= int(input("choose a: "))
b= int(input("choose b: "))

encryption = ""
for n in array:

   if n in user_input :
    
    inin=array.find(n)
    result =array.index(user_input) * a+b
    enc= result % m
    encryption = encryption + array[enc]
    
    
    
    
   
 print("Encrypted message is :"+encryption)
    
    
   

正如您從上面的代碼中看到的,我的代碼運行良好,但我面臨的唯一問題是我需要將消息加密為字符串,就像我想使用“hello world”一樣,而不僅僅是一個字符“s”,並將其加密為“ d”我想要“你好詞”

我試圖更正您的代碼:

array = "qwertyuiopasdfghjklzxcvbnm"

m = len(array)
user_input= input("Enter plaintext: ")
a= int(input("choose a: "))
b= int(input("choose b: "))

encryption = ""
for char in user_input:
   if char in array :
    index = (array.find(char) * a + b) % m
    encryption += array[index]
   else:
    encryption += char

print("Encrypted message is :", encryption)

據我了解,您正試圖通過使用仿射函數移動索引來加密您的消息(並應用模數,以便在索引大於您的字母大小時“循環”)。 由於您的輸入可以包含多次相同的字符,因此您需要遍歷輸入,然后檢查字母表(數組)中每個字符的索引。 然后,您可以將公式應用於索引並獲取新字符。 如果數組中不存在未加密的字符,我還添加了一個代碼來添加它。 這將防止您在加密時丟失信息。 例如: hello world將被加密xirrm tmars代替xirrmtmars這不可能是不加密來獲得原始。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM