-
-
Notifications
You must be signed in to change notification settings - Fork 48.7k
Add RC4 Stream Cipher Implementation with Key Scheduling and Pseudo-Random Generation #12735
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
Closed
Closed
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
eb297ef
Merge pull request #1 from TheAlgorithms/master
Charuvarthan 93f50dd
Added RC4 stream cipher implementation
Charuvarthan-T 8abed30
Merge branch 'TheAlgorithms:master' into master
Charuvarthan 7ab1902
Added the rc4.py
Charuvarthan-T 152cea6
Merge branch 'master' of https://github.com/Charuvarthan/Python
Charuvarthan-T 19bbad4
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
from typing import List | ||
|
||
|
||
class RC4: | ||
def __init__(self, key: bytes): | ||
self.key = key | ||
self.s = self._ksa(key) | ||
|
||
def _ksa(self, key: bytes) -> List[int]: | ||
""" | ||
Key Scheduling Algorithm (KSA) | ||
|
||
>>> rc4 = RC4(b"SecretKey") | ||
>>> len(rc4._ksa(b"SecretKey")) | ||
256 | ||
""" | ||
key_length = len(key) | ||
s = list(range(256)) | ||
j = 0 | ||
for i in range(256): | ||
j = (j + s[i] + key[i % key_length]) % 256 | ||
s[i], s[j] = s[j], s[i] | ||
return s | ||
|
||
def _prga(self) -> int: | ||
""" | ||
Pseudo-Random Generation Algorithm (PRGA) | ||
|
||
>>> rc4 = RC4(b"SecretKey") | ||
>>> prga = rc4._prga() | ||
>>> isinstance(next(prga), int) | ||
True | ||
""" | ||
i = 0 | ||
j = 0 | ||
while True: | ||
i = (i + 1) % 256 | ||
j = (j + self.s[i]) % 256 | ||
self.s[i], self.s[j] = self.s[j], self.s[i] | ||
yield self.s[(self.s[i] + self.s[j]) % 256] | ||
|
||
def _reset_state(self): | ||
Charuvarthan marked this conversation as resolved.
Show resolved
Hide resolved
|
||
"""Reset state for each encryption/decryption.""" | ||
self.s = self._ksa(self.key) | ||
|
||
def encrypt(self, plaintext: bytes) -> bytes: | ||
""" | ||
Encrypt plaintext using RC4 | ||
|
||
>>> rc4 = RC4(b"SecretKey") | ||
>>> plaintext = b"Hello" | ||
>>> encrypted = rc4.encrypt(plaintext) | ||
>>> len(encrypted) == len(plaintext) | ||
True | ||
""" | ||
self._reset_state() | ||
prga = self._prga() | ||
return bytes([p ^ next(prga) for p in plaintext]) | ||
|
||
def decrypt(self, ciphertext: bytes) -> bytes: | ||
""" | ||
Decrypt ciphertext using RC4 | ||
|
||
>>> rc4 = RC4(b"SecretKey") | ||
>>> ciphertext = rc4.encrypt(b"Hello") | ||
>>> rc4.decrypt(ciphertext) == b"Hello" | ||
True | ||
""" | ||
return self.encrypt(ciphertext) | ||
|
||
|
||
if __name__ == "__main__": | ||
import doctest | ||
|
||
doctest.testmod() | ||
|
||
key = b"SecretKey" | ||
rc4 = RC4(key) | ||
|
||
plaintext = b"Hello, RC4 Cipher!" | ||
print(f"Original: {plaintext}") | ||
|
||
ciphertext = rc4.encrypt(plaintext) | ||
print(f"Encrypted: {ciphertext}") | ||
|
||
decrypted_text = rc4.decrypt(ciphertext) | ||
print(f"Decrypted: {decrypted_text}") | ||
|
||
assert plaintext == decrypted_text, "Decryption failed!" | ||
print("Encryption and decryption successful.") |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please provide return type hint for the function:
__init__
. If the function does not return a value, please provide the type hint as:def function() -> None: