librsa/0000775000175000017500000000000011770066045011726 5ustar kaiyoukaiyoulibrsa/__init__.pyc0000664000175000017500000002203411770066045014203 0ustar kaiyoukaiyou daOc@s1ddlZddlZddlZddlZddlZddlZddlmZmZdZ dZ dZ dZ dZ dZd Zd Zd Zd Zd ZdZdZdZdZdZdZdZdZdZdZdZdZdS(iN(tdumpstloadscCsdGt\}}}}tjddGt|||}t|dd}|jdtjdt|dt|dd |j t|d d}|jd tjdt|d t|dd |j dS(Ns. . .is.pubtws----- BEGIN PUBLIC KEY ----- s(n = s, e = t)s----- END PUBLIC KEY ----- s.privs----- BEGIN PRIVATE KEY ----- s, d = s----- END PRIVATE KEY ----- ( t genPubKeyttimetsleept genPrivKeytopentwritetbase64t encodestringtstrtclose(tnametntetptqtdtf((sV/home/kaiyou/Projects/ndh2k12/challenges/wargame2k12/crypto/rsa/src/librsa/__init__.pytgenkey s , ,cCsttj}|d9}tt|}tj}|d9}tt|}||}d}dG||||fS(Ni iies+ + +ii(trandomt nextPrimetint(RRRR((sV/home/kaiyou/Projects/ndh2k12/challenges/wargame2k12/crypto/rsa/src/librsa/__init__.pyR s     cCs#|d|d}dGt||S(Nis+ + +(tmul_inv(RRRtm((sV/home/kaiyou/Projects/ndh2k12/challenges/wargame2k12/crypto/rsa/src/librsa/__init__.pyR.scCst}xu|s}|d}ttj|d}d}x*||krd||dkrd|d}q;W||kr t}q q W|S(Niii(tFalseRtmathtsqrttTrue(RtisPrimetlimitti((sV/home/kaiyou/Projects/ndh2k12/challenges/wargame2k12/crypto/rsa/src/librsa/__init__.pyR3s    cCsz|dkr|dkrdS|dkrE|t|dt|fSt|||\}}}||||||fS(Ni(iii(tabstbezout(tatbtutvR((sV/home/kaiyou/Projects/ndh2k12/challenges/wargame2k12/crypto/rsa/src/librsa/__init__.pyR#?s  cCs6t||\}}}|dkr2|t|SdS(Ni(R#R"(txRR&t_R((sV/home/kaiyou/Projects/ndh2k12/challenges/wargame2k12/crypto/rsa/src/librsa/__init__.pyREs cCszt|tjkp't|tjks9tdnd}x4|dkrudt|d@|f}|d:}qBW|S(NsYou must pass a long or an inttis%s%sii(ttypettypestLongTypetIntTypet TypeErrortchr(Rtstring((sV/home/kaiyou/Projects/ndh2k12/challenges/wargame2k12/crypto/rsa/src/librsa/__init__.pyt int2bytesIs*cCsSd}xF|D]>}|d9}t|tjkrAt|}n||7}q W|S(Nii(R+R,t StringTypetord(tbytestintegertbyte((sV/home/kaiyou/Projects/ndh2k12/challenges/wargame2k12/crypto/rsa/src/librsa/__init__.pyt bytes2intSs  cCst|}|d}ttjtj|d}|d}||}||dkrh|d7}ng} xTt|D]F} | |} || | |!} t| } | j|| ||q{Wt| S(Niiii( tlenRRtfloortlogtrangeR8tappendt picklechops(tmessagetkeyRtfuncreftmsglentmbitstnbitstnbytestblockstciphertbindextoffsettblocktvalue((sV/home/kaiyou/Projects/ndh2k12/challenges/wargame2k12/crypto/rsa/src/librsa/__init__.pyt chopstring[s  !     cCs.tjt|}tj|}|jS(N(tzlibtcompressRR R tstrip(tchopsRKtencoded((sV/home/kaiyou/Projects/ndh2k12/challenges/wargame2k12/crypto/rsa/src/librsa/__init__.pyR>mscCsttjtj|S(N(RRMt decompressR t decodestring(R1((sV/home/kaiyou/Projects/ndh2k12/challenges/wargame2k12/crypto/rsa/src/librsa/__init__.pyt unpicklechopsrscCsq||}g}x+|dkr=|j|d@|d?}qWx,|rl|j}|||d|}qAW|S(Nii(R=tpop(R$RRtresultt remainderstrem((sV/home/kaiyou/Projects/ndh2k12/challenges/wargame2k12/crypto/rsa/src/librsa/__init__.pytfast_exponentiationus   cCst|tjkr+tt|||S|dkr|tjtj|dtjtj|dkr|tdnt |||S(NiisThe message is too long( R+R,R.t getCiphertexttlongRR:R;t OverflowErrorRY(R?RR((sV/home/kaiyou/Projects/ndh2k12/challenges/wargame2k12/crypto/rsa/src/librsa/__init__.pyRZs BcCs9t|d}|j|j}|jt|S(Ntr(RtreadlineR t extractPubKey(tfilenameRtpubKey((sV/home/kaiyou/Projects/ndh2k12/challenges/wargame2k12/crypto/rsa/src/librsa/__init__.pyt getPubKeys    cCsd}tj|}d}}t}xm|D]e}|dkrGt}n|tkrl||krl||7}q,|tkr,||kr,||7}q,q,W||fS(Nt 0123456789R*t,(R RSRR(RatcharsettpubRRtmodulusR!((sV/home/kaiyou/Projects/ndh2k12/challenges/wargame2k12/crypto/rsa/src/librsa/__init__.pyR_s     cCs)t|d}|j}|j|S(NR](RtreadR (R`RR((sV/home/kaiyou/Projects/ndh2k12/challenges/wargame2k12/crypto/rsa/src/librsa/__init__.pyt getMessages  cCsat|}t|dd}|jdt|t|t|tdd|jdS(Ns.ascRs----- BEGIN MESSAGE ----- s s----- END MESSAGE ----- (RiRR RLRRZR (RRR`R?R((sV/home/kaiyou/Projects/ndh2k12/challenges/wargame2k12/crypto/rsa/src/librsa/__init__.pytencrypts  &cCs9t|d}|j|j}|jt|S(NR](RR^R textractPrivKey(R`RtprivKey((sV/home/kaiyou/Projects/ndh2k12/challenges/wargame2k12/crypto/rsa/src/librsa/__init__.pyt getPrivKeys    cCsd}tj|}d}}t}xm|D]e}|dkrGt}n|tkrl||krl||7}q,|tkr,||kr,||7}q,q,W||fS(NRcR*Rd(R RSRR(RlRetprivRRRgR!((sV/home/kaiyou/Projects/ndh2k12/challenges/wargame2k12/crypto/rsa/src/librsa/__init__.pyRks     cCsZt|d}|j}|j|jd}|tddtdd !}|S(NR]s s----- BEGIN MESSAGE -----is----- END MESSAGE -----(RRhR trstripR9(R`RR((sV/home/kaiyou/Projects/ndh2k12/challenges/wargame2k12/crypto/rsa/src/librsa/__init__.pytgetUnknownMessages   "cCst|||S(N(RZ(R?RR((sV/home/kaiyou/Projects/ndh2k12/challenges/wargame2k12/crypto/rsa/src/librsa/__init__.pyt getPlaintextscCsId}t|}x0|D](}||||}|t|7}qW|S(NR*(RTR2(RPR@RRAR?tcparttmpart((sV/home/kaiyou/Projects/ndh2k12/challenges/wargame2k12/crypto/rsa/src/librsa/__init__.pyt gluechopss   cCsZt|}t|jdd}|jt|t|t|t|jdS(Ns.ascR(RpRRoR RtRRqR (RRR`R?R((sV/home/kaiyou/Projects/ndh2k12/challenges/wargame2k12/crypto/rsa/src/librsa/__init__.pytdecrypts (( RRRRMR R,tcPickleRRRRRRR#RR2R8RLR>RTRYRZRbR_RiRjRmRkRpRqRtRu(((sV/home/kaiyou/Projects/ndh2k12/challenges/wargame2k12/crypto/rsa/src/librsa/__init__.pyts0H                  librsa/__init__.py0000664000175000017500000001122611765660544014051 0ustar kaiyoukaiyou# librsa # by Benjamin Randazzo - 2012 # based on Sybren Stuvel, Marloes de Boer and Ivo Tamboer work (2009) #!/bin/python import random, time, math, zlib, base64, types from cPickle import dumps, loads def genkey(name): print ". . .", (n, e, p, q) = genPubKey() time.sleep(3) print ". . .", d = genPrivKey(e, p, q) # write public key f = open(name+".pub", 'w') f.write('----- BEGIN PUBLIC KEY -----\n'+ base64.encodestring("(n = "+str(n)+", e = "+str(e)+")")+ '----- END PUBLIC KEY -----\n') f.close() # write private key f = open(name+".priv", 'w') f.write('----- BEGIN PRIVATE KEY -----\n'+ base64.encodestring("(n = "+str(n)+", d = "+str(d)+")")+ '----- END PRIVATE KEY -----\n') f.close() def genPubKey(): p = random.random() p *= 10**8 p = nextPrime(int(p)) q = random.random() q *= 10**8 q = nextPrime(int(q)) n = p*q e = 101 print "+ + +", return (n, e, p, q) def genPrivKey(e, p, q): m = (p-1)*(q-1) print "+ + +", return mul_inv(e, m) def nextPrime(n): isPrime = False while (not isPrime): n = n+1 limit = int(math.sqrt(n)+1) i = 2 while (i < limit and n%i != 0): i = i+1 if i == limit: isPrime = True return n def bezout(a, b): if a == 0 and b == 0: return (0, 0, 0) if b == 0: return (a/abs(a), 0, abs(a)) (u, v, p) = bezout(b, a%b) return (v, (u - v*(a/b)), p) def mul_inv(x, m): (u, _, p) = bezout(x, m) if p == 1: return u%abs(m) def int2bytes(n): if not (type(n) is types.LongType or type(n) is types.IntType): raise TypeError("You must pass a long or an int") string = "" while n > 0: string = "%s%s" % (chr(n & 0xFF), string) n /= 256 return string def bytes2int(bytes): integer = 0 for byte in bytes: integer *= 256 if type(byte) is types.StringType: byte = ord(byte) integer += byte return integer def chopstring(message, key, n, funcref): msglen = len(message) mbits = msglen*8 nbits = int(math.floor(math.log(n, 2))) nbytes = nbits/8 blocks = msglen/nbytes if msglen%nbytes > 0: blocks += 1 cipher = [] for bindex in range(blocks): offset = bindex*nbytes block = message[offset:offset+nbytes] value = bytes2int(block) cipher.append(funcref(value, key, n)) return picklechops(cipher) def picklechops(chops): value = zlib.compress(dumps(chops)) encoded = base64.encodestring(value) return encoded.strip() def unpicklechops(string): return loads(zlib.decompress(base64.decodestring(string))) def fast_exponentiation(a, p, n): result = a%n remainders = [] while p != 1: remainders.append(p & 1) p = p >> 1 while remainders: rem = remainders.pop() result = ((a ** rem) * result ** 2) % n return result def getCiphertext(message, e, n): if type(message) is types.IntType: return getCiphertext(long(message), e, n) if message > 0 and math.floor(math.log(message, 2)) > math.floor(math.log(n, 2)): raise OverflowError("The message is too long") return fast_exponentiation(message, e, n) def getPubKey(filename): f = open(filename, 'r') f.readline() pubKey = f.readline() f.close() return extractPubKey(pubKey) def extractPubKey(pubKey): charset = '0123456789' pub = base64.decodestring(pubKey) n = e = '' modulus = True for i in pub: if (i == ','): modulus = False if (modulus == True and i in charset): n += i elif (modulus == False and i in charset): e += i return (n, e) def getMessage(filename): f = open(filename, 'r') m = f.read() f.close() return m def encrypt(n, e, filename): message = getMessage(filename) f = open(filename+'.asc', 'w') f.write('----- BEGIN MESSAGE -----\n'+ chopstring(message, int(e), int(n), getCiphertext)+'\n'+ '----- END MESSAGE -----\n') f.close() def getPrivKey(filename): f = open(filename, 'r') f.readline() privKey = f.readline() f.close() return extractPrivKey(privKey) def extractPrivKey(privKey): charset = '0123456789' priv = base64.decodestring(privKey) n = d = '' modulus = True for i in priv: if (i == ','): modulus = False if (modulus == True and i in charset): n += i elif (modulus == False and i in charset): d += i return (n, d) def getUnknownMessage(filename): f = open(filename, 'r') m = f.read() f.close() m = m.rstrip('\n') m = m[len('----- BEGIN MESSAGE -----')+1:-(len('----- END MESSAGE -----')+1)] return m def getPlaintext(message, d ,n): return getCiphertext(message, d, n) def gluechops(chops, key, n, funcref): message = "" chops = unpicklechops(chops) for cpart in chops: mpart = funcref(cpart, key, n) message += int2bytes(mpart) return message def decrypt(n, d, filename): message = getUnknownMessage(filename) f = open(filename.rstrip('.asc'), 'w') f.write(gluechops(message, int(d), int(n), getPlaintext)) f.close() decrypt.py0000664000175000017500000000031111765660544012501 0ustar kaiyoukaiyou#!/bin/python import sys, librsa if (len(sys.argv) < 3): sys.exit("usage: python decrypt.py privateKey message") (n, d) = librsa.getPrivKey(str(sys.argv[1])) librsa.decrypt(n, d, str(sys.argv[2])) flag.asc0000664000175000017500000000067211771317166012064 0ustar kaiyoukaiyou----- BEGIN MESSAGE ----- eJw1krt1HUEMQ3NVosiHIMFfD68J5wrcf2TM7Crce0gQwOz3zz98fRCgkT5DEjWfr7+fjU6rxWA2 3A+CO6NsTN9cuygYHWRsrTHzMG/mTld1Gwd3DB5M93SaC98DgslEc3zuIrgENlZkgwd12HDHNPkr JaMdbr0zI3xYdSZg1dDdfkzMSjyHxZ6IZ1POfGxNGQIP89J1erlVbV+2x49ceBjsHcPRC3SHwj3W FN3G4TgpM2+myqytE7QLbyhjWA3YYDheVqMOvWRNk/U4Wa5i9God9cx5+8LpUtUTzVNSri6G9xr6 dac4Mh8qWW9xV1Mtekjw+O23uUrEdkVQD33Vasow0yrEG++BU5oKVgIp4j2qh1HfEaN/oMX+/AcX TXp2 ----- END MESSAGE ----- john.pub0000664000175000017500000000014711771317166012126 0ustar kaiyoukaiyou----- BEGIN PUBLIC KEY ----- KG4gPSAxNTQ5Mzg4MzAyOTk5NTE5LCBlID0gMTAxKQ== ----- END PUBLIC KEY ----- README0000664000175000017500000000030711765660544011342 0ustar kaiyoukaiyouEn sniffant le réseau HZV vous avez intercepté un message chiffré destiné à un gars appelé John et sa clé publique. Ce message contient sans doute des informations intéressantes pour vous.