-
Notifications
You must be signed in to change notification settings - Fork 0
/
afine.py
executable file
·48 lines (40 loc) · 1.39 KB
/
afine.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
#! /usr/local/bin/python
#-*- coding: utf-8 -*-
__author__ = "Cedric Bonhomme"
__version__ = "$Revision: 0.1 $"
__date__ = "$Date: 2010/10/26 $"
__copyright__ = "Copyright (c) 2009-2010 Cedric Bonhomme"
__license__ = "GPL v3"
import string
import utils
def encrypt(message, cle):
"""
Afine encryption.
"""
return [(chr(((ord(i) - 65) * cle[0] + cle[1]) % 26 + 65)) for i in message]
def decrypt(cypher, cle):
"""
Afine decryption
"""
inv = utils.inv_modulo(cle[0], 26)
return [(chr(((ord(i) - 65 - cle[1]) * inv) % 26 + 65)) for i in cypher]
def brute_force(cypher):
""""Brute force
"""
valeurs_a = [1, 3, 5, 7, 9, 11, 15, 17, 19, 21, 23, 25]
valeurs_b = range(1, 26)
for i in valeurs_a:
for j in valeurs_b:
print (i, j)
print "".join(decrypt(cypher, (i, j)))
print
if __name__ == '__main__':
# Point of entry in execution mode
message ="LaventureaMelbournesesttermineebrutalementpourMarionBartoliLaFrancaiseopposeeaVeraZvonarevaen quartsdefinaledelOpendAustralienapasvulejourhormisendebutdepremiersetResultatunedefaiteendeuxmanches"
print "Cipher text:"
print "".join(encrypt(string.upper(message), (7, 3)))
print
print "Text:"
print "".join(decrypt("".join(encrypt(string.upper(message), (7, 3))), (7, 3)))
print
#brute_force("".join(encrypt(string.upper(message), (7, 3))))