Beispiel #1
0
 def analyze_plain(self, plain, start = 0):
     """Devine une partie de la clef grace @plain"""
     i     = start
     key   = []
     index = 0
     while i < len(self.cipher):
         success  = False
         matchset = getitem(self.matches, i)
         for k, data in matchset.items():
             char = xor(self.cipher[i], k)
             if char == plain[index]:
                 key.append(k)
                 success = True
                 index  += 1
                 break
         if success == False:
             i    -= index
             index = 0
             key   = []
         elif len(key) == len(plain):
             break
         i += 1
     offset = i % self.keylen - len(key)
     for x in xrange(len(key)):
         offset += 1
         offset  = offset if offset >= 0 else self.keylen + offset
         offset  = offset % self.keylen
         print "[%02d]" % offset, fmt("['%c']" % key[x], WHITE),
     print
     if i != len(self.cipher):
         self.analyze_plain(plain, i + 1)
Beispiel #2
0
 def analyze_plain(self, plain, start=0):
     """Devine une partie de la clef grace @plain"""
     i = start
     key = []
     index = 0
     while i < len(self.cipher):
         success = False
         matchset = getitem(self.matches, i)
         for k, data in matchset.items():
             char = xor(self.cipher[i], k)
             if char == plain[index]:
                 key.append(k)
                 success = True
                 index += 1
                 break
         if success == False:
             i -= index
             index = 0
             key = []
         elif len(key) == len(plain):
             break
         i += 1
     offset = i % self.keylen - len(key)
     for x in xrange(len(key)):
         offset += 1
         offset = offset if offset >= 0 else self.keylen + offset
         offset = offset % self.keylen
         print "[%02d]" % offset, fmt("['%c']" % key[x], WHITE),
     print
     if i != len(self.cipher):
         self.analyze_plain(plain, i + 1)
Beispiel #3
0
 def _translate(cipher, charset, offset):
     """Decalle les caracteres de @offset"""
     plain = ""
     for c in cipher:
         if c in charset:
             plain += getitem(charset, charset.index(c) + offset)
         else:
             plain += c
     return plain
Beispiel #4
0
 def _caesar(cipher, offset):
     """Decalle les lettres de @offset"""
     plain = ""
     for c in cipher:
         if c.isalpha():
             plain += getitem(alphacharset(c), alphaindex(c) + offset)
         else:
             plain += c
     return plain
Beispiel #5
0
 def _translate(cipher, charset, offset):
     """Decalle les caracteres de @offset"""
     plain = ""
     for c in cipher:
         if c in charset:
             plain += getitem(charset, charset.index(c) + offset)
         else:
             plain += c
     return plain
Beispiel #6
0
 def _caesar(cipher, offset):
     """Decalle les lettres de @offset"""
     plain = ""
     for c in cipher:
         if c.isalpha():
             plain += getitem(alphacharset(c), alphaindex(c) + offset)
         else:
             plain += c
     return plain
Beispiel #7
0
def decrypt(cipher, key, charset=uppercase):
    """Dechiffrement"""
    plain = ""
    for c1, c2 in zip(cipher, key):
        if c1 in charset:
            i1 = charset.index(c1)
            i2 = charset.index(c2)
            plain += getitem(charset, i1 - i2)
        else:
            plain += c1
    return plain
Beispiel #8
0
def encrypt(plain, key, charset=uppercase):
    """Chiffrement"""
    cipher = ""
    for c1, c2 in zip(plain, key):
        if c1 in charset:
            i1 = charset.index(c1)
            i2 = charset.index(c2)
            cipher += getitem(charset, i1 + i2)
        else:
            cipher += c1
    return cipher
Beispiel #9
0
def decrypt(cipher, key, charset = uppercase):
    """Dechiffrement"""
    plain = ""
    for c1, c2 in zip(cipher, key):
        if c1 in charset:
            i1     = charset.index(c1)
            i2     = charset.index(c2)
            plain += getitem(charset, i1 - i2)
        else:
            plain += c1
    return plain
Beispiel #10
0
def encrypt(plain, key, charset = uppercase):
    """Chiffrement"""
    cipher = ""
    for c1, c2 in zip(plain, key):
        if c1 in charset:
            i1      = charset.index(c1)
            i2      = charset.index(c2)
            cipher += getitem(charset, i1 + i2)
        else:
            cipher += c1
    return cipher
Beispiel #11
0
def xor(text, key):
    """Fonction asm xor"""
    if isinstance(key, int):
        key = itos(key)
    return "".join(
        [chr(ord(c) ^ ord(getitem(key, i))) for i, c in enumerate(text)])