First encryption program











up vote
3
down vote

favorite












This is the first piece of code that I write that's not just an exercise but a full program I can actually run.



Features:




  • Generate random letter/number pairs for each "printable" ASCII character

  • Save those pairs in a pickle file

  • Encode/decode any string based on that

  • Basic user interface


I would greatly appreciate any advice, feedback, comment you can give me on it, as I've been learning programming by myself for a couple months now. Did I structure it right, is it how a program is supposed to be put together, is it commented well and enough?



Also, I realize it's probably a weird way of encrypting anything and there's surely a better/safer/easier way to do encryption but it was mostly an excuse to write the program.



from string import printable,
ascii_letters,
ascii_lowercase,
ascii_uppercase
import random
import pickle

def generate_code():
# Generates a random number for each printable ASCII character,
# Returns a list of (character, number) tuples for each pair
characters = printable
numbers = random.sample(range(len(characters) + 1000), len(characters))
code = list(zip(characters, numbers))
return code

def encode(string, code):
# Replaces each character of string with its code-number
# and adds a random number of random letters

coded_string =
# find matching number for each character
for character in string:
for letter, number in code:
if character == letter:
# add the matching number
coded_string.append(str(number))
coded_string.append(''.join(
random.sample(
ascii_lowercase,
random.randint(2,6)
)
)
)# random letters used to separate numbers
for _ in range(random.randrange(len(string))):
coded_string.insert(
random.randrange(len(coded_string)),
''.join(random.sample(
ascii_uppercase, random.randint(1, 3)
))
) # random uppercase letters randomly inserted
return ''.join(coded_string)

def decode(string, code):

def retrieve_letter(n):
for letter, number in code:
if int(n) == number:
return letter
else:
continue
return "No Match found"


decoded_list =
decoded_string = ''
character = ''
for item in string:
if item.isdigit():
character += item
else:
if character != '':
decoded_list.append(character)
character = ''
for n in decoded_list:
decoded_string += retrieve_letter(n)
return decoded_string

def save_code(object):
with open("code.p", "wb") as f:
pickle.dump(object, f)

def load_code():
try:
with open("code.p", "rb") as f:
return pickle.load(f)
except FileNotFoundError:
print("No saved code found.")
return None


def main():

import time

code = generate_code()

print("Welcome to my encryption program!")

while True: #Code selection menu

print("Please select an option:")
print("1: Use saved code")
print("2: Use new code and overwrite saved code")
print("3: Use new code and keep saved code")
prompt = input(">")
if prompt == "1":
if load_code() == None:
code = generate_code()
else:
code = load_code()
break
elif prompt == "2":
save_code(code)
break
elif prompt == "3":
break
else:
"This option is not available"
continue

while True: #Main Loop, asks user if he wants to encode/decode

print("Would you like to encrypt a phrase?(Y/N)")

prompt = input(">")
if prompt in ("N", "no", "No", "n"):
print("Press Enter to exit or type in anything to continue:")
prompt = input(">")
if prompt == '':
print ("Thank you for using the program, good bye!")
time.sleep(2)
break
else:
phrase = input("Enter your text here :n>")
print (f"nHere is your code : {encode(phrase, code)}n")

print("Would you like to decrypt a phrase?(Y/N)")

prompt = input(">")
if prompt in ("N", "no", "No", "n"):
print("Press Enter to exit or type in anything to continue:")
prompt = input(">")
if prompt == '':
print ("Thank you for using the program, good bye!")
time.sleep(2)
break
else:
coded_phrase = input("Enter your code here :n>")
print(f"nHere is your original text: {decode(coded_phrase, code)}n")
time.sleep(1)
input("Press Enter to continue")
print("n")

if __name__ == '__main__':
main()









share|improve this question









New contributor




Guillaume Liard is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
























    up vote
    3
    down vote

    favorite












    This is the first piece of code that I write that's not just an exercise but a full program I can actually run.



    Features:




    • Generate random letter/number pairs for each "printable" ASCII character

    • Save those pairs in a pickle file

    • Encode/decode any string based on that

    • Basic user interface


    I would greatly appreciate any advice, feedback, comment you can give me on it, as I've been learning programming by myself for a couple months now. Did I structure it right, is it how a program is supposed to be put together, is it commented well and enough?



    Also, I realize it's probably a weird way of encrypting anything and there's surely a better/safer/easier way to do encryption but it was mostly an excuse to write the program.



    from string import printable,
    ascii_letters,
    ascii_lowercase,
    ascii_uppercase
    import random
    import pickle

    def generate_code():
    # Generates a random number for each printable ASCII character,
    # Returns a list of (character, number) tuples for each pair
    characters = printable
    numbers = random.sample(range(len(characters) + 1000), len(characters))
    code = list(zip(characters, numbers))
    return code

    def encode(string, code):
    # Replaces each character of string with its code-number
    # and adds a random number of random letters

    coded_string =
    # find matching number for each character
    for character in string:
    for letter, number in code:
    if character == letter:
    # add the matching number
    coded_string.append(str(number))
    coded_string.append(''.join(
    random.sample(
    ascii_lowercase,
    random.randint(2,6)
    )
    )
    )# random letters used to separate numbers
    for _ in range(random.randrange(len(string))):
    coded_string.insert(
    random.randrange(len(coded_string)),
    ''.join(random.sample(
    ascii_uppercase, random.randint(1, 3)
    ))
    ) # random uppercase letters randomly inserted
    return ''.join(coded_string)

    def decode(string, code):

    def retrieve_letter(n):
    for letter, number in code:
    if int(n) == number:
    return letter
    else:
    continue
    return "No Match found"


    decoded_list =
    decoded_string = ''
    character = ''
    for item in string:
    if item.isdigit():
    character += item
    else:
    if character != '':
    decoded_list.append(character)
    character = ''
    for n in decoded_list:
    decoded_string += retrieve_letter(n)
    return decoded_string

    def save_code(object):
    with open("code.p", "wb") as f:
    pickle.dump(object, f)

    def load_code():
    try:
    with open("code.p", "rb") as f:
    return pickle.load(f)
    except FileNotFoundError:
    print("No saved code found.")
    return None


    def main():

    import time

    code = generate_code()

    print("Welcome to my encryption program!")

    while True: #Code selection menu

    print("Please select an option:")
    print("1: Use saved code")
    print("2: Use new code and overwrite saved code")
    print("3: Use new code and keep saved code")
    prompt = input(">")
    if prompt == "1":
    if load_code() == None:
    code = generate_code()
    else:
    code = load_code()
    break
    elif prompt == "2":
    save_code(code)
    break
    elif prompt == "3":
    break
    else:
    "This option is not available"
    continue

    while True: #Main Loop, asks user if he wants to encode/decode

    print("Would you like to encrypt a phrase?(Y/N)")

    prompt = input(">")
    if prompt in ("N", "no", "No", "n"):
    print("Press Enter to exit or type in anything to continue:")
    prompt = input(">")
    if prompt == '':
    print ("Thank you for using the program, good bye!")
    time.sleep(2)
    break
    else:
    phrase = input("Enter your text here :n>")
    print (f"nHere is your code : {encode(phrase, code)}n")

    print("Would you like to decrypt a phrase?(Y/N)")

    prompt = input(">")
    if prompt in ("N", "no", "No", "n"):
    print("Press Enter to exit or type in anything to continue:")
    prompt = input(">")
    if prompt == '':
    print ("Thank you for using the program, good bye!")
    time.sleep(2)
    break
    else:
    coded_phrase = input("Enter your code here :n>")
    print(f"nHere is your original text: {decode(coded_phrase, code)}n")
    time.sleep(1)
    input("Press Enter to continue")
    print("n")

    if __name__ == '__main__':
    main()









    share|improve this question









    New contributor




    Guillaume Liard is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
    Check out our Code of Conduct.






















      up vote
      3
      down vote

      favorite









      up vote
      3
      down vote

      favorite











      This is the first piece of code that I write that's not just an exercise but a full program I can actually run.



      Features:




      • Generate random letter/number pairs for each "printable" ASCII character

      • Save those pairs in a pickle file

      • Encode/decode any string based on that

      • Basic user interface


      I would greatly appreciate any advice, feedback, comment you can give me on it, as I've been learning programming by myself for a couple months now. Did I structure it right, is it how a program is supposed to be put together, is it commented well and enough?



      Also, I realize it's probably a weird way of encrypting anything and there's surely a better/safer/easier way to do encryption but it was mostly an excuse to write the program.



      from string import printable,
      ascii_letters,
      ascii_lowercase,
      ascii_uppercase
      import random
      import pickle

      def generate_code():
      # Generates a random number for each printable ASCII character,
      # Returns a list of (character, number) tuples for each pair
      characters = printable
      numbers = random.sample(range(len(characters) + 1000), len(characters))
      code = list(zip(characters, numbers))
      return code

      def encode(string, code):
      # Replaces each character of string with its code-number
      # and adds a random number of random letters

      coded_string =
      # find matching number for each character
      for character in string:
      for letter, number in code:
      if character == letter:
      # add the matching number
      coded_string.append(str(number))
      coded_string.append(''.join(
      random.sample(
      ascii_lowercase,
      random.randint(2,6)
      )
      )
      )# random letters used to separate numbers
      for _ in range(random.randrange(len(string))):
      coded_string.insert(
      random.randrange(len(coded_string)),
      ''.join(random.sample(
      ascii_uppercase, random.randint(1, 3)
      ))
      ) # random uppercase letters randomly inserted
      return ''.join(coded_string)

      def decode(string, code):

      def retrieve_letter(n):
      for letter, number in code:
      if int(n) == number:
      return letter
      else:
      continue
      return "No Match found"


      decoded_list =
      decoded_string = ''
      character = ''
      for item in string:
      if item.isdigit():
      character += item
      else:
      if character != '':
      decoded_list.append(character)
      character = ''
      for n in decoded_list:
      decoded_string += retrieve_letter(n)
      return decoded_string

      def save_code(object):
      with open("code.p", "wb") as f:
      pickle.dump(object, f)

      def load_code():
      try:
      with open("code.p", "rb") as f:
      return pickle.load(f)
      except FileNotFoundError:
      print("No saved code found.")
      return None


      def main():

      import time

      code = generate_code()

      print("Welcome to my encryption program!")

      while True: #Code selection menu

      print("Please select an option:")
      print("1: Use saved code")
      print("2: Use new code and overwrite saved code")
      print("3: Use new code and keep saved code")
      prompt = input(">")
      if prompt == "1":
      if load_code() == None:
      code = generate_code()
      else:
      code = load_code()
      break
      elif prompt == "2":
      save_code(code)
      break
      elif prompt == "3":
      break
      else:
      "This option is not available"
      continue

      while True: #Main Loop, asks user if he wants to encode/decode

      print("Would you like to encrypt a phrase?(Y/N)")

      prompt = input(">")
      if prompt in ("N", "no", "No", "n"):
      print("Press Enter to exit or type in anything to continue:")
      prompt = input(">")
      if prompt == '':
      print ("Thank you for using the program, good bye!")
      time.sleep(2)
      break
      else:
      phrase = input("Enter your text here :n>")
      print (f"nHere is your code : {encode(phrase, code)}n")

      print("Would you like to decrypt a phrase?(Y/N)")

      prompt = input(">")
      if prompt in ("N", "no", "No", "n"):
      print("Press Enter to exit or type in anything to continue:")
      prompt = input(">")
      if prompt == '':
      print ("Thank you for using the program, good bye!")
      time.sleep(2)
      break
      else:
      coded_phrase = input("Enter your code here :n>")
      print(f"nHere is your original text: {decode(coded_phrase, code)}n")
      time.sleep(1)
      input("Press Enter to continue")
      print("n")

      if __name__ == '__main__':
      main()









      share|improve this question









      New contributor




      Guillaume Liard is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.











      This is the first piece of code that I write that's not just an exercise but a full program I can actually run.



      Features:




      • Generate random letter/number pairs for each "printable" ASCII character

      • Save those pairs in a pickle file

      • Encode/decode any string based on that

      • Basic user interface


      I would greatly appreciate any advice, feedback, comment you can give me on it, as I've been learning programming by myself for a couple months now. Did I structure it right, is it how a program is supposed to be put together, is it commented well and enough?



      Also, I realize it's probably a weird way of encrypting anything and there's surely a better/safer/easier way to do encryption but it was mostly an excuse to write the program.



      from string import printable,
      ascii_letters,
      ascii_lowercase,
      ascii_uppercase
      import random
      import pickle

      def generate_code():
      # Generates a random number for each printable ASCII character,
      # Returns a list of (character, number) tuples for each pair
      characters = printable
      numbers = random.sample(range(len(characters) + 1000), len(characters))
      code = list(zip(characters, numbers))
      return code

      def encode(string, code):
      # Replaces each character of string with its code-number
      # and adds a random number of random letters

      coded_string =
      # find matching number for each character
      for character in string:
      for letter, number in code:
      if character == letter:
      # add the matching number
      coded_string.append(str(number))
      coded_string.append(''.join(
      random.sample(
      ascii_lowercase,
      random.randint(2,6)
      )
      )
      )# random letters used to separate numbers
      for _ in range(random.randrange(len(string))):
      coded_string.insert(
      random.randrange(len(coded_string)),
      ''.join(random.sample(
      ascii_uppercase, random.randint(1, 3)
      ))
      ) # random uppercase letters randomly inserted
      return ''.join(coded_string)

      def decode(string, code):

      def retrieve_letter(n):
      for letter, number in code:
      if int(n) == number:
      return letter
      else:
      continue
      return "No Match found"


      decoded_list =
      decoded_string = ''
      character = ''
      for item in string:
      if item.isdigit():
      character += item
      else:
      if character != '':
      decoded_list.append(character)
      character = ''
      for n in decoded_list:
      decoded_string += retrieve_letter(n)
      return decoded_string

      def save_code(object):
      with open("code.p", "wb") as f:
      pickle.dump(object, f)

      def load_code():
      try:
      with open("code.p", "rb") as f:
      return pickle.load(f)
      except FileNotFoundError:
      print("No saved code found.")
      return None


      def main():

      import time

      code = generate_code()

      print("Welcome to my encryption program!")

      while True: #Code selection menu

      print("Please select an option:")
      print("1: Use saved code")
      print("2: Use new code and overwrite saved code")
      print("3: Use new code and keep saved code")
      prompt = input(">")
      if prompt == "1":
      if load_code() == None:
      code = generate_code()
      else:
      code = load_code()
      break
      elif prompt == "2":
      save_code(code)
      break
      elif prompt == "3":
      break
      else:
      "This option is not available"
      continue

      while True: #Main Loop, asks user if he wants to encode/decode

      print("Would you like to encrypt a phrase?(Y/N)")

      prompt = input(">")
      if prompt in ("N", "no", "No", "n"):
      print("Press Enter to exit or type in anything to continue:")
      prompt = input(">")
      if prompt == '':
      print ("Thank you for using the program, good bye!")
      time.sleep(2)
      break
      else:
      phrase = input("Enter your text here :n>")
      print (f"nHere is your code : {encode(phrase, code)}n")

      print("Would you like to decrypt a phrase?(Y/N)")

      prompt = input(">")
      if prompt in ("N", "no", "No", "n"):
      print("Press Enter to exit or type in anything to continue:")
      prompt = input(">")
      if prompt == '':
      print ("Thank you for using the program, good bye!")
      time.sleep(2)
      break
      else:
      coded_phrase = input("Enter your code here :n>")
      print(f"nHere is your original text: {decode(coded_phrase, code)}n")
      time.sleep(1)
      input("Press Enter to continue")
      print("n")

      if __name__ == '__main__':
      main()






      python beginner cryptography






      share|improve this question









      New contributor




      Guillaume Liard is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.











      share|improve this question









      New contributor




      Guillaume Liard is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.









      share|improve this question




      share|improve this question








      edited 9 mins ago









      Jamal

      30.2k11115226




      30.2k11115226






      New contributor




      Guillaume Liard is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.









      asked 1 hour ago









      Guillaume Liard

      163




      163




      New contributor




      Guillaume Liard is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.





      New contributor





      Guillaume Liard is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.






      Guillaume Liard is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.






















          1 Answer
          1






          active

          oldest

          votes

















          up vote
          1
          down vote













          Congrats on teaching yourself programming! You seem to be doing a good job of it. Aside from the fact that I would not want to use a homebrew crypto algorithm the following should be taken as minimal requirements for being able to rely on this code for productive work:




          • The sleep calls do not add anything useful. sleep should be avoided like the plague in production code.

          • I would expect this sort of program to follow some general *nix shell script guidelines:



            • Read from standard input and print() reusable data to standard output. So rather than open("code.p", …) and input() you'd read the input stream, perform decryption or encryption based on parameters, and print() the reusable data before exiting. In other words, after running something like ./my.py --encrypt --key=password1 < input.txt | ./my.py --decrypt --key=password1 > output.txt (I don't know what that would look like on Windows, sorry) input.txt and output.txt should be identical.

            • Expose all functionality in a non-interactive manner.



          • I can't follow the algorithm. I see a lot of mixing of numbers and letters, and what looks like an attempt to obfuscate the ciphertext by adding random length strings to the contents. Some unit tests (or even doctests) would be very useful to understand how this is meant to be read and understood.

          • It's hard to follow two while True loops. The first one doesn't even seem to be necessary - it only loops back if the user specifies an invalid option. It would be perfectly valid in this case to exit the program with an error instead.


          • Running the code through at least one linter such as flake8 or pycodestyle until it passes both without any warnings will help you create more idiomatic Python. Since you're using Python 3 I would also recommend adding type hints and running through a strict mypy configuration such as this one:



            [mypy]
            check_untyped_defs = true
            disallow_untyped_defs = true
            ignore_missing_imports = true
            no_implicit_optional = true
            warn_redundant_casts = true
            warn_return_any = true
            warn_unused_ignores = true







          share|improve this answer























          • When you say "requirements for putting this code into production" you mean "make it usable by professionals"? Also regarding the while true loops, I wanted to prompt the user again for the same choice if his choice is invalid, how should I do it? And loads of things I've never heard of but thanks a lot, that means I know what to research now!
            – Guillaume Liard
            45 mins ago












          • I'd consider those kind of the same thing - I think I can use "production" to mean it's performing useful work and can be built on top of, not necessarily that it's on a server answering queries.
            – l0b0
            44 mins ago












          • Also : "read from standard input, write to standard output" ? I don't really understand that one. Also I'm on Windows at the moment.
            – Guillaume Liard
            40 mins ago











          Your Answer





          StackExchange.ifUsing("editor", function () {
          return StackExchange.using("mathjaxEditing", function () {
          StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix) {
          StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["\$", "\$"]]);
          });
          });
          }, "mathjax-editing");

          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "196"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          convertImagesToLinks: false,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: null,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });






          Guillaume Liard is a new contributor. Be nice, and check out our Code of Conduct.










          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f209191%2ffirst-encryption-program%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes








          up vote
          1
          down vote













          Congrats on teaching yourself programming! You seem to be doing a good job of it. Aside from the fact that I would not want to use a homebrew crypto algorithm the following should be taken as minimal requirements for being able to rely on this code for productive work:




          • The sleep calls do not add anything useful. sleep should be avoided like the plague in production code.

          • I would expect this sort of program to follow some general *nix shell script guidelines:



            • Read from standard input and print() reusable data to standard output. So rather than open("code.p", …) and input() you'd read the input stream, perform decryption or encryption based on parameters, and print() the reusable data before exiting. In other words, after running something like ./my.py --encrypt --key=password1 < input.txt | ./my.py --decrypt --key=password1 > output.txt (I don't know what that would look like on Windows, sorry) input.txt and output.txt should be identical.

            • Expose all functionality in a non-interactive manner.



          • I can't follow the algorithm. I see a lot of mixing of numbers and letters, and what looks like an attempt to obfuscate the ciphertext by adding random length strings to the contents. Some unit tests (or even doctests) would be very useful to understand how this is meant to be read and understood.

          • It's hard to follow two while True loops. The first one doesn't even seem to be necessary - it only loops back if the user specifies an invalid option. It would be perfectly valid in this case to exit the program with an error instead.


          • Running the code through at least one linter such as flake8 or pycodestyle until it passes both without any warnings will help you create more idiomatic Python. Since you're using Python 3 I would also recommend adding type hints and running through a strict mypy configuration such as this one:



            [mypy]
            check_untyped_defs = true
            disallow_untyped_defs = true
            ignore_missing_imports = true
            no_implicit_optional = true
            warn_redundant_casts = true
            warn_return_any = true
            warn_unused_ignores = true







          share|improve this answer























          • When you say "requirements for putting this code into production" you mean "make it usable by professionals"? Also regarding the while true loops, I wanted to prompt the user again for the same choice if his choice is invalid, how should I do it? And loads of things I've never heard of but thanks a lot, that means I know what to research now!
            – Guillaume Liard
            45 mins ago












          • I'd consider those kind of the same thing - I think I can use "production" to mean it's performing useful work and can be built on top of, not necessarily that it's on a server answering queries.
            – l0b0
            44 mins ago












          • Also : "read from standard input, write to standard output" ? I don't really understand that one. Also I'm on Windows at the moment.
            – Guillaume Liard
            40 mins ago















          up vote
          1
          down vote













          Congrats on teaching yourself programming! You seem to be doing a good job of it. Aside from the fact that I would not want to use a homebrew crypto algorithm the following should be taken as minimal requirements for being able to rely on this code for productive work:




          • The sleep calls do not add anything useful. sleep should be avoided like the plague in production code.

          • I would expect this sort of program to follow some general *nix shell script guidelines:



            • Read from standard input and print() reusable data to standard output. So rather than open("code.p", …) and input() you'd read the input stream, perform decryption or encryption based on parameters, and print() the reusable data before exiting. In other words, after running something like ./my.py --encrypt --key=password1 < input.txt | ./my.py --decrypt --key=password1 > output.txt (I don't know what that would look like on Windows, sorry) input.txt and output.txt should be identical.

            • Expose all functionality in a non-interactive manner.



          • I can't follow the algorithm. I see a lot of mixing of numbers and letters, and what looks like an attempt to obfuscate the ciphertext by adding random length strings to the contents. Some unit tests (or even doctests) would be very useful to understand how this is meant to be read and understood.

          • It's hard to follow two while True loops. The first one doesn't even seem to be necessary - it only loops back if the user specifies an invalid option. It would be perfectly valid in this case to exit the program with an error instead.


          • Running the code through at least one linter such as flake8 or pycodestyle until it passes both without any warnings will help you create more idiomatic Python. Since you're using Python 3 I would also recommend adding type hints and running through a strict mypy configuration such as this one:



            [mypy]
            check_untyped_defs = true
            disallow_untyped_defs = true
            ignore_missing_imports = true
            no_implicit_optional = true
            warn_redundant_casts = true
            warn_return_any = true
            warn_unused_ignores = true







          share|improve this answer























          • When you say "requirements for putting this code into production" you mean "make it usable by professionals"? Also regarding the while true loops, I wanted to prompt the user again for the same choice if his choice is invalid, how should I do it? And loads of things I've never heard of but thanks a lot, that means I know what to research now!
            – Guillaume Liard
            45 mins ago












          • I'd consider those kind of the same thing - I think I can use "production" to mean it's performing useful work and can be built on top of, not necessarily that it's on a server answering queries.
            – l0b0
            44 mins ago












          • Also : "read from standard input, write to standard output" ? I don't really understand that one. Also I'm on Windows at the moment.
            – Guillaume Liard
            40 mins ago













          up vote
          1
          down vote










          up vote
          1
          down vote









          Congrats on teaching yourself programming! You seem to be doing a good job of it. Aside from the fact that I would not want to use a homebrew crypto algorithm the following should be taken as minimal requirements for being able to rely on this code for productive work:




          • The sleep calls do not add anything useful. sleep should be avoided like the plague in production code.

          • I would expect this sort of program to follow some general *nix shell script guidelines:



            • Read from standard input and print() reusable data to standard output. So rather than open("code.p", …) and input() you'd read the input stream, perform decryption or encryption based on parameters, and print() the reusable data before exiting. In other words, after running something like ./my.py --encrypt --key=password1 < input.txt | ./my.py --decrypt --key=password1 > output.txt (I don't know what that would look like on Windows, sorry) input.txt and output.txt should be identical.

            • Expose all functionality in a non-interactive manner.



          • I can't follow the algorithm. I see a lot of mixing of numbers and letters, and what looks like an attempt to obfuscate the ciphertext by adding random length strings to the contents. Some unit tests (or even doctests) would be very useful to understand how this is meant to be read and understood.

          • It's hard to follow two while True loops. The first one doesn't even seem to be necessary - it only loops back if the user specifies an invalid option. It would be perfectly valid in this case to exit the program with an error instead.


          • Running the code through at least one linter such as flake8 or pycodestyle until it passes both without any warnings will help you create more idiomatic Python. Since you're using Python 3 I would also recommend adding type hints and running through a strict mypy configuration such as this one:



            [mypy]
            check_untyped_defs = true
            disallow_untyped_defs = true
            ignore_missing_imports = true
            no_implicit_optional = true
            warn_redundant_casts = true
            warn_return_any = true
            warn_unused_ignores = true







          share|improve this answer














          Congrats on teaching yourself programming! You seem to be doing a good job of it. Aside from the fact that I would not want to use a homebrew crypto algorithm the following should be taken as minimal requirements for being able to rely on this code for productive work:




          • The sleep calls do not add anything useful. sleep should be avoided like the plague in production code.

          • I would expect this sort of program to follow some general *nix shell script guidelines:



            • Read from standard input and print() reusable data to standard output. So rather than open("code.p", …) and input() you'd read the input stream, perform decryption or encryption based on parameters, and print() the reusable data before exiting. In other words, after running something like ./my.py --encrypt --key=password1 < input.txt | ./my.py --decrypt --key=password1 > output.txt (I don't know what that would look like on Windows, sorry) input.txt and output.txt should be identical.

            • Expose all functionality in a non-interactive manner.



          • I can't follow the algorithm. I see a lot of mixing of numbers and letters, and what looks like an attempt to obfuscate the ciphertext by adding random length strings to the contents. Some unit tests (or even doctests) would be very useful to understand how this is meant to be read and understood.

          • It's hard to follow two while True loops. The first one doesn't even seem to be necessary - it only loops back if the user specifies an invalid option. It would be perfectly valid in this case to exit the program with an error instead.


          • Running the code through at least one linter such as flake8 or pycodestyle until it passes both without any warnings will help you create more idiomatic Python. Since you're using Python 3 I would also recommend adding type hints and running through a strict mypy configuration such as this one:



            [mypy]
            check_untyped_defs = true
            disallow_untyped_defs = true
            ignore_missing_imports = true
            no_implicit_optional = true
            warn_redundant_casts = true
            warn_return_any = true
            warn_unused_ignores = true








          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited 34 mins ago

























          answered 58 mins ago









          l0b0

          4,122923




          4,122923












          • When you say "requirements for putting this code into production" you mean "make it usable by professionals"? Also regarding the while true loops, I wanted to prompt the user again for the same choice if his choice is invalid, how should I do it? And loads of things I've never heard of but thanks a lot, that means I know what to research now!
            – Guillaume Liard
            45 mins ago












          • I'd consider those kind of the same thing - I think I can use "production" to mean it's performing useful work and can be built on top of, not necessarily that it's on a server answering queries.
            – l0b0
            44 mins ago












          • Also : "read from standard input, write to standard output" ? I don't really understand that one. Also I'm on Windows at the moment.
            – Guillaume Liard
            40 mins ago


















          • When you say "requirements for putting this code into production" you mean "make it usable by professionals"? Also regarding the while true loops, I wanted to prompt the user again for the same choice if his choice is invalid, how should I do it? And loads of things I've never heard of but thanks a lot, that means I know what to research now!
            – Guillaume Liard
            45 mins ago












          • I'd consider those kind of the same thing - I think I can use "production" to mean it's performing useful work and can be built on top of, not necessarily that it's on a server answering queries.
            – l0b0
            44 mins ago












          • Also : "read from standard input, write to standard output" ? I don't really understand that one. Also I'm on Windows at the moment.
            – Guillaume Liard
            40 mins ago
















          When you say "requirements for putting this code into production" you mean "make it usable by professionals"? Also regarding the while true loops, I wanted to prompt the user again for the same choice if his choice is invalid, how should I do it? And loads of things I've never heard of but thanks a lot, that means I know what to research now!
          – Guillaume Liard
          45 mins ago






          When you say "requirements for putting this code into production" you mean "make it usable by professionals"? Also regarding the while true loops, I wanted to prompt the user again for the same choice if his choice is invalid, how should I do it? And loads of things I've never heard of but thanks a lot, that means I know what to research now!
          – Guillaume Liard
          45 mins ago














          I'd consider those kind of the same thing - I think I can use "production" to mean it's performing useful work and can be built on top of, not necessarily that it's on a server answering queries.
          – l0b0
          44 mins ago






          I'd consider those kind of the same thing - I think I can use "production" to mean it's performing useful work and can be built on top of, not necessarily that it's on a server answering queries.
          – l0b0
          44 mins ago














          Also : "read from standard input, write to standard output" ? I don't really understand that one. Also I'm on Windows at the moment.
          – Guillaume Liard
          40 mins ago




          Also : "read from standard input, write to standard output" ? I don't really understand that one. Also I'm on Windows at the moment.
          – Guillaume Liard
          40 mins ago










          Guillaume Liard is a new contributor. Be nice, and check out our Code of Conduct.










          draft saved

          draft discarded


















          Guillaume Liard is a new contributor. Be nice, and check out our Code of Conduct.













          Guillaume Liard is a new contributor. Be nice, and check out our Code of Conduct.












          Guillaume Liard is a new contributor. Be nice, and check out our Code of Conduct.
















          Thanks for contributing an answer to Code Review Stack Exchange!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          Use MathJax to format equations. MathJax reference.


          To learn more, see our tips on writing great answers.





          Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


          Please pay close attention to the following guidance:


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f209191%2ffirst-encryption-program%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          Costa Masnaga

          Fotorealismo

          Sidney Franklin