使用Python Caesar代码-需要帮助理解前几行代码吗

使用Python Caesar代码-需要帮助理解前几行代码吗,python,loops,encryption,decode,encode,Python,Loops,Encryption,Decode,Encode,我用python创建了一个Caesar密码程序,它编码、解码、读取和写入消息。我的解决方案完全有效,我没有面临任何问题。我的目标是在尽可能少的代码行中完成任务,经过大量研究,我使用了一些代码作为灵感。我尝试了许多不同的版本,但这似乎对我来说是最好的我要问的是,我想了解代码的前5行是如何工作的,在这两行代码中特定数字的意义,循环和函数。我只想用几句话来解释这5行代码负责什么过程或计算,以及它是如何执行它们的 非常感谢您的帮助,因为我一直在研究类似的代码集,但在我的写作中找不到对代码的任何解释。抱歉

我用python创建了一个Caesar密码程序,它编码、解码、读取和写入消息。我的解决方案完全有效,我没有面临任何问题。我的目标是在尽可能少的代码行中完成任务,经过大量研究,我使用了一些代码作为灵感。我尝试了许多不同的版本,但这似乎对我来说是最好的

我要问的是,我想了解代码的前5行是如何工作的,在这两行代码中特定数字的意义,循环和函数。我只想用几句话来解释这5行代码负责什么过程或计算,以及它是如何执行它们的

非常感谢您的帮助,因为我一直在研究类似的代码集,但在我的写作中找不到对代码的任何解释。抱歉提供了这么长的背景信息

以下是我的全部代码:

def caesar(s, k, decode = False):
  if decode: k = 26 - k
  return "".join([chr((ord(i) - 65 + k) % 26 + 65)
                for i in s.upper()
                if ord(i) >= 65 and ord(i) <= 90 ])
result = str
decrypt = str
offset = int
exit = False
while(exit == False):
  print " "
  print " " 
  print "Welcome to Caeser's Cipher code, you can encrypt, decrypt, read, and store messages"
  print "DECRYPT - DECRYPT A MESSAGE"
  print "ENCRYPT - ENCRYPT A MESSAGE"
  print "READ - READ AND DECODE A MESSAGE"
  print "EXIT - EXIT THE PROGRAM"

  NoOfLetters = 26
  offset = 0
  option = raw_input("Please Enter your choice: ")

  if(option == "DECODE"):
    decrypt = raw_input("Enter the coded message: ")
    offset = int(raw_input("Enter the  Offset as an integer: "))
    print "Result: "
    print caesar(decrypt, offset, decode = True)
    print

  elif(option == "ENCODE"):
    encrypt = raw_input("Enter the message to code")
    offset = int(raw_input("Enter the Offset as integer: "))d
    print "Result: "
    print caesar(encrypt, offset, decode = False)
    result = caesar(encrypt, offset, decode = False)

    option2 = raw_input("Would you like to store this coded message and offset? (Y/N): ")
    if(option2 == "Y"):
      f = open("code.txt", "w")
      print >> f, result
      print >> f, offset
      f.close()
      print"The message and offset has been stored in a file called 'code.txt'"

  elif(option == "READ"):
    print"The program is reading and decrypting the code stored in 'code.txt'"
    print"The code.txt file is stored in the same location as the application"
    f = open("/Users/Emaad/Desktop/CIPHER CODE/code.txt")
    decrypt = f.readline()
    offset = int(f.readline())
    f.close()
    print "Result:"
    print caesar(decrypt, offset, decode = True)
    print "Offset:"
    print offset


  elif(option == "EXIT"):
    print"Thanks for using..."
    print"exiting the application...."
    exit = True
def caesar(s,k,decode=False):
如果解码:k=26-k
返回“”连接([chr((ord(i)-65+k)%26+65)
对于s.upper()中的i
如果ord(i)>=65且ord(i)>f,则为结果
打印>>f,偏移量
f、 关闭()
打印“消息和偏移量已存储在名为“code.txt”的文件中”
elif(选项==“读取”):
打印“程序正在读取和解密存储在“code.txt”中的代码”
打印“code.txt文件存储在与应用程序相同的位置”
f=打开(“/Users/Emaad/Desktop/CIPHER CODE/CODE.txt”)
decrypt=f.readline()
偏移量=int(f.readline())
f、 关闭()
打印“结果:”
打印caesar(解密、偏移、解码=真)
打印“偏移量:”
印刷胶印
elif(选项==“退出”):
打印“感谢使用…”
打印“退出应用程序…”
退出=真
下面是我想理解的代码行:

def caesar(s, k, decode = False):
    if decode: k = 26 - k
    return "".join([chr((ord(i) - 65 + k) % 26 + 65)
                for i in s.upper()
                if ord(i) >= 65 and ord(i) <= 90 ])
def caesar(s,k,decode=False):
如果解码:k=26-k
返回“”连接([chr((ord(i)-65+k)%26+65)
对于s.upper()中的i

如果ord(i)>=65且ord(i),则有助于理解我们是否将代码拆分为更小的函数:

def chr2int(i):
    return ord(i) - 65
这用于将大写字母映射为从0开始的整数。示例:

chr2int('A') == ord('A') - 65 == 0
chr2int('B') == ord('B') - 65 == 1
chr2int('Z') == ord('Z') - 65 == 25
65是大写字母“A”的十进制代码

ord('A') == 65
rot()将k与数字n相加(模26):


26是从A到Z的所有字母数:

ord('Z') - ord('A') + 1 == 26 
例如:

rot(1, 1) == 2
rot(25, 0) == 25
rot(25, 1) == 0
rot(25, 2) == 1
int2chr()执行chr2int的反函数-将整数转换回字母

def int2chr(i):
    return chr(i + 65)
现在,您可以将三个功能组合在一起,以一个(大写)字符执行Caeser chiper:

def caeser_char(char, key):
    return int2chr(rot(chr2int(char), key))
要将ceaser chiper应用于整个单词,您可以使用列表理解:

def ceaser(word, key):
    chars = [caeser_char(i, key)                # perform caeser cipher
             for i in word.upper()              # for each char in word
             if ord(i) >= 65 and ord(i) <= 90]  # only if i is a letter
                                                # (between word('A')==65 and ord('Z')==90)
    return "".join(chars)                       # build a word from a list of chars

希望这有帮助。

您声称编写了这段代码,但不知道它是如何工作的?很可能您是在网上的某个地方发现了这段代码片段,在这种情况下,这对您的学习没有帮助。原因是您的函数在第二行之后返回,使其余代码变得无关紧要。我编写了这段代码的大部分,正如我说前5行纯粹是其他人的作品,但我无法用我所做的所有研究来解释这一部分是如何工作的。这太棒了,它已经帮助我加载了,我只需要对“模26”部分进行一点阐述,以及它到底需要什么。26是从a到Z的所有字母的数量。“模”是在一个范围内“旋转”一个数字的运算。使用“模12”,你可以像模拟时钟一样执行算术运算,11AM+2h=1PM,(11+2)%12==1。Caeser密码的工作原理相同-你需要将加密密钥添加到每个字母代码中。然后你可以执行“模26”运算,围绕所有可能的字母旋转。例如:“ZED”+1(键)='AFE',或,(25+1)%26==0,(4+1)%26==5,(5+1)%26==6
def ceaser(word, key):
    chars = [caeser_char(i, key)                # perform caeser cipher
             for i in word.upper()              # for each char in word
             if ord(i) >= 65 and ord(i) <= 90]  # only if i is a letter
                                                # (between word('A')==65 and ord('Z')==90)
    return "".join(chars)                       # build a word from a list of chars
def decode_key(encode_key):
    return 26 - encode_key