-2

我一直在尝试用 Python 编写一个简单的加密程序,当我尝试在 Linux 上执行代码时,它没有打印任何东西。有人可以告诉我为什么吗?

    #!/usr/bin/env python2
import binascii
def encrypt():
    text = raw_input("Please input your information to encrypt: ")
    for i in text:
        #Convert text into a binary sequence
        i = bin(int(binascii.hexlify(i),16))
    key = raw_input("Please input your key for decryption: ")
    for j in key:
        #Convert key into a binary sequence
        j = bin(int(binascii.hexlify(j),16))
    #This is only here for developmental purposes
    print key
    print text

编辑 我做了一位用户所说的,但我的代码似乎仍然没有像我想要的那样将我的纯文本转换为二进制文件。

#!/usr/bin/env python2
def encrypt():
    text = raw_input("Please input your information to encrypt: ")
    for i in text:
        #Convert text into a binary sequence
        i = bin(ord(i))
    key = raw_input("Please input your key for decryption: ")
    for j in key:
        #Convert key into a binary sequence
        j = bin(ord(j))
    #This is only here for developmental purposes
    print key
    print text

encrypt()
4

2 回答 2

2

您的程序存在许多主要问题:

  1. 您正在定义一个名为 的函数encrypt(),但从不调用它。

  2. 您的两个循环都没有实际修改字符串。他们修改循环变量,这不是一回事!

  3. int(binascii.hexlify(x), 16)是一种过于复杂的写作方式ord(x)

  4. bin()没有做你希望在这里做的事情。

  5. 这里实际上没有任何东西被异或。您的程序未完成。

于 2013-10-08T05:08:00.833 回答
0

我认为这就是你想要做的

def encrypt():
  cleartext = raw_input("Please input your information to encrypt: ")
  ciphertext = []
  for char in cleartext:
    ciphertext.append(bin(ord(char))[2:])
  ciphertext = ''.join(ciphertext)

  key = raw_input("Please input your key for decryption: ")
  decryptionKey = []
  for char in key:
    decryptionKey.append(bin(ord(char))[2:])
  decryptionKey = ''.join(decryptionKey)

  print "The key is '%s'" %decryptionKey
  print "The encrypted text is '%s'" %ciphertext
于 2013-10-08T05:26:13.427 回答