Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up

Classical ciphers: Caesar, ADFGX, ROT13 and etc.

License

NotificationsYou must be signed in to change notification settings

tigertv/secretpy

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Go to PyPiGo to PyPiGo to PyPiRead the DocsGo to PyPiGo to GithubGo to Travis

Download:

https://pypi.org/project/secretpy

Documentation:

https://secretpy.readthedocs.io

Source code & Development:

https://github.com/tigertv/secretpy

Description

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

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

Installation

To install this library, you can use pip:

pip install secretpy

Alternatively, you can install the package using the repo's cloning and the make:

git clone https://github.com/tigertv/secretpycd secretpymake install

Usage

Direct way

The cipher classes can encrypt only characters which exist in the alphabet, and they don't have a state.

fromsecretpyimportCaesar,alphabetsasaldefencdec(cipher,plaintext,key,alphabet=al.ENGLISH):print('========================================================================================')print(plaintext)enc=cipher.encrypt(plaintext,key,alphabet)print(enc)print(cipher.decrypt(enc,key,alphabet))key=3cipher=Caesar()plaintext=u"thequickbrownfoxjumpsoverthelazydog"encdec(cipher,plaintext,key)alphabet=al.GERMANplaintext=u"schweißgequältvomödentextzürnttypografjakob"encdec(cipher,plaintext,key,alphabet)alphabet=al.SWEDISHplaintext=u"faqomschweizklövdutrångpjäxby"encdec(cipher,plaintext,key,alphabet)'''Output:========================================================================================thequickbrownfoxjumpsoverthelazydogwkhtxlfneurzqiramxpsvryhuwkhodcbgrjthequickbrownfoxjumpsoverthelazydog========================================================================================schweißgequältvomödentextzürnttypografjakobvfkzhlcjhtxßowyrpaghqwhäwübuqwwösrjudimdnreschweißgequältvomödentextzürnttypografjakob========================================================================================faqomschweizklövdutrångpjäxbyidtrpvfkzhlönocygxwuaqjsmbåeäfaqomschweizklövdutrångpjäxby'''

CryptMachine

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 useCryptMachine and decorators(SaveAll,Block), so it's a preferred way to do encryption/decryption:

fromsecretpyimportCaesar,CryptMachine,alphabetsasalfromsecretpy.cmdecoratorsimportSaveAll,Blockdefencdec(machine,plaintext):print("--------------------------------------------------------------------")print(plaintext)enc=machine.encrypt(plaintext)print(enc)print(machine.decrypt(enc))key=3cipher=Caesar()cm0=CryptMachine(cipher,key)cm=cm0cm.set_alphabet(al.ENGLISH)plaintext="I don't love non-alphabet characters. I will remove all of them: ^,&@$~(*;?&#. Great!"encdec(cm,plaintext)cm=Block(cm,length=5,sep="-")plaintext="This text is divided by blocks of length 5!"encdec(cm,plaintext)cm=SaveAll(cm0)plaintext="I love non-alphabet characters. These are : ^,&@$~(*;?&#. That's it!"encdec(cm,plaintext)cm.set_alphabet(al.ENGLISH_SQUARE_IJ)plaintext="Jj becomes Ii because we use ENGLISH_SQUARE_IJ!"encdec(cm,plaintext)cm.set_alphabet(al.JAPANESE_HIRAGANA)cm.set_key(1)plaintext=u"text あい だやぎへぐゆぢ"encdec(cm,plaintext)'''Output:--------------------------------------------------------------------I don't love non-alphabet characters. I will remove all of them: ^,&@$~(*;?&#. Great!lgrqworyhqrqdoskdehwfkdudfwhuvlzloouhpryhdooriwkhpjuhdwidontlovenonalphabetcharactersiwillremoveallofthemgreat--------------------------------------------------------------------This text is divided by blocks of length 5!wklvw-hawlv-glylg-hgebe-orfnv-riohq-jwkthistextisdividedbyblocksoflength--------------------------------------------------------------------I love non-alphabet characters. These are : ^,&@$~(*;?&#. That's it!L oryh qrq-doskdehw fkdudfwhuv. Wkhvh duh : ^,&@$~(*;?&#. Wkdw'v lw!I love non-alphabet characters. These are : ^,&@$~(*;?&#. That's it!--------------------------------------------------------------------Jj becomes Ii because we use ENGLISH_SQUARE_IJ!Mm ehfrphv Mm ehfdxvh zh xvh HQKOMVL_VTXDUH_MM!Ii becomes Ii because we use ENGLISH_SQUARE_II!--------------------------------------------------------------------text あい だやぎへぐゆぢtext いう ぢゆぐほげよづtext あい だやぎへぐゆぢ'''

CompositeMachine

Combining several ciphers to get more complex cipher, you can useCompositeMachine:

fromsecretpyimportRot13,Caesar,CryptMachine,CompositeMachinefromsecretpy.cmdecoratorsimportSaveAlldefencdec(machine,plaintext):print("=======================================")print(plaintext)enc=machine.encrypt(plaintext)print(enc)dec=machine.decrypt(enc)print(dec)key=5plaintext=u"Dog jumps four times and cat six times"print(plaintext)cm1=SaveAll(CryptMachine(Caesar(),key))enc=cm1.encrypt(plaintext)print(enc)cm2=SaveAll(CryptMachine(Rot13()))enc=cm2.encrypt(enc)print(enc)print("=======================================")cm=CompositeMachine(cm1)cm.add_machines(cm2)enc=cm.encrypt(plaintext)print(enc)encdec(cm,plaintext)cm.add_machines(cm1,cm2)encdec(cm,plaintext)'''Output:Dog jumps four times and cat six timesItl ozrux ktzw ynrjx fsi hfy xnc ynrjxVgy bmehk xgmj laewk sfv usl kap laewk=======================================Vgy bmehk xgmj laewk sfv usl kap laewk=======================================Dog jumps four times and cat six timesVgy bmehk xgmj laewk sfv usl kap laewkDog jumps four times and cat six times=======================================Dog jumps four times and cat six timesNyq tewzc pyeb dswoc kxn mkd csh dswocDog jumps four times and cat six times'''

Maintainers


[8]ページ先頭

©2009-2025 Movatter.jp