密文XOR错误

时间:2017-02-27 14:55:40

标签: python

我正在尝试获取文本文件并执行一些XOR的密文。当我运行脚本时,我从XORing密文1和其他密文获得了以下错误。有人可以帮我吗?

Traceback (most recent call last):
  File "./letters.py", line 45, in <module>
    print xorTwoLists(c[0],c[x], lenTarget)
  File "./letters.py", line 22, in xorTwoLists
    xorValue = int(l1[x],16)  ^  int(l2[x],16)
IndexError: list index out of range

这是我的代码:

#!/usr/bin/python

def splitIntoHexBytes (s) :
   l = []
   for x  in range(0, len(s)/2) :
      y = x*2
      hexStr = '0x'+ s[y:(y+2)]
      l.append( hexStr )
   return l


def makeLen4string(s) :
   if len(s) < 4  :  
      return  s[0:2] + '0' + s[2]
   else :
      return  s


def  xorTwoLists(l1, l2, length) :
   resultList = []
   for x in range(length) :
      xorValue = int(l1[x],16)  ^  int(l2[x],16)
      hexXorValue = hex(xorValue)
      hexString = makeLen4string(hexXorValue)
      resultList.append(hexString)
   return resultList



infile = open('ctexts.txt', 'r')
ciphertexts = infile.readlines()
infile.close()
target = splitIntoHexBytes(ciphertexts[0])

c=[]
for x in range( len(ciphertexts)-1 ) :
   c.append(splitIntoHexBytes(ciphertexts[x+1]) )


lenTarget = len(target)

# Check the first ciphertext for blanks
print "the folllowing is the output of XORing ciphertext 1 with the others ciphertexts :"
for x in range(1, len(c) ):
  print xorTwoLists(c[0],c[x], lenTarget)
print
print "the folllowing is the output of XORing ciphertext 1 with the target :"
print xorTwoLists(c[0],target, lenTarget)

1 个答案:

答案 0 :(得分:0)

如果要对两个整数列表进行异或,请尝试以下操作:

  <!-- Modal content-->
  <div class="modal-content">
    <div class="modal-header">
      <h4 class="modal-title">Fill the details</h4>
    </div>
    <div class="modal-body">
         <div class="container">
                   <!--<h2> Horizontal form: control states</h2>-->
                  <form class="form-horizontal" method="get">
                    <div class="form-group">
                      <label class="col-sm-2 control-label">User Name</label>
                      <div class="col-sm-3">
                        <input class="form-control"  type="text" readonly>
                      </div>
                    </div>

                    <div class="form-group">
                      <label for="inputPassword" class="col-sm-2 control-label">Password</label>
                      <div class="col-sm-3">
                        <input class="form-control" type="text">
                      </div>
                    </div>

                    <div class="form-group">
                      <label for="inputPassword" class="col-sm-2 control-label">Address1</label>
                      <div class="col-sm-3">
                        <input class="form-control" type="text">
                      </div>
                    </div>

                    <div class="form-group">
                      <label for="inputPassword" class="col-sm-2 control-label">Address2</label>
                      <div class="col-sm-3">
                        <input class="form-control"  type="text">
                      </div>
                    </div>

                   </form>
          </div>
    </div>
   </div>
    <!-- ^^^^^^Modal content^^^^^^^-->

    <div class="modal-footer">
      <button type="button" class="btnmodal btn-default" onclick="okButtonClick()">Ok</button>
  </div>
</div>
<!--^^^^^^^^ Modal ^^^^^^^^-->

对于输入:

def xorTwoLists(l1, l2):
    xorBytes = lambda (x, y): x ^ y # lambda function to XOR individual bytes
    return map(xorBytes, zip(l1, l2))  # zip the two lists then apply xorBytes to the byte tuples

您将获得两个列表的XOR,直到较小列表的长度。 在这种情况下:

c1 = [224, 22, 149, 10, 65, 125, 77, 58]
c2 = [11, 162, 186, 182, 69, 175, 52, 75, 13, 119, 8, 176, 0, 39, 205, 74]

希望它有所帮助。