2015-05-22 12:33:55 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
from enigmail import *
|
2015-05-23 20:21:42 +00:00
|
|
|
import getpass, sys, os
|
2015-05-22 12:33:55 +00:00
|
|
|
|
2015-05-23 20:21:42 +00:00
|
|
|
path = os.path.abspath( os.path.dirname(sys.argv[0]) );
|
2015-05-22 12:33:55 +00:00
|
|
|
|
2015-05-23 20:21:42 +00:00
|
|
|
# paramètres utilisateurs
|
|
|
|
conf = getConf(path);
|
|
|
|
|
|
|
|
if( conf == False ): # si manque des paramètres
|
|
|
|
print "parametres manquants";
|
|
|
|
raise SystemExit(0);
|
2015-05-22 12:33:55 +00:00
|
|
|
|
|
|
|
|
|
|
|
# DEFINITION DE L'ALPHABET
|
|
|
|
SIGMA = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'; # maj
|
|
|
|
SIGMA += 'abcdefghijklmnopqrstuvwxyz'; # min
|
|
|
|
SIGMA += '&=+^~@%,.?!:;[](){}-_#$*/ \\"\'\n'; # ponctuation + retour charriot
|
|
|
|
SIGMA += '0123456789'; # digit
|
|
|
|
SIGMA += 'éèêàùç'; # accents
|
|
|
|
SIGMA = SIGMA.decode('utf-8');
|
|
|
|
# ALPHABET FORMATE EN LISTE
|
|
|
|
SIGMA = list(SIGMA);
|
2015-05-23 15:00:52 +00:00
|
|
|
|
|
|
|
|
2015-05-22 12:33:55 +00:00
|
|
|
# CHOIX DE LA CLE
|
|
|
|
userkey = int( raw_input('Cle (hex ou int): '), 0);
|
2015-05-23 15:00:52 +00:00
|
|
|
|
|
|
|
# CALCUL de LEVEL en fonction de la clé (LEVEL = nombre de rotors)
|
2015-05-22 12:33:55 +00:00
|
|
|
LEVEL = calcLevel(userkey, SIGMA);
|
|
|
|
# DECOMPOSITION DE LA CLE PRIMAIRE EN CLES SECONDAIRES
|
|
|
|
KEY = decomposeKey(userkey, len(SIGMA), LEVEL);
|
|
|
|
# CREATION DES ROTORS EN FONCTION DES CLES SECONDAIRES
|
|
|
|
ROTOR = [];
|
|
|
|
ROTOR.append( [] );
|
2015-05-23 15:00:52 +00:00
|
|
|
# on cree les rotors grace a SIGMA et aux cles recuperees
|
2015-05-22 12:33:55 +00:00
|
|
|
for i in range(0, LEVEL):
|
|
|
|
ROTOR.append( shuffle( SIGMA, KEY[i]) ); # on creer le rotor et le melange suivant la cle
|
|
|
|
ROTOR[0].append( ROTOR[i+1][0] ); # on enregistre la l&ettre en premiere position dans la premiere entree du rotor
|
|
|
|
|
|
|
|
|
|
|
|
# AFFICHAGE DES ROTORS
|
|
|
|
# printRotors(ROTOR);
|
|
|
|
|
|
|
|
# OUVERTURE ET LECTURE DU FICHIER
|
2015-05-23 20:21:42 +00:00
|
|
|
inFile = open(path + '/../bucket-file', 'r');
|
2015-05-22 12:33:55 +00:00
|
|
|
m = inFile.read().decode('utf-8');
|
2015-05-22 14:09:49 +00:00
|
|
|
inFile.close();
|
2015-05-22 12:33:55 +00:00
|
|
|
|
2015-05-22 14:09:49 +00:00
|
|
|
# CHOIX DU TYPE (ENCODE+MAIL / ENCODE / DECODE)
|
2015-05-22 12:33:55 +00:00
|
|
|
type = '';
|
2015-05-22 14:09:49 +00:00
|
|
|
while( type != 'M' and type != 'C' and type != 'D' ):
|
2015-05-23 15:00:52 +00:00
|
|
|
type = ( raw_input('[M] Crypter et envoyer par mail\n[C] Crypter\n[D] Décrypter\n> ') ).upper();
|
|
|
|
print;
|
2015-05-22 12:33:55 +00:00
|
|
|
# VARIABLE DU HASH
|
|
|
|
M = '';
|
|
|
|
|
2015-05-23 15:00:52 +00:00
|
|
|
|
2015-05-22 14:09:49 +00:00
|
|
|
if( type == 'M' or type == 'C' ):
|
2015-05-23 15:00:52 +00:00
|
|
|
M = encodeStr(m, SIGMA, ROTOR); # ENCODAGE DU MESSAGE
|
|
|
|
else:
|
|
|
|
M = decodeStr(m, SIGMA, ROTOR); # DECODAGE DU MESSAGE
|
2015-05-22 12:33:55 +00:00
|
|
|
|
|
|
|
|
2015-05-23 15:00:52 +00:00
|
|
|
# ENVOI DU MAIL
|
2015-05-22 14:09:49 +00:00
|
|
|
if( type == 'M' ):
|
|
|
|
To = str( raw_input('Destinataire : ') );
|
|
|
|
Subj = str( raw_input('Objet : ') );
|
2015-05-23 15:00:52 +00:00
|
|
|
Pass = str( getpass.getpass('Mot de passe : ') );
|
|
|
|
print '...';
|
|
|
|
|
2015-05-23 20:21:42 +00:00
|
|
|
sendMail(conf, Pass, To, Subj, M);
|
2015-05-23 15:00:52 +00:00
|
|
|
|
2015-05-22 14:09:49 +00:00
|
|
|
# ECRITURE FICHIER
|
|
|
|
else:
|
2015-05-23 20:21:42 +00:00
|
|
|
outFile = open(path + '/../bucket-file', 'w');
|
2015-05-22 14:09:49 +00:00
|
|
|
outFile.write( M.encode('utf-8') );
|
|
|
|
outFile.close();
|