fork(6) download
  1. def chain_add(seq, end):
  2. a = len(seq)
  3. for i in range(end-a):
  4. seq += chr(48+(ord(seq[i])+ord(seq[i+1]))%32%10)
  5. return seq
  6.  
  7. def sequent(seq):
  8. res = [0]*len(seq);seq=list(seq)
  9. for z in range(len(seq)):
  10. b=min(range(len(seq)),key=lambda i:seq[i])
  11. res[b]=z
  12. seq[b]="~"
  13. return res
  14.  
  15. def transpose(text, keys, disrupt=False):
  16. if disrupt:
  17. num_rows, len_last = divmod(len(text),len(keys))
  18. num_rows+=1
  19. if len_last==0:len_last=len(keys)
  20. d_rows = []
  21. for i in range(num_rows):
  22. d_rows += [[]]
  23. text_index = 0
  24. current_row = 0
  25. stop_key = 0
  26. stop_index = keys.index(stop_key)
  27. while current_row < num_rows:
  28. d_rows[current_row]=list(text[text_index:text_index+stop_index])
  29. text_index+=stop_index
  30. stop_index+=1
  31. if stop_index>len(keys):
  32. stop_key=(stop_key+1)%len(keys)
  33. stop_index=keys.index(stop_key)
  34. current_row+=1
  35. current_row = 0
  36. stop_key = 0
  37. stop_len = len(keys)-keys.index(stop_key)
  38. while current_row < num_rows:
  39. d_rows[current_row]+=list(text[text_index:text_index+stop_len])
  40. text_index+=stop_len
  41. stop_len-=1
  42. if stop_len<1:
  43. stop_key+=1
  44. stop_len=len(keys)-keys.index(stop_key)
  45. current_row+=1
  46. current_row+=1
  47. d_rows[-1]+=['']*(len(keys)-len(d_rows[-1]))
  48. columns = []
  49. for j in range(len(keys)):
  50. columns+=[''.join(i[j]for i in d_rows)]
  51. else:
  52. columns = ['']*len(keys)
  53. for t in range(len(text)):
  54. columns[t%len(keys)]+=text[t]
  55. res = [0]*len(keys)
  56. for index in range(len(keys)):
  57. res[keys[index]]=columns[index]
  58. return''.join(res)
  59.  
  60. def checkerboard(message, seq, word, null):
  61. trans = {".":seq[-2:], "#":seq[-1]*2};m=65;res=''
  62. for i in range(len(word)):
  63. trans[word[i]]=seq[i]
  64. for z in[0,1]:
  65. while chr(m)in word:
  66. m+=1
  67. trans[chr(m)]=seq[z-2]+seq[i];m+=1
  68. while m<90:
  69. for i in seq[len(word):]:
  70. for z in[0,1]:
  71. while chr(m)in word:
  72. m+=1
  73. trans[chr(m)]=seq[z-2]+i;m+=1
  74. for i in message:
  75. if i.isdigit():
  76. res+=i
  77. else:
  78. res+=trans[i]
  79. return res+null[:-len(res)%5]
  80.  
  81. def vic_cipher(message, keyword, phrase, date, agent, m_id, null):
  82. s1=sequent(phrase[:10])
  83. s2=sequent(phrase[10:20])
  84. a=g=t=""
  85. for i in range(5):
  86. a+=str((int(m_id[i])-int(date[i]))%10)
  87. a=chain_add(a,10)
  88. for i in range(10):
  89. g+=str((int(a[i])+int(s1[i]))%10)
  90. for i in range(10):
  91. t+=str(s2[int(g[i])])
  92. u=chain_add(t,60)[10:]
  93. p=agent+int(u[-2])
  94. q=agent+int(u[-1])
  95. seqT=sequent(t)
  96. v=transpose(u,seqT)
  97. k1=sequent(v[:p])
  98. k2=sequent(v[p:p+q])
  99. c=''.join(str(i)for i in sequent(u[40:]))
  100. x=checkerboard(message,c,keyword,null)
  101. y=transpose(x,k1)
  102. z=transpose(y,k2,1)
  103. e=[z[5*i:5*-~i]for i in range(-(-len(z)//5))]
  104. return' '.join(e[:1-int(date[-1])]+[m_id]+e[1-int(date[-1]):])
  105.  
  106. print(vic_cipher("HINGELSE.MOVETOSAFEHOUSEFOXTROT#3#..WEAREDISCOVERED.TAKEWHATYOUCAN.BURNEVERYT",\
  107. "SENATORI","THEFIRSTPRINCIPLEIST","3172016",9,"47921","4561"))
Success #stdin #stdout 0.02s 9984KB
stdin
Standard input is empty
stdout
71431 14842 71192 58619 04185 22614 14745 49924 32826 13348 02189 38641 70140 40252 44820 64522 43982 47921 71283 22694 94443 80642 14521