I recently worked on a simple and basic encryption program. (skip reading the code before reading the question)
def generator():
enckey="abcdefghijklmnopqrstuvwxyz"
a=list(enckey)
random.shuffle(a)
c=''
for i in a:
c+=i
c=c+a[0]
return c
def senc(a,enckey=generator()):
b=list(a)
for i in range(0,len(a)):
for j in range(0,27):
if b[i]==enckey[j]:
b[i]=enckey[j+1]
break
c=''
for i in b:
c+=i
print("Encrypted text:",c,"\nEncryption Key:",enckey)
Now when I call the function senc("argument")
, it generates a enckey
and uses it to encrypt the supplied text. Now, since I say that enckey=generator()
I expect that every time senc
is executed it will re execute generator to get a key. But this did not happen.
The following is the proof:
>>> id(senc("Andy"))
Encrypted text: Arnj
Encryption Key: htpkzwqlxcsdnregobvaimyjufh
1655676136
>>> id(senc("Candy"))
Encrypted text: Cirnj
Encryption Key: htpkzwqlxcsdnregobvaimyjufh
1655676136
Both are at the same memory address and as seen the encryption key is same. Where did I go wrong, and why does it not call the function?
Please note that the encryption key does change for every new instance of IDLE/commandprompt, etc.