&引用;列表索引超出范围“;Python3文件中的错误
我有一个自制的文本加密脚本,当我输入“嘿,伙计,你好吗?”作为要加密的文本,“KFC”作为加密密钥时,它会输出一个“列表索引超出范围”错误 我更准确地了解到:&引用;列表索引超出范围“;Python3文件中的错误,python,encryption,Python,Encryption,我有一个自制的文本加密脚本,当我输入“嘿,伙计,你好吗?”作为要加密的文本,“KFC”作为加密密钥时,它会输出一个“列表索引超出范围”错误 我更准确地了解到: 回溯(最近一次呼叫最后一次): 文件“/Users/BCPianist/Google Drive/Project Cryptonite/NCrypt.py”,第75行,在 主(用户输入,a) 文件“/Users/BCPianist/Google Drive/Project Cryptonite/NCrypt.py”,第47行,主视图 cu
回溯(最近一次呼叫最后一次):
文件“/Users/BCPianist/Google Drive/Project Cryptonite/NCrypt.py”,第75行,在
主(用户输入,a)
文件“/Users/BCPianist/Google Drive/Project Cryptonite/NCrypt.py”,第47行,主视图
currentscrtrank+=1+scrtlst[scrtrankcounter]
索引器:列表索引超出范围
我在这个脚本中使用了两个文件(主脚本和函数库)。我只是不知道这个错误是从哪里来的。。。
以下是我的主要剧本:
import random
from cryptolib import *
# !/usr/bin/env python
# -*- coding: UTF-8 -*-
# enable debugging
print("Content-Type: text/plain;charset=utf-8") # Definit le code d'ecriture
print() # Insere une ligne d'espacement.
def main(userin, numberedsecretkey): # Fonction principale (le gros de l'encryption)
"""
This is my main.
>>> main("Allo", "Hey")
10842839726
"""
comments = True # Definit si les commentaires lors de l'impression sont actif ou non (False les desactive)
total = convdecimal(userin) # Time to deal with the encryption key:
scrtkeytotal = convdecimal(numberedsecretkey)
# converting numbered message to list
msglst = [int(elem) for elem in str(total)]
if comments == True:
printdebug("The initial numbered message is:%s " % msglst)
# converting numbered key to list
scrtlst = [int(thingy) for thingy in str(scrtkeytotal)]
if not comments != True:
printdebug("The initial encryption key is:%s" % scrtlst)
# Attempting Encryption
scrtrankcounter = 0
currentscrtrank = scrtlst[scrtrankcounter]
while currentscrtrank < len(msglst):
randomgen = random.randint(0, 9)
msglst.insert(currentscrtrank, randomgen)
if comments:
printdebug(str(randomgen) + " was inserted at rank: " + str(
scrtrankcounter) + ", therefore, at index position: " + str(currentscrtrank) + ".")
printdebug("List now appears as: " + str(msglst))
scrtrankcounter += 1
if scrtrankcounter > len(scrtlst):
scrtrankcounter = 0
currentscrtrank += 1 + scrtlst[scrtrankcounter]
return listtoint(msglst)
def convdecimal(userin):
rank = len(userin)
total = 0
for character in userin:
rank -= 1 # decreasing the letter rank by one
letter = ord(character)
if letter < 32:
pass
elif letter == 27:
pass
else:
rankmult = 255 ** rank # Making a multiplier
lettervalue = letter * rankmult # Multiplying the letter by this multiplier
total += lettervalue # Adding the letter's value to the total
return total
if __name__ == "__main__":
userIn = input("Enter the word/Sentence you want to encrypt:")
a = input("Enter a password with which you would like to encrypt your message:")
print(userIn)
main(userIn, a)
随机导入
从cryptolib导入*
# !/usr/bin/env python
#-*-编码:UTF-8-*-
#启用调试
打印(“内容类型:文本/纯文本;字符集=utf-8”)#定义数字代码
print()#插入对齐位置。
def main(userin,numberedsecretkey):#功能原理(legrosdell'encryption)
"""
这是我的主要任务。
>>>主(“Allo”,“Hey”)
10842839726
"""
评论=正确#定义为评论或印象,但不活动(错误,不活动)
total=convdecimal(userin)#处理加密密钥的时间:
scrtkeytotal=convdecimal(numberedsecretkey)
#将带编号的邮件转换为列表
msglst=[int(elem)表示str中的元素(总计)]
如果注释==True:
printdebug(“初始编号消息为:%s”%msglst)
#将编号键转换为列表
scrtlst=[int(thingy)表示str中的thingy(scrtkeytottal)]
如果没有评论!=正确:
printdebug(“初始加密密钥为:%s”%scrtlst)
#正在尝试加密
scrtrankcounter=0
currentscrtrank=scrtlst[scrtrankcounter]
当currentscrtranklen(scrtlst):
scrtrankcounter=0
currentscrtrank+=1+scrtlst[scrtrankcounter]
返回列表输入(msglst)
def convdecimal(userin):
秩=len(userin)
总数=0
对于userin中的字符:
秩-=1#将字母秩减少1
字母=ord(字符)
如果字母<32:
通过
elif字母==27:
通过
其他:
rankmult=255**rank#生成乘数
lettervalue=字母*rankmult#将字母乘以此乘数
总计+=字母值#将字母值添加到总计中
返回总数
如果名称=“\uuuuu main\uuuuuuuu”:
userIn=input(“输入要加密的单词/句子:”)
a=输入(“输入一个密码,用于加密您的邮件:”)
打印(用户输入)
主(用户输入,a)
这是我的函数库文件:
import os
DEBUG = True
def printdebug(log: object) -> object:
if DEBUG:
print(log)
def loading(sec=10):
start = 1
input("Press Enter to continue")
printdebug("loading...")
while start <= sec:
printdebug("...")
start += 1
def codepause():
os.system("Pause")
input("press enter to continue")
def listtoint(msglst):
# Conversion from list to int
"""
>>> listtoint([1,2,3,4,5,6,7,8,9,0])
1234567890
"""
ncryptedkey = 0
base = 10
for d in msglst:
ncryptedkey = base * ncryptedkey + d
# printDebugJob:
printdebug("The encrypted message is:" + str(ncryptedkey))
return ncryptedkey
def convdecimal(userin):
rank = len(userin)
total = 0
for character in userin:
rank -= 1 # decreasing the letter rank by one
letter = ord(character)
if letter < 32:
pass
elif letter == 27:
pass
else:
rankmult = 255 ** rank # Making a multiplier
lettervalue = letter * rankmult # Multiplying the letter by this multiplier
total += lettervalue # Adding the letter's value to the total
def letterprint(nb):
nb = chr(nb)
print(nb, end='')
导入操作系统
调试=真
def printdebug(日志:对象)->对象:
如果调试:
打印(日志)
def加载(秒=10):
开始=1
输入(“按回车键继续”)
printdebug(“加载…”)
虽然start我还没有测试过这个,但是看起来你需要把大于号换成大于或等于,因为它可能太高了
if scrtrankcounter >= len(scrtlst):
scrtrankcounter = 0
currentscrtrank += 1 + scrtlst[scrtrankcounter]
例如,如果scrtlst
的长度为5,则最高索引为4,因此如果您尝试scrtlst[5]
,它将给出一个错误