Skip to main content
Make link more easy treada a read to read
Source Link

A warning

As a toy this is fine, but please do not use it (or encourage others to use it) for real cryptographic application. It is fun as an exercise, but will not be sufficiently strong to protect you against certain common attacks.

Strings as sequences

In Python, a string is a sequence of one-character strings. So you don't need to represent it as a list of strings, because for your purposes that's what a string already is:

alphabets = 'abcdefghijklmnopqrstuvwxyz'

That said, you can replace the entire thing with https://docs.python.org/3/library/string.html#string.ascii_lowercasestring.ascii_lowercase .:

from string import ascii_lowercase
alphabets = ascii_lowercase

Position

This whole function can be replaced with:

source.index(to_find)

Parens

We aren't in C/Java, so this:

if (z>26):

does not need parentheses.

Magic numbers

Do not hard-code 26 here:

z = z % 26

Instead, use len(alphabets). Also, use in-place modulus:

z %= len(alphabets)

A warning

As a toy this is fine, but please do not use it (or encourage others to use it) for real cryptographic application. It is fun as an exercise, but will not be sufficiently strong to protect you against certain common attacks.

Strings as sequences

In Python, a string is a sequence of one-character strings. So you don't need to represent it as a list of strings, because for your purposes that's what a string already is:

alphabets = 'abcdefghijklmnopqrstuvwxyz'

That said, you can replace the entire thing with https://docs.python.org/3/library/string.html#string.ascii_lowercase .

Position

This whole function can be replaced with:

source.index(to_find)

Parens

We aren't in C/Java, so this:

if (z>26):

does not need parentheses.

Magic numbers

Do not hard-code 26 here:

z = z % 26

Instead, use len(alphabets). Also, use in-place modulus:

z %= len(alphabets)

A warning

As a toy this is fine, but please do not use it (or encourage others to use it) for real cryptographic application. It is fun as an exercise, but will not be sufficiently strong to protect you against certain common attacks.

Strings as sequences

In Python, a string is a sequence of one-character strings. So you don't need to represent it as a list of strings, because for your purposes that's what a string already is:

alphabets = 'abcdefghijklmnopqrstuvwxyz'

That said, you can replace the entire thing with string.ascii_lowercase:

from string import ascii_lowercase
alphabets = ascii_lowercase

Position

This whole function can be replaced with:

source.index(to_find)

Parens

We aren't in C/Java, so this:

if (z>26):

does not need parentheses.

Magic numbers

Do not hard-code 26 here:

z = z % 26

Instead, use len(alphabets). Also, use in-place modulus:

z %= len(alphabets)
Source Link
Reinderien
  • 65.6k
  • 5
  • 70
  • 191

A warning

As a toy this is fine, but please do not use it (or encourage others to use it) for real cryptographic application. It is fun as an exercise, but will not be sufficiently strong to protect you against certain common attacks.

Strings as sequences

In Python, a string is a sequence of one-character strings. So you don't need to represent it as a list of strings, because for your purposes that's what a string already is:

alphabets = 'abcdefghijklmnopqrstuvwxyz'

That said, you can replace the entire thing with https://docs.python.org/3/library/string.html#string.ascii_lowercase .

Position

This whole function can be replaced with:

source.index(to_find)

Parens

We aren't in C/Java, so this:

if (z>26):

does not need parentheses.

Magic numbers

Do not hard-code 26 here:

z = z % 26

Instead, use len(alphabets). Also, use in-place modulus:

z %= len(alphabets)