首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Python加密程序

Python加密程序
EN

Stack Overflow用户
提问于 2020-10-01 07:11:10
回答 1查看 69关注 0票数 0

我正在试着写一个替换加密程序,它根据一个随机数来改变字母表。这是逐个字母或逐个字符完成的

这就是我得到的错误。

代码语言:javascript
复制
File "./encrypt.py", line 35, in <module>
    print("after encryption: ", encrypt(text))
File "./encrypt.py", line 26, in encrypt
    cipher = cipher + chr((ord(char) + str(function1()) - 65) % 27 + 65) TypeError: unsupported operand type(s) for +: 'int' and 'str'

有什么解决方案吗?

代码语言:javascript
复制
import random

text = input("enter string: ")

number = 0
number += len(text)

def encrypt(string):

  def otp(filename):
    file = open(filename + ".txt", "a")
    file.write(str(function1()) + "\n")
    file.close()

  def function1():
    for num in range(number):
      shift = str(random.randint(1,26))
      print(shift)

  cipher = ''
  for char in string:
    if char == ' ':
        cipher = cipher + chr((ord(char) + str(function1()) - 65) % 27 + 65)
        otp("onetimepad")
    elif  char.isupper():
        cipher = cipher + chr((ord(char) + str(function1()) - 65) % 27 + 65)
        otp("onetimepad")
    else:
        cipher = cipher + chr((ord(char) + str(function1()) - 97) % 27 + 97)
        otp("onetimepad")

  return cipher

print("original string: ", text)
print("after encryption: ", encrypt(text))
EN

回答 1

Stack Overflow用户

发布于 2020-10-01 07:14:50

错误消息非常清楚。所引用的代码行只有两个+操作,第一个操作显然违反了+的可用定义

代码语言:javascript
复制
ord(char) + str(function1())

在左边有一个整数;在右边,您显式地将返回值转换为字符串。确定你想要做什么,并修复你的表达。看起来你正在尝试对一个新的序数值进行整数计算。解决方案可能只是简单地不转换函数的返回值。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/64146968

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档