- #!/usr/bin/python
- # -*- coding: iso8859-1 -*-
-
- '''
- Module d'encodage & decodage
- Jule Cesar & Carrer de Viginaire
- Coded by Marnage
-
- Usage :
-
- from crypto import encode,decode
-
- e = encode("cesar",1,"aA Bb Cc Yy Zz 09 ?!")
- print e
-
- e = decode("cesar",1,e)
- print e
-
-
- e = encode("viginaire",123,"aA Bb Cc Yy Zz 09 ?!")
- print e
-
- e = decode("viginaire",123,e)
- print e
-
- '''
-
- from sys import stdout,exit
-
- __all__ = "encode,decode"
-
- class Variable:
- def __init__(self,txt,key):
- self.txt = txt
- self.key = key
- self.txt_crypt = ""
- self.txt_decrypt = ""
- if (type(key) != int ):
- try: self.key = int(key)
- except :
- stdout.write("[!] Erreur : key != type int , conversion impossible\n")
- exit(0)
- if (type(txt) != str ):
- try: self.key = str(key)
- except :
- stdout.write("[!] Erreur : texte != type char , conversion impossible\n")
- exit(0)
-
-
- class Cesar(Variable):
- def __init__(self,txt,key):
- Variable.__init__(self,txt,key)
-
-
- def Encode(self):
- i = 0
- while ( i < len(self.txt) ):
- num = ord(self.txt[i])+self.key
- self.txt_crypt += chr(num)
- i += 1
-
- def Decode(self):
- i = 0
- while ( i < len(self.txt) ):
- num = ord(self.txt[i])-self.key
- self.txt_decrypt += chr(num)
- i += 1
-
- class Viginaire(Variable):
- def __init__(self,txt,key):
- Variable.__init__(self,txt,key)
-
- def Encode(self):
- i = 0
- i_key = 0
- while ( i < len(self.txt) ):
- if ( i_key == len(str(self.key)) ):
- i_key = 0
- num = ord(self.txt[i])
- char_key = str(self.key)
- buffer_key = char_key[i_key]
- num += int(buffer_key)
- self.txt_crypt += chr(num)
- i += 1
- i_key += 1
-
- def Decode(self):
- i = 0
- i_key = 0
- while ( i < len(self.txt) ):
- if ( i_key == len(str(self.key)) ):
- i_key = 0
- num = ord(self.txt[i])
- char_key = str(self.key)
- buffer_key = char_key[i_key]
- num -= int(buffer_key)
- self.txt_decrypt += chr(num)
- i += 1
- i_key += 1
-
- def encode(typ,key,txt):
- if ( typ == "cesar" ):
- Class = Cesar(txt,key)
- Class.Encode()
- elif ( typ == "viginaire" ):
- Class = Viginaire(txt,key)
- Class.Encode()
- else :
- return "[!] argument inconnu !"
- return Class.txt_crypt
-
-
- def decode(typ,key,txt):
- if ( typ == "cesar" ):
- Class = Cesar(txt,key)
- Class.Decode()
- elif ( typ == "viginaire" ):
- Class = Viginaire(txt,key)
- Class.Decode()
- else :
- return "[!] argument inconnu"
- return Class.txt_decrypt
#!/usr/bin/python
# -*- coding: iso8859-1 -*-
'''
Module d'encodage & decodage
Jule Cesar & Carrer de Viginaire
Coded by Marnage
Usage :
from crypto import encode,decode
e = encode("cesar",1,"aA Bb Cc Yy Zz 09 ?!")
print e
e = decode("cesar",1,e)
print e
e = encode("viginaire",123,"aA Bb Cc Yy Zz 09 ?!")
print e
e = decode("viginaire",123,e)
print e
'''
from sys import stdout,exit
__all__ = "encode,decode"
class Variable:
def __init__(self,txt,key):
self.txt = txt
self.key = key
self.txt_crypt = ""
self.txt_decrypt = ""
if (type(key) != int ):
try: self.key = int(key)
except :
stdout.write("[!] Erreur : key != type int , conversion impossible\n")
exit(0)
if (type(txt) != str ):
try: self.key = str(key)
except :
stdout.write("[!] Erreur : texte != type char , conversion impossible\n")
exit(0)
class Cesar(Variable):
def __init__(self,txt,key):
Variable.__init__(self,txt,key)
def Encode(self):
i = 0
while ( i < len(self.txt) ):
num = ord(self.txt[i])+self.key
self.txt_crypt += chr(num)
i += 1
def Decode(self):
i = 0
while ( i < len(self.txt) ):
num = ord(self.txt[i])-self.key
self.txt_decrypt += chr(num)
i += 1
class Viginaire(Variable):
def __init__(self,txt,key):
Variable.__init__(self,txt,key)
def Encode(self):
i = 0
i_key = 0
while ( i < len(self.txt) ):
if ( i_key == len(str(self.key)) ):
i_key = 0
num = ord(self.txt[i])
char_key = str(self.key)
buffer_key = char_key[i_key]
num += int(buffer_key)
self.txt_crypt += chr(num)
i += 1
i_key += 1
def Decode(self):
i = 0
i_key = 0
while ( i < len(self.txt) ):
if ( i_key == len(str(self.key)) ):
i_key = 0
num = ord(self.txt[i])
char_key = str(self.key)
buffer_key = char_key[i_key]
num -= int(buffer_key)
self.txt_decrypt += chr(num)
i += 1
i_key += 1
def encode(typ,key,txt):
if ( typ == "cesar" ):
Class = Cesar(txt,key)
Class.Encode()
elif ( typ == "viginaire" ):
Class = Viginaire(txt,key)
Class.Encode()
else :
return "[!] argument inconnu !"
return Class.txt_crypt
def decode(typ,key,txt):
if ( typ == "cesar" ):
Class = Cesar(txt,key)
Class.Decode()
elif ( typ == "viginaire" ):
Class = Viginaire(txt,key)
Class.Decode()
else :
return "[!] argument inconnu"
return Class.txt_decrypt