Skip to content

Commit

Permalink
Merge branch 'master' into stable
Browse files Browse the repository at this point in the history
  • Loading branch information
tigertv committed Jul 27, 2021
2 parents b480ec7 + 7762dd4 commit b504495
Show file tree
Hide file tree
Showing 81 changed files with 2,032 additions and 1,254 deletions.
34 changes: 17 additions & 17 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -21,22 +21,20 @@ Description

SecretPy is a cryptographic Python package. It uses the following classical cipher algorithms:

- Affine
- Atbash
- Bazeries
- Beaufort
- Caesar, Caesar Progressive
- Chaocipher
- Keyword
- Playfair, Two Square(Double Playfair), Three Square, Four Square
- Polybius, ADFGX, ADFGVX, Bifid, Trifid, Nihilist
- Rot13, Rot5, Rot18, Rot47
- Scytale
- Simple Substitution
- Transposition
- Columnar, Myszkowski, Zigzag(Railfence)
- Vic
- Vigenere, Autokey, Gronsfeld, Porta
* Affine
* Atbash
* Bazeries
* Beaufort
* Caesar, Caesar Progressive
* Chaocipher
* Keyword
* Playfair, Two Square(Double Playfair), Three Square, Four Square
* Polybius, ADFGX, ADFGVX, Bifid, Trifid, Nihilist
* Rot13, Rot5, Rot18, Rot47
* Simple Substitution
* Transposition: Columnar, Scytale, Spiral, Myszkowski, Zigzag(Railfence)
* Vic
* Vigenere, Autokey, Gronsfeld, Porta

Installation
============
Expand Down Expand Up @@ -110,7 +108,7 @@ The cipher classes can encrypt only characters which exist in the alphabet, and
CryptMachine
------------

``CryptMachine`` saves a state. There are alphabet, key and cipher, they can be changed in anytime.
``CryptMachine`` saves a state. There are alphabet, key and cipher, they can be changed at anytime.
In the previous example, plaintext contains only characters existing in the alphabet i.e. without spaces and etc.
To change the behaviour, you can use ``CryptMachine`` and decorators(``SaveAll``, ``Block``), so it's a preferred way to do encryption/decryption:

Expand All @@ -128,6 +126,8 @@ To change the behaviour, you can use ``CryptMachine`` and decorators(``SaveAll``
print(machine.decrypt(enc))
key = 3
cipher = Caesar()
cm0 = CryptMachine(cipher, key)
cm = cm0
Expand Down
2 changes: 1 addition & 1 deletion docs/api.rst
Original file line number Diff line number Diff line change
Expand Up @@ -269,7 +269,7 @@ Examples
.. literalinclude:: ../examples/rot47.py
:linenos:

Simple Substitution
Scytale
===================
.. autoclass:: secretpy.Scytale
:members:
Expand Down
10 changes: 5 additions & 5 deletions docs/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -196,8 +196,8 @@
# (source start file, target name, title,
# author, documentclass [howto, manual, or own class]).
latex_documents = [
('index', 'ReadtheDocsTemplate.tex', u'SecretPy Documentation',
u'Read the Docs', 'manual'),
('index', 'ReadtheDocsTemplate.tex', u'SecretPy Documentation',
u'Read the Docs', 'manual'),
]

# The name of an image file (relative to this directory) to place at the top of
Expand Down Expand Up @@ -240,9 +240,9 @@
# (source start file, target name, title, author,
# dir menu entry, description, category)
texinfo_documents = [
('index', 'ReadtheDocsTemplate', u'SecretPy Documentation',
u'Read the Docs', 'SecretPyTemplate', 'SecretPy is a Python package of classical ciphers.',
'Miscellaneous'),
('index', 'ReadtheDocsTemplate', u'SecretPy Documentation',
u'Read the Docs', 'SecretPyTemplate', 'SecretPy is a Python package of classical ciphers.',
'Miscellaneous'),
]

# Documents to append as an appendix to all manuals.
Expand Down
57 changes: 43 additions & 14 deletions examples/adfgvx.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
# -*- encoding: utf-8 -*-

from secretpy import ADFGVX, CryptMachine
from secretpy.cmdecorators import Block, UpperCase


def encdec(machine, plaintext):
Expand All @@ -12,23 +13,51 @@ def encdec(machine, plaintext):
print("-------------------------------")


key = "cargo"
cm = CryptMachine(ADFGVX(), key)
key = "privacy"
cm = UpperCase(Block(CryptMachine(ADFGVX(), key), 4))

alphabet = [
u"f", u"n", u"h", u"e", u"q", u"0",
u"r", u"d", u"z", u"o", u"c", u"9",
u"ij", u"s", u"a", u"g", u"u", u"8",
u"b", u"v", u"k", u"p", u"w", u"7",
u"x", u"m", u"y", u"t", u"l", u"6",
u"1", u"2", u"3", u"4", u"5", u".",
]
alphabet = (
u"n", u"a", u"1", u"c", u"3", u"h",
u"8", u"t", u"b", u"2", u"o", u"m",
u"e", u"5", u"w", u"r", u"p", u"d",
u"4", u"f", u"6", u"g", u"7", u"i",
u"9", u"j", u"0", u"k", u"l", u"q",
u"s", u"u", u"v", u"x", u"y", u"z",
)
cm.set_alphabet(alphabet)
key = "battle"
plaintext = "attackatdawn11.25"
plaintext = "Attack at 12.00am!"
encdec(cm, plaintext)

key = "deutsch"
key = "pangram"
cm.set_key(key)
plaintext = "howstuffworks"
plaintext = "The quick brown fox jumps over the lazy dog."
encdec(cm, plaintext)

alphabet = (
u"5", u"б", u"в", u"г", u"д", u"её",
u"ж", u"з", u"ий", u"к", u"л", u"м",
u"н", u"о", u"п", u"р", u"с", u"т",
u"у", u"ф", u"х", u"ц", u"ч", u"ш",
u"щ", u"ы", u"ьъ", u"э", u"ю", u"я",
u"0", u"1", u"2", u"3", u"4", u"а",
)
cm.set_alphabet(alphabet)
key = "russian"
cm.set_key(key)
plaintext = u"Эй, жлоб! Где туз? Прячь юных съёмщиц в шкаф."
encdec(cm, plaintext)

'''
Attack at 12.00am!
DGDD DAGD DGAF ADDF DADV DVFA ADVX
ATTACKAT1200AM
-------------------------------
The quick brown fox jumps over the lazy dog.
DXGF VDVD VFAA GGDX AFXG XGFA GFFA DDVG DDXA FAXG ADDF XXXD AXDX VVDD DGVV FXFA VVFX XV
THEQUICKBROWNFOXJUMPSOVERTHELAZYDOG
-------------------------------
Эй, жлоб! Где туз? Прячь юных съёмщиц в шкаф.
AAXF FGXG GDDF FVDA FDDG GGVF DGXV VAAV VFXA XDDA DGAV AGDF AXFV VFDX GFVD XFVV FG
ЭИЖЛОБГДЕТУЗПРЯЧЬЮНЫХСЬЕМЩИЦВШКАФ
-------------------------------
'''
134 changes: 99 additions & 35 deletions examples/adfgx.py
Original file line number Diff line number Diff line change
@@ -1,55 +1,119 @@
#!/usr/bin/python
# -*- encoding: utf-8 -*-

from secretpy import ADFGX
from secretpy import ADFGX, CryptMachine, alphabets as al
from secretpy.cmdecorators import SaveAll, Block, UpperCase

alphabet = [
u"b", u"t", u"a", u"l", u"p", u"d", u"h", u"o", u"z", u"k", u"q",
u"f", u"v", u"s", u"n", u"g", u"ij", u"c", u"u", u"x", u"m", u"r",
u"e", u"w", u"y"
]

plaintext = u"attackatonce"
alphabet = (
u"b", u"t", u"a", u"l", u"p",
u"d", u"h", u"o", u"z", u"k",
u"q", u"f", u"v", u"s", u"n",
u"g", u"ij", u"c", u"u", u"x",
u"m", u"r", u"e", u"w", u"y"
)
key = "cargo"
cipher = ADFGX()
plaintext = u"attackatonce"

print(plaintext)
enc = cipher.encrypt(plaintext, key, alphabet)
print(enc)
print(cipher.decrypt(enc, key, alphabet))
print("----------------------------------")

dec = cipher.decrypt(enc, key, alphabet)
print(dec)

##############################################################################
print("-------------------------------")
def encdec(machine, plaintext):
print(plaintext)
enc = machine.encrypt(plaintext)
print(enc)
print(machine.decrypt(enc))
print("----------------------------------")

alphabet = [
u"f", u"n", u"h", u"e", u"q",
u"r", u"d", u"z", u"o", u"c",
u"ij", u"s", u"a", u"g", u"u",
u"b", u"v", u"k", u"p", u"w",
u"x", u"m", u"y", u"t", u"l"
]
key = "battle"
plaintext = "attackatdawn"

print(plaintext)
enc = cipher.encrypt(plaintext, key, alphabet)
print(enc)
cm0 = CryptMachine(cipher, key)
cm = UpperCase(Block(cm0))
cm.set_alphabet(alphabet)
plaintext = u"Attack at once!"
encdec(cm, plaintext)

dec = cipher.decrypt(enc, key, alphabet)
print(dec)
cm1 = SaveAll(cm0)
plaintext = u"Attack at once!"
encdec(cm1, plaintext)

############################################################################
print("-------------------------------")
alphabet = al.GERMAN_SQUARE
key = "german"
cm.set_key(key)
cm.set_alphabet(alphabet)
plaintext = u"Schweißgequält vom öden Text zürnt Typograf Jakob"
encdec(cm, plaintext)

key = "deutsch"
plaintext = "howstuffworks"
alphabet = (
u"uúů", u"aá", u"b", u"cč", u"dď",
u"f", u"g", u"h", u"iíj", u"zž",
u"k", u"l", u"m", u"nň", u"oó",
u"p", u"q", u"rř", u"sš", u"tť",
u"v", u"w", u"x", u"yý", u"eéě",
)
key = "czech"
cm.set_key(key)
cm.set_alphabet(alphabet)
plaintext = u"Nechť již hříšné saxofony ďáblů rozezvučí síň úděsnými tóny waltzu, tanga a quickstepu."
encdec(cm, plaintext)

# use default english alphabet 5x5
print(plaintext)
enc = cipher.encrypt(plaintext, key)
print(enc)
alphabet = (
u"zž", u"aáä", u"b", u"cč", u"dď",
u"eé", u"f", u"g", u"h", u"iíj",
u"k", u"lĺľ", u"m", u"oóô", u"p",
u"q", u"rŕ", u"sš", u"tť", u"uú",
u"v", u"w", u"x", u"yý", u"nň",
)
key = "slovak"
cm.set_key(key)
cm.set_alphabet(alphabet)
plaintext = u"Vypätá dcéra grófa Maxwella s IQ nižším ako kôň núti čeľaď hrýzť hŕbu jabĺk."
encdec(cm, plaintext)

alphabet = (
u"αά", u"β", u"γ", u"δ", u"εέ",
u"ζ", u"ηή", u"θ", u"ιί", u"κ",
u"λ", u"μ", u"ν", u"ξ", u"οό",
u"π", u"ρ", u"σς", u"τ", u"υύ",
u"φ", u"χ", u"ψ", u"ωώ", u"",
)
key = "greek"
cm.set_key(key)
cm.set_alphabet(alphabet)
plaintext = u"Ξεσκεπάζω την ψυχοφθόρα σας βδελυγμία."
encdec(cm, plaintext)

dec = cipher.decrypt(enc, key)
print(dec)
'''
attackatonce
faxdfadddgdgfffafaxafafx
attackatonce
----------------------------------
Attack at once!
FAXDF ADDDG DGFFF AFAXA FAFX
ATTACKATONCE
----------------------------------
Attack at once!
Faxdfa dd dgdg!fffafaxafafx
Attack at once!
----------------------------------
Schweißgequält vom öden Text zürnt Typograf Jakob
DDAAX FFXGG FGDFF DFAAG GGGDG GAADG XGGFF AGGGG FAAAF XDXGD XXXFG DAXFG XAAGF FXGXD GGAAD GGFAA XFXDD D
SCHWEISGEQUALTVOMODENTEXTZURNTTYPOGRAFIAKOB
----------------------------------
Nechť již hříšné saxofony ďáblů rozezvučí síň úděsnými tóny waltzu, tanga a quickstepu.
FGDXD GAXFX FFXAD GAGFX XDDXD DDAXA XGGGG GFFGA ADXAG AXXGF DGAFD AGGAX FDFGX XAXDA XDAGG XGDXX DADAD AGGAX DFFGF XAFGX XGDAG GGGAX GGAAF XAGDG DGXDD GADFX AGFXF FFGFX ADGGG X
NECHTIIZHRISNESAXOFONYDABLUROZEZVUCISINUDESNYMITONYWALTZUTANGAAQUICKSTEPU
----------------------------------
Vypätá dcéra grófa Maxwella s IQ nižším ako kôň núti čeľaď hrýzť hŕbu jabĺk.
FADDD ADAGA FFXGD AXDGA XDAFD DADAA FGXGA XGGXF ADXDD DFDFX FDAXX DGADX DXGAA FFXFD DDFFG AAGGA AFXAA GGAXF GXGAF XDFDA GDFGG GDGFD DXXXA GXGDD GFDA
VYPATADCERAGROFAMAXWELLASIQNIZSIMAKOKONNUTICELADHRYZTHRBUIABLK
----------------------------------
Ξεσκεπάζω την ψυχοφθόρα σας βδελυγμία.
AXAGF XXXGF AXDXA AGFXA GFAXA GFFGA DFFFA AAAFA GXDGX DDDAD FFAGD AXDGX FAGGG D
ΞΕΣΚΕΠΑΖΩΤΗΝΨΥΧΟΦΘΟΡΑΣΑΣΒΔΕΛΥΓΜΙΑ
----------------------------------
'''
Loading

0 comments on commit b504495

Please sign in to comment.