Last active
August 29, 2015 14:04
-
-
Save niceboy120/a885aab28fb464fea19b to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
alpha = 'abcdefghijklmnopqrstuvwxyz' | |
#alpha=[chr(i) for i in range(ord('a'), ord('z')+1)] | |
def codageCesar(t,n,d): | |
resultat = '' | |
alpha2 = alpha[len(alpha)-d:]+alpha[:len(alpha)-d] | |
for lettre in t: | |
resultat = resultat + alpha2[alpha.index(lettre)] | |
return resultat | |
def decodageCesar(t,n,d): | |
resultat ='' | |
alpha2 = alpha[len(alpha)-d:]+alpha[:len(alpha)-d] | |
for lettre in t: | |
resultat = resultat + alpha[alpha2.index(lettre)] | |
return resultat | |
#### Fonction pour retourner la fréquénce d'apparition ###### | |
def frequences(tCrypter,n): | |
freq = [0] * 26 | |
for c in tCrypter : | |
if c in alpha : | |
freq[ord(c) - ord('a')] += 1 | |
#somme=sum(freq) | |
#freq=[v / somme * 1000.0 for v in freq] | |
#print(freq) | |
return freq | |
## Fonction décryptage auto | |
def decodageAuto(tCrypter,n): | |
freq = frequences(tCrypter,n) | |
indexmax=0 | |
resultat='' | |
for i in range(26): | |
if freq[i]>freq[indexmax]: | |
indexmax=i | |
cle= alpha.index('e')-indexmax | |
if cle <0: | |
cle = 26+cle | |
print("la clé est :" + str(cle)) | |
alpha2 = alpha[len(alpha)-cle:]+alpha[:len(alpha)-cle] | |
for lettre in tCrypter: | |
resultat = resultat + alpha[alpha2.index(lettre)] | |
return resultat | |
def creerCle(chaineCle): | |
longueurCle=len(chaineCle) | |
cleEntiers=[0]*longueurCle | |
chaineCle = chaineCle.lower() | |
i=0 | |
for i in range(longueurCle): | |
cleEntiers[i]=(ord(chaineCle[i]) - ord('a')) | |
return cleEntiers | |
def codageVigenere(t,n,c,k): | |
tCrypter="" | |
compteur=0 | |
while compteur < n: | |
i = compteur%k | |
char=ord(t[compteur])+c[i] | |
if char > ord('z'): | |
char -= 26 | |
#print(t[compteur] + "------>" + chr(char)) | |
tCrypter +=chr(char) | |
compteur += 1 | |
#print("Message crypté : " + tCrypter) | |
return tCrypter | |
def reste(a,b): | |
if a<b: | |
return a | |
else: | |
return reste(a-b,b) | |
def pgcd(a,b): | |
if b == 0: | |
return a | |
else: | |
return pgcd(b, reste(a,b)) | |
def pgcd2(a,b): | |
"""pgcd(a,b): calcul du 'Plus Grand Commun Diviseur' entre les 2 nombres entiers a et b""" | |
while b!=0: | |
a,b=b,a%b | |
return a | |
########### Code principal ############################## | |
######## Codage Cesar ################################### | |
message="informatiqueestunesciencequitraitelesinformationsdefaconautomatiqueavecunordinateur" | |
message=message.lower() | |
n=len(message) | |
d=3 | |
print("Message à crypter : ") | |
print(message) | |
print("**********************************************************") | |
crypter = codageCesar(message,n,d) | |
print("Message crypté : ") | |
print(crypter) | |
print("**********************************************************") | |
messagedecrypter = decodageCesar(crypter,n,d) | |
print("Message décrypté : " ) | |
print(messagedecrypter) | |
print("**********************************************************") | |
messagedecrypter=decodageAuto(crypter,n) | |
print("Message auto décrypté : " ) | |
print(messagedecrypter) | |
print("**********************************************************") | |
######## Codage Vigenere ################### | |
#cle="concours" | |
#message="ecolepolytechnique" | |
#n=len(message) | |
#cleEntiers= creerCle(cle) | |
#k=len(cleEntiers) | |
#codageVigenere(message,n,cleEntiers,k) | |
#print(str(pgcd2(56,42))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment