diff --git a/qraltcrypt.py b/qraltcrypt.py index 30d9458..d673f09 100644 --- a/qraltcrypt.py +++ b/qraltcrypt.py @@ -6,6 +6,7 @@ import sys P=int(base64.b85decode('|NsC0|NsC0$q(A1A!x!h#KvNafZQC>DFRLiif7Dp0t>#TI}@8CQA!ATjx+?_@0A&|%{oIHFe?ge@?R7;PvJFfZBfES?l^1vT8W>fuAd|kd`lnZNhoGf?BrWL!T@}@p<}-VnA%7-997erX%;`I{Upy{gJoSK+@r0Q9AfiUAca!9p4BM_aF&*BXACt?O1zi^@pNnm$`~PhGLT+IE;i0P<6NcwZ&cVZ%k6j-Yd)Yl~@-TIh9F#>XmBc71AacnEC|}6>^SRima+=|NsC0|NsC0').hex(),16) h=lambda k,m:hmac.new(k,m,'sha256').digest() def kg():s=secrets.randbelow((P-1)//2);return s,pow(2,s,P) +cr=lambda k,x:bytes(a^b for a,b in zip(x,b''.join(h(h(b'e',k),n+bytes([i&255,i>>8]))for i in range((len(x)+31)//32)))) def pe(s):print(s,file=sys.stderr);sys.exit(1) def us():pe('Usage: qraltcrypt.py -G|-E|-D seckeyfile/pubkey') a=sys.argv @@ -17,10 +18,10 @@ if a[1]=='-G': elif a[1]=='-E': m=sys.stdin.buffer.read() if len(m)>1<<21:pe('Error: input too big') - e,E=kg();k=bytes.fromhex('%0512x'%pow(int(a[2],16),e,P));n=os.urandom(32);c=bytes(i^j for i,j in zip(m,b''.join(h(h(b'e',k),n+bytes([i&255,i>>8]))for i in range((len(m)+31)//32))));print('%0512x%s'%(E,(n+c+h(h(b'm',k),c)).hex())) + e,E=kg();k=bytes.fromhex('%0512x'%pow(int(a[2],16),e,P));n=os.urandom(32);c=cr(k,m);print('%0512x%s'%(E,(n+c+h(h(b'm',k),c)).hex())) elif a[1]=='-D': with open(a[2],'r')as f:s=int(f.read(),16) i=input();E,e=int(i[:512],16),bytes.fromhex(i[512:]);n,c,t=e[:32],e[32:-32],e[-32:];k=bytes.fromhex('%0512x'%pow(E,s,P)) if not secrets.compare_digest(t,h(h(b'm',k),c)):pe('Error: auth fail') - sys.stdout.buffer.write(bytes(i^j for i,j in zip(c,b''.join(h(h(b'e',k),n+bytes([i&255,i>>8]))for i in range((len(e)+1)//32))))) + sys.stdout.buffer.write(cr(k,c)) else:us()