#!/usr/bin/env python3 # -*- coding: utf-8 -*- ''' , /| , ,--.________/ /-----/|-------------------------------------.._ ( /_/_/_/_ |--------- DEATH TO ALL TABS ---------------< _`> `--´ \ \-----\|-------------------------------------''´ \| ' '''# ' assert __name__ == '__main__' import sys def die(E): print(F'E:',E,file=sys.stderr) sys.exit(1) T=lambda A,B,C,D,E,F,G,H,I:A*E*I+B*F*G+C*D*H-G*E*C-H*F*A-I*D*B&255 def U(K): R=pow(T(*K),-1,256) A,B,C,D,E,F,G,H,I=K return [R*V%256 for V in [E*I-F*H,C*H-B*I,B*F-C*E,F*G-D*I,A*I-C*G,C*D-A*F,D*H-E*G,B*G-A*H,A*E-B*D]] def C(K,M): B=lambda A,B,C,D,E,F,G,H,I,X,Y,Z:bytes((A*X+B*Y+C*Z&0xFF, D*X+E*Y+F*Z&0xFF,G*X+H*Y+I*Z&0xFF)) N=len(M) R=N%3 R=R and 3-R M=M+R*B'\0' return B''.join(B(*K,*W) for W in zip(*[iter(M)]*3)).rstrip(B'\0') len(sys.argv) == 3 or die('FOOL') K=bytes(sys.argv[2], 'ascii') len(K)==9 and T(*K)&1 or die('INVALID') M=sys.stdin.read() if sys.argv[1].upper() == 'E': M=B'SPACEARMY'+bytes(M,'ascii') print(C(U(K),M).hex().upper()) else: M=C(K,bytes.fromhex(M)) M[:9]==B'SPACEARMY' or die('INVALID') print(M[9:].decode('ascii'))