0

有什么办法可以将我的输出放在 16 行中?这些行也需要有 16 列垂直排列。我想在不导入任何东西的情况下做到这一点。

def main():

    # asks the user to input key
    key = input("Enter a key [6-15 characters]: ")

    s = []

    # append all items in list
    for i in range(256):

        s.append(i)

    #Runs the key through a loop to give RC4 encryption and assigns j to 0
    j = 0
    for i in range(256):
        j = ((j+s[i]+ord(key[i%len(key)]))%256)
        #Swap values of s[i] and s[j]
        s[i],s[j] = s[j],s[i]
    print(s)

main()
4

1 回答 1

1

你想要 16 行,每行 16 个字符吗?这是我的做法(以十六进制打印):

s = range(256)

for i in xrange(256):
    j = ((j+s[i]+ord(key[i%len(key)]))%256)
    s[i],s[j] = s[j],s[i]

for i in xrange(0, 256, 16):
    print ''.join('%02x' % c for c in s[i:i+16])

对于随机生成的 256 字节序列([0,255] 范围内的整数),我得到

f7a98bd77f4accd553f1b86f582d678e
265e38af7c6c4e09af0a6132bec36fb5
47527d8acc69c4f3f099d0fa34fdb527
7a170e102c794692356ca0e441c787e9
98dfb550b316a3952e67983489f4f1f4
8f8db56a58e94b6beae95fc5f394295a
83ed9c72b6c24dbf15ad28506d505eed
e2359cdeed6c1526b2c3d11b14074b08
332e37fcd686b50d6de15df4feb8c404
de5c0ae40eaf755f209300ad5143397f
77b626dab0c99e9658a4eb1d5f028a40
bb5ed8d40b6dc925954990aa84bed906
5ed527fab36762e8c65069a2f4a68888
0a114e0019e3b2174265d77a9e959a06
52429fc7106db54098a6c8a1219625aa
e59b98fc6c38a115fca2e0d7faf3376f

您可以调整最后一行以满足您的需要。例如,要打印十进制整数,请使用

for i in xrange(0, 256, 16):
    print ''.join('%4d' % c for c in s[i:i+16])

要得到

  92 142 194 158 114  83 120  69 155 118 191  72  33  53 216 219
  29  97  30 213 236 177 170  96 101  45 126   6  48  43 185   2
 139 123  47  19 147 191 222 127 254  26 248 183 192 232 190 226
 110 125 222 252 212 235 246 120 107 195  52 251 169 220 157 182
 169  47 151 122 131  73 174  97 175 202 201 225  66 179  34 160
 105  62 117 227  80 217  25  17 120  51  56 209 238 249 121  21
 152 100 113 190 209 243 165  44 118  85 195 236 176 105 149 225
 146 225  69 225  89  12 176 194  88 223 141 185 107 216 157 198
  16  57 132  10   3 155 130 227  30  10  11  31   7 212 244  61
  50 212  21  98 202 244  62 180  23   5 148   3  91  53 226 147
 189 189 222 124 179 194 147  43  46  94 216  62  10  44 149  16
 173 120  78  75 123 113  61   4 106 100  96 117 243 181 118 148
 223 110  83 117 212  86  83 161 141  48 193  48 255 196  20  10
 169 144  64 160 239 136  34 145 104 101  53  58   5 178  20 141
  46  40 254 153 166 108 172 215 184 222  55 127 116 210  49 216
 202 120  34 208  22  57  98 179 238 153 173 195 101 229 113 220
于 2013-03-30T02:29:17.787 回答