HiveBrain v1.2.0
Get Started
← Back to all entries
patternpythonMinor

Silly Caesar encryption in Python

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
caesarencryptionsillypython

Problem

I made this variation of a Caesar cipher. Any tips on improving, either method or underlying algorithm? Don't worry... I promise not to use it to encrypt anything of value.

```
#########################
# #
# caesarED By: 1 0 3 E #
# 0 3 E 1 #
# 3 E 1 0 #
# E 1 0 3 #
# #
#########################
#########################

import random

######
#@@@@@# StringDoc+InitCond
######

""" CeasarED: encryption, decryption and public & private keygen

----------------
Encryption:
----------------

P1 = plaintext,
bS = baseString
pKa = shuffAlphaPrivateKey
pK1=shuffNumPrivateKey
PKa = alphaPublic key
PK1 = numPubKey
k = shift factor
C1 = cipher1
C2 = cipher2
C3 = cipher3
NaCl= salt

  1. P1 >>C1 -- to each letter in plaintext adds k*i



C1[i] = (P1[i]+bS[i*k]) % bS)

2.C1 >> C2 -- for each letter of cipher1 in baseString sub letter in alphaPrivateKey

C2[i] = pKa[bS[C1[i]]]

3.C2 >> C3 -- generate salt, add k to each num in numPubKey, use numPrivateKey to select salt from (numPubKey+k)

NaCl[i] = PKa[(pK1[i+k]) mod len (PKa) ]

C3[i] = bS[(C2[i] + NaCl[i]) mod len(bS)]

----------------
Decryption:
----------------

Reverse of encryption.

----------------
KeyGens:
----------------

PUBLIC
----------------

keyLen = length of public key

  1. Generate OneKey



OneKey = keyLen length array of random integers range 0 to (len(baseString) -1)

  1. Generate alphaKeySpace



alphaKeySpace[i] = baseString[OneKey[i]]

PRIVATE
----------------

  1. Generate numPrivKey, for each char in baseString, find all pos of char in alphaKeySpace, record i[] , choose one i for each char



  1. Generate alphaPrivKey



pKa[i] = baseString[pK1[i]]

  1. Generate shuffAlphaPrivKey and shuffNumPrivKey, arrange elem in list alphaPriv Key in random order



"""

main =

Solution

Let's start with a few things that will make your code way better:

  • Use docstrings, not # for comments.



  • Use a PEP 8 checker such as pycodestyle or flake8



  • The way you handle input and options is clever, but use a better argument parsing, such as click



  • You don't need alphabetDictionary, simply write baseString[int(temp1)]



  • Separate your code into functions which will have their own variables, so that eg. cipherOne, cipherTwo and so on won't be global variables



  • Describe your algorithmin a docstring

Context

StackExchange Code Review Q#140897, answer score: 4

Revisions (0)

No revisions yet.