Git Product home page Git Product logo

secretpy's Introduction

SecretPy

Go to PyPi Go to PyPi Go to PyPi Read the Docs Go to PyPi Go to Github Go 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/secretpy
cd secretpy
make install

Usage

Direct way

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

from secretpy import Caesar, alphabets as al


def encdec(cipher, plaintext, key, alphabet=al.ENGLISH):
    print('========================================================================================')
    print(plaintext)
    enc = cipher.encrypt(plaintext, key, alphabet)
    print(enc)
    print(cipher.decrypt(enc, key, alphabet))


key = 3
cipher = Caesar()

plaintext = u"thequickbrownfoxjumpsoverthelazydog"
encdec(cipher, plaintext, key)

alphabet = al.GERMAN
plaintext = u"schweißgequältvomödentextzürnttypografjakob"
encdec(cipher, plaintext, key, alphabet)

alphabet = al.SWEDISH
plaintext = u"faqomschweizklövdutrångpjäxby"
encdec(cipher, plaintext, key, alphabet)

'''
Output:

========================================================================================
thequickbrownfoxjumpsoverthelazydog
wkhtxlfneurzqiramxpsvryhuwkhodcbgrj
thequickbrownfoxjumpsoverthelazydog
========================================================================================
schweißgequältvomödentextzürnttypografjakob
vfkzhlcjhtxßowyrpaghqwhäwübuqwwösrjudimdnre
schweißgequältvomödentextzürnttypografjakob
========================================================================================
faqomschweizklövdutrångpjäxby
idtrpvfkzhlö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 use CryptMachine and decorators(SaveAll, Block), so it's a preferred way to do encryption/decryption:

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


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


key = 3
cipher = Caesar()
cm0 = CryptMachine(cipher, key)

cm = cm0
cm.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!
lgrqworyhqrqdoskdehwfkdudfwhuvlzloouhpryhdooriwkhpjuhdw
idontlovenonalphabetcharactersiwillremoveallofthemgreat
--------------------------------------------------------------------
This text is divided by blocks of length 5!
wklvw-hawlv-glylg-hgebe-orfnv-riohq-jwk
thistextisdividedbyblocksoflength
--------------------------------------------------------------------
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 use CompositeMachine:

from secretpy import Rot13, Caesar, CryptMachine, CompositeMachine
from secretpy.cmdecorators import SaveAll


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


key = 5
plaintext = 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 times
Itl ozrux ktzw ynrjx fsi hfy xnc ynrjx
Vgy bmehk xgmj laewk sfv usl kap laewk
=======================================
Vgy bmehk xgmj laewk sfv usl kap laewk
=======================================
Dog jumps four times and cat six times
Vgy bmehk xgmj laewk sfv usl kap laewk
Dog jumps four times and cat six times
=======================================
Dog jumps four times and cat six times
Nyq tewzc pyeb dswoc kxn mkd csh dswoc
Dog jumps four times and cat six times

'''

Maintainers

secretpy's People

Contributors

luminouslizard avatar tigertv avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

secretpy's Issues

Error when using a space in vigenere cipher

i looked through your code and its really solid, the problem is whenver my ciphertext has a space, the code raises a KeyError exception and it says 'cant find the character ' ' in alphabet!

Error when i using affine cipher

File "D:\Myprojects\TestEncryptions\test.py", line 101, in affine_encrypt
return Affine().encrypt('cool','12')
File "D:\Myprojects\TestEncryptions\secretpy\ciphers\affine.py", line 26, in encrypt
subst = {c: alphabet[(i * a + b) % len(alphabet)][0] for i, letters in enumerate(alphabet) for c in letters}
File "D:\Myprojects\TestEncryptions\secretpy\ciphers\affine.py", line 26, in
subst = {c: alphabet[(i * a + b) % len(alphabet)][0] for i, letters in enumerate(alphabet) for c in letters}
TypeError: not all arguments converted during string formatting

I tried affine cipher. Python 3.10.2
Text: cool
Key: "12" (str)

Autokey broken?

from secretpy import Autokey
from secretpy import CryptMachine
from secretpy.cmdecorators import UpperCase, SaveSpaces, NoSpaces
from secretpy import alphabets

cipher = Autokey()
key = "test"
msg = "this is a secret message"

cm = CryptMachine(cipher, key)
out = cm.encrypt(msg)

print(out)

This should work, but it doesn't for some reason... Shouldn't the autokey "key" be a string instead of an integer? It's documented as an integer.

can only concatenate str (not "bytes") to str

plaintext = u"string"

Works fine

Problem

What if we have to ask plaintext from user, how can we unicode it now.
Python 3.x does not support unicode() function

My Code

cipher = secretpy.Vigenere()

string = input("Enter Plaintext")
key = input("Enter Key")

def encrypt_vigenere(string,key):
    encrypted_string = cipher.encrypt(string,key)
    return encrypted_string

encrypt_vigenere(string,key)

Error

File "/usr/local/lib/python3.8/dist-packages/secretpy/ciphers/vigenere.py", line 43, in encrypt
    return self.__encDec(alphabet, key, text, 1)
File "/usr/local/lib/python3.8/dist-packages/secretpy/ciphers/vigenere.py", line 19, in __encDec
    raise Exception("Can't find char '" + wrchar + "' of text in alphabet!")
TypeError: can only concatenate str (not "bytes") to str

Non-existing character in an alphabet

Conclusions from #2:
Handle exceptions when a character doesn't exist in an alphabet.
A solution would be either to show an error message or remove non-existing characters from message.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.