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

Encrypter - Double Vigenere Cipher in Python

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

Problem

I wrote this encrypter based on the idea of a Vigenere cipher, but instead of using only one key, it "makes" another key from the existing key. The length of the second key also depends on the different characters in the first key. And so it uses both keys in the shifting of letters.

```
def scram(key):
key2 = []
#make the length of the second key varying from key to key for harder cracking
length = (key[0]-32)+(key[int(len(key)/2)]-32)+(key[len(key)-1]-32)
#make the max length = 256
length = length % 256
#if the length is less than 64, multiply by two to make it longer
while(length < 64):
length*=2

#scrambles the letters around
for x in range(length):
#basically shifts the key according to the current character,
#how many times it has looped the key, where it is in the key,
#and the modulo of x and a few prime numbers to make sure that
#an overlap/repeat doesn't happen.
toapp = (x%(len(key)-1)) + (x/(len(key)-1)) + (key[x%(len(key)-1)]) + (x%3) +(x%5)+(x%53)+(x%7)
toapp = int(toapp % 94)
key2.append(toapp)

return key2

def cipher(mes,key,ac):
#makes the second key
key2 = scram(key)
res=[]

#do proper shifting in the keys
for x in range(len(mes)):
temp=mes[x]

if(action == "2"):
temp = temp - key[x%(len(key)-1)] - key2[x%(len(key2)-1)]
else:
temp = temp + key[x%(len(key)-1)] + key2[x%(len(key2)-1)]

temp = int(temp % 94)

res.append(chr(temp+32))

return res

#encrypt or decrypt
action = input("type 1 to encypt. type 2 to decrypt:")
#input
m= input("Text:")
k= input("key - 4 or more char:")

#changes the letters to ascii value
mes= []
for x in m:
mes.append(ord(x)-32)

key= []
for x in k:
key.append(ord(x)-32)

#encrypts it
result = cipher(mes,key,action)

for x in result:
print(x,end="")

print("")

y = input("Press enter

Solution

List comprehension

m= input("Text:")
k= input("key - 4 or more char:")

#changes the letters to ascii value
mes= []
for x in m:
    mes.append(ord(x)-32)

key= []
for x in k:
    key.append(ord(x)-32)


This all can be shortened to two list comprehensions:

mes = [ord(x) - 32 for x in input("Text: ")]
key = # Exercise for the reader


Avoid globals in functions

if(action == "2"):


action should be a parameter of the function.

temp forever

temp=mes[x]


You name a variable temp and use it 8 lines... please give a sensible name to this important variable.

Super long formula

toapp = (x%(len(key)-1)) + (x/(len(key)-1)) + (key[x%(len(key)-1)]) + (x%3) +(x%5)+(x%53)+(x%7)


Please, put a comment near this huge formula.

Avoid so many assignments

result = cipher(mes,key)

for x in result:
    print(x,end="")

print("")


In python it is idiomatic to avoid extraneous variable assignements and explicit for loops are rarely really necessary, this all can be shortened to:

print(''.join(cipher(mes,key)))


Do not assign junk input

The last line should just be:

input("Enter to continue...")

Code Snippets

m= input("Text:")
k= input("key - 4 or more char:")

#changes the letters to ascii value
mes= []
for x in m:
    mes.append(ord(x)-32)

key= []
for x in k:
    key.append(ord(x)-32)
mes = [ord(x) - 32 for x in input("Text: ")]
key = # Exercise for the reader
if(action == "2"):
temp=mes[x]
toapp = (x%(len(key)-1)) + (x/(len(key)-1)) + (key[x%(len(key)-1)]) + (x%3) +(x%5)+(x%53)+(x%7)

Context

StackExchange Code Review Q#102246, answer score: 7

Revisions (0)

No revisions yet.