Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add autoclave cipher #8029

Merged
merged 11 commits into from
Dec 18, 2022
Merged

Add autoclave cipher #8029

merged 11 commits into from
Dec 18, 2022

Conversation

VictorRS27
Copy link
Contributor

Describe your change:

I'm adding the autoclave or autokey cipher.
Is similar to the vigenerè cipher, but using the own plaintext as the key.
The code implements an encrypt and a decrypt function.
For more info:
https://en.wikipedia.org/wiki/Autokey_cipher

  • Add an algorithm?
  • Fix a bug or typo in an existing algorithm?
  • Documentation change?

Checklist:

  • I have read CONTRIBUTING.md.
  • This pull request is all my own work -- I have not plagiarized.
  • I know that pull requests will not be merged if they fail the automated tests.
  • This PR only changes one algorithm file. To ease review, please open separate PRs for separate algorithms.
  • All new Python files are placed inside an existing directory.
  • All filenames are in all lowercase characters with no spaces or dashes.
  • All functions and variable names follow Python naming conventions.
  • All function parameters and return values are annotated with Python type hints.
  • All functions have doctests that pass the automated testing.
  • All new algorithms include at least one URL that points to Wikipedia or another similar explanation.
  • If this pull request resolves one or more open issues then the commit message contains Fixes: #{$ISSUE_NO}.

Copy link
Contributor

@CaedenPH CaedenPH left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please link some documentation at the start of the file and include more doctests within each function

Comment on lines 6 to 12
@params
plaintext - a normal text to be encrypted (string)
key - a small text or word to start the replacing (sFtring)

@return
A string with the ciphertext

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
@params
plaintext - a normal text to be encrypted (string)
key - a small text or word to start the replacing (sFtring)
@return
A string with the ciphertext

Typically, we want the parameter names to be self-documenting so this isn't really necessary

Comment on lines 21 to 23
if plaintext == "":
raise ValueError("plaintext is empty")
if key == "":
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if plaintext == "":
raise ValueError("plaintext is empty")
if key == "":
if not plaintext:
raise ValueError("plaintext is empty")
if not key:

if key == "":
raise ValueError("key is empty")

key = key + plaintext
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
key = key + plaintext
key += plaintext

@@ -0,0 +1,111 @@
def encrypt(plaintext, key) -> str:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
def encrypt(plaintext, key) -> str:
def encrypt(plaintext, key) -> str:

These parameters need to be typehinted

Comment on lines 102 to 111
operation = int(input("Type 1 to encrypt or 2 to decrypt:"))
if operation == 1:
plaintext = str(input("Typeplaintext to be encrypted:\n"))
key = str(input("Type the key:\n"))
print(encrypt(plaintext, key))
elif operation == 2:
ciphertext = str(input("Type the ciphertext to be decrypted:\n"))
key = str(input("Type the key:\n"))
print(decrypt(ciphertext, key))
decrypt("jsqqs avvwo", "coffee")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please move this into an if __name__ == "__main__": block

@@ -0,0 +1,111 @@
def encrypt(plaintext, key) -> str:
Copy link
Member

@cclauss cclauss Dec 15, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Clave is Latin for key so the more popular name for this algorithm is autokey.

Suggested change
def encrypt(plaintext, key) -> str:
”””
https://en.wikipedia.org/wiki/Autokey_cipher
> An autokey cipher (also known as the autoclave cipher) is a cipher that incorporates the message (the plaintext into the key. The key is generated from the message in some automated fashion, sometimes by selecting certain letters from the text or, more commonly, by adding a short primer key to the front of the message.
”””
def encrypt(plaintext: str, key: str) -> str:

>>> encrypt("hello world", "coffee")
'jsqqs avvwo'
"""
if type(plaintext) != str:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

PEP8: Use isinstance(plaintext, str) instead of directly comparing types.

@algorithms-keeper algorithms-keeper bot added the awaiting reviews This PR is ready to be reviewed label Dec 18, 2022
@algorithms-keeper algorithms-keeper bot added the tests are failing Do not merge until tests pass label Dec 18, 2022
@algorithms-keeper algorithms-keeper bot removed the awaiting reviews This PR is ready to be reviewed label Dec 18, 2022
@algorithms-keeper algorithms-keeper bot added the awaiting reviews This PR is ready to be reviewed label Dec 18, 2022
@algorithms-keeper algorithms-keeper bot removed the tests are failing Do not merge until tests pass label Dec 18, 2022
@cclauss cclauss merged commit 3f8b2af into TheAlgorithms:master Dec 18, 2022
@algorithms-keeper algorithms-keeper bot removed the awaiting reviews This PR is ready to be reviewed label Dec 18, 2022
Cjkjvfnby pushed a commit to Cjkjvfnby/Python that referenced this pull request Mar 13, 2023
* Add autoclave cipher

* Update autoclave with the given suggestions

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

* Fixing errors

* Another fixes

* Update and rename autoclave.py to autokey.py

* Rename gaussian_naive_bayes.py to gaussian_naive_bayes.py.broken.txt

* Rename gradient_boosting_regressor.py to gradient_boosting_regressor.py.broken.txt

* Rename random_forest_classifier.py to random_forest_classifier.py.broken.txt

* Rename random_forest_regressor.py to random_forest_regressor.py.broken.txt

* Rename equal_loudness_filter.py to equal_loudness_filter.py.broken.txt

Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
Co-authored-by: Christian Clauss <cclauss@me.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants