python - Python如何将字符转换为字符串
问题描述
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
无法解密以获取原件。
推荐阅读
- c# - 为什么我的 .NET REST API 返回带有额外子属性的列表而不是 [..]?
- c++ - 3D 向量的高效除法算子
- python - 在列表中元素的开头添加 2 个空格
- python - @object.method 在 Python 中做了什么?
- javascript - 如何找到我要编辑的联系人的 ID?
- sql - 夜班工作人员与 lenel onguard 的持续时间
- jquery - 如何将 $.ajax 与 then() 一起使用?
- javascript - 如何修复自定义警报功能
- vue.js - WebStorm Vue 未解析的变量或类型
- facebook-android-sdk - Facebook 原生广告未显示,错误“1011 - 显示格式不匹配”