forked from UTSAVS26/PySnippets
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'UTSAVS26:main' into main
- Loading branch information
Showing
9 changed files
with
333 additions
and
43 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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,47 @@ | ||
# Security Policy | ||
|
||
## Reporting a Vulnerability | ||
|
||
We take the security of our project seriously. If you discover a security vulnerability, please follow these steps: | ||
|
||
1. **DO NOT** create a public GitHub issue for the vulnerability. | ||
2. Send a report to our team through our Discord server: https://discord. | ||
3. Provide as much information as possible about the vulnerability: | ||
- Type of issue | ||
- Full paths of source file(s) related to the issue | ||
- Location of the affected source code | ||
- Any special configuration required to reproduce the issue | ||
- Step-by-step instructions to reproduce the issue | ||
- Proof-of-concept or exploit code (if possible) | ||
- Impact of the issue | ||
|
||
## Response Timeline | ||
|
||
- We will acknowledge receipt of your vulnerability report within 48 hours. | ||
- We will provide a more detailed response within 7 days. | ||
- We will work on fixing the vulnerability and will keep you informed of our progress. | ||
- Once the vulnerability is fixed, we will publicly disclose the security issue. | ||
|
||
## Supported Versions | ||
|
||
We will address security vulnerabilities in the following versions: | ||
|
||
| Version | Supported | | ||
| ------- | ------------------ | | ||
| latest | :white_check_mark: | | ||
|
||
## Best Practices | ||
|
||
- Please give us reasonable time to address the issue before making any public disclosure. | ||
- Act in good faith towards our users' privacy and data. | ||
- Do not access or modify other users' data without explicit permission. | ||
|
||
## Recognition | ||
|
||
We appreciate the security research community's efforts in helping keep our project safe. Responsible disclosure of vulnerabilities helps us ensure the security and privacy of our users. | ||
|
||
## Contact | ||
|
||
For any security-related concerns, please contact us through: | ||
- Discord: https://discord. | ||
Thank you for helping keep our community safe! |
This file contains 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,17 @@ | ||
import unittest | ||
from pysnippets.Cipher_algorithms.affine_cipher import AffineCipher | ||
|
||
class TestAffineCipher(unittest.TestCase): | ||
def setUp(self): | ||
self.cipher = AffineCipher(a=5, b=8) | ||
|
||
def test_encrypt(self): | ||
self.assertEqual(self.cipher.encrypt('HELLO'), 'MJQQT') | ||
self.assertEqual(self.cipher.encrypt('AFFINE CIPHER'), 'IHHWVC SWFRCP') | ||
|
||
def test_decrypt(self): | ||
self.assertEqual(self.cipher.decrypt('MJQQT'), 'HELLO') | ||
self.assertEqual(self.cipher.decrypt('IHHWVC SWFRCP'), 'AFFINE CIPHER') | ||
|
||
if __name__ == '__main__': | ||
unittest.main() |
This file contains 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,74 @@ | ||
import unittest | ||
import sys | ||
import os | ||
|
||
sys.path.insert(0, os.path.abspath(os.path.join(os.path.dirname(__file__), '../../..'))) | ||
from pysnippets.Cipher_algorithms.caesar_cipher import CaesarCipher | ||
|
||
class TestCaesarCipher(unittest.TestCase): | ||
def setUp(self): | ||
self.cipher = CaesarCipher() | ||
|
||
def test_default_encryption(self): | ||
message = "Hello, World!" | ||
expected = "Khoor, Zruog!" | ||
self.assertEqual(self.cipher.encrypt(message), expected) | ||
|
||
def test_default_decryption(self): | ||
encrypted = "Khoor, Zruog!" | ||
expected = "Hello, World!" | ||
self.assertEqual(self.cipher.decrypt(encrypted), expected) | ||
|
||
def test_custom_shift(self): | ||
custom_cipher = CaesarCipher(shift=5) | ||
message = "Python Programming" | ||
encrypted = custom_cipher.encrypt(message) | ||
decrypted = custom_cipher.decrypt(encrypted) | ||
self.assertEqual(decrypted, message) | ||
|
||
def test_non_alphabetic_characters(self): | ||
message = "Hello, World! 123" | ||
encrypted = self.cipher.encrypt(message) | ||
decrypted = self.cipher.decrypt(encrypted) | ||
self.assertEqual(message, decrypted) | ||
|
||
def test_brute_force_decryption(self): | ||
message = "Hello, World!" | ||
encrypted = self.cipher.encrypt(message) | ||
decryptions = self.cipher.brute_force_decrypt(encrypted) | ||
found_decryption = any( | ||
decryption == message for _, decryption in decryptions | ||
) | ||
self.assertTrue(found_decryption) | ||
|
||
def test_full_cycle(self): | ||
message = "The quick brown fox jumps over the lazy dog!" | ||
encrypted = self.cipher.encrypt(message) | ||
decrypted = self.cipher.decrypt(encrypted) | ||
self.assertEqual(message, decrypted) | ||
|
||
def test_empty_string(self): | ||
message = "" | ||
encrypted = self.cipher.encrypt(message) | ||
decrypted = self.cipher.decrypt(encrypted) | ||
self.assertEqual(message, decrypted) | ||
|
||
def test_large_shift(self): | ||
custom_cipher = CaesarCipher(shift=30) | ||
message = "Large Shift" | ||
encrypted = custom_cipher.encrypt(message) | ||
decrypted = custom_cipher.decrypt(encrypted) | ||
self.assertEqual(decrypted, message) | ||
|
||
def test_negative_shift(self): | ||
custom_cipher = CaesarCipher(shift=-3) | ||
message = "Negative Shift" | ||
encrypted = custom_cipher.encrypt(message) | ||
decrypted = custom_cipher.decrypt(encrypted) | ||
self.assertEqual(decrypted, message) | ||
|
||
def test_non_string_input(self): | ||
with self.assertRaises(TypeError): | ||
self.cipher.encrypt(12345) | ||
with self.assertRaises(TypeError): | ||
self.cipher.decrypt(12345) |
This file contains 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,17 @@ | ||
import unittest | ||
from pysnippets.Cipher_algorithms.playfair_cipher import playfair_encrypt, playfair_decrypt | ||
|
||
class TestPlayfairCipher(unittest.TestCase): | ||
|
||
def test_playfair_encrypt(self): | ||
self.assertEqual(playfair_encrypt("HELLO", "KEYWORD"), "RIJVS") | ||
self.assertEqual(playfair_encrypt("PLAYFAIR", "KEYWORD"), "RLBMZIXR") | ||
self.assertEqual(playfair_encrypt("HACKTOBERFEST", "KEYWORD"), "RIBKZQKZBFXZ") | ||
|
||
def test_playfair_decrypt(self): | ||
self.assertEqual(playfair_decrypt("RIJVS", "KEYWORD"), "HELXLO") | ||
self.assertEqual(playfair_decrypt("RLBMZIXR", "KEYWORD"), "PLAYFAIR") | ||
self.assertEqual(playfair_decrypt("RIBKZQKZBFXZ", "KEYWORD"), "HACKTOBERFEST") | ||
|
||
if __name__ == '__main__': | ||
unittest.main() |
This file contains 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,18 @@ | ||
class AffineCipher: | ||
def __init__(self, a, b): | ||
self.a = a | ||
self.b = b | ||
self.m = 26 # Length of the alphabet | ||
|
||
def encrypt(self, plaintext): | ||
return ''.join( | ||
chr(((self.a * (ord(char) - ord('A')) + self.b) % self.m) + ord('A')) | ||
if char.isalpha() else char for char in plaintext.upper() | ||
) | ||
|
||
def decrypt(self, ciphertext): | ||
a_inv = pow(self.a, -1, self.m) | ||
return ''.join( | ||
chr(((a_inv * ((ord(char) - ord('A')) - self.b)) % self.m) + ord('A')) | ||
if char.isalpha() else char for char in ciphertext.upper() | ||
) |
This file contains 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,49 @@ | ||
import string | ||
|
||
class CaesarCipher: | ||
def __init__(self, shift=3): | ||
self.shift = shift % 26 | ||
self.encrypt_table = self._create_translation_table(self.shift) | ||
self.decrypt_table = self._create_translation_table(-self.shift) | ||
|
||
def _create_translation_table(self, shift): | ||
lower = string.ascii_lowercase | ||
upper = string.ascii_uppercase | ||
lower_shifted = lower[shift:] + lower[:shift] | ||
upper_shifted = upper[shift:] + upper[:shift] | ||
trans_table = str.maketrans( | ||
lower + upper, | ||
lower_shifted + upper_shifted | ||
) | ||
return trans_table | ||
|
||
def encrypt(self, message): | ||
if not isinstance(message, str): | ||
raise TypeError("Message must be a string") | ||
return message.translate(self.encrypt_table) | ||
|
||
def decrypt(self, encrypted_message): | ||
if not isinstance(encrypted_message, str): | ||
raise TypeError("Encrypted message must be a string") | ||
return encrypted_message.translate(self.decrypt_table) | ||
|
||
def brute_force_decrypt(self, encrypted_message): | ||
possible_decryptions = [] | ||
for potential_shift in range(26): | ||
temp_cipher = CaesarCipher(shift=potential_shift) | ||
decrypted = temp_cipher.decrypt(encrypted_message) | ||
possible_decryptions.append((potential_shift, decrypted)) | ||
return possible_decryptions | ||
|
||
def main(): | ||
cipher = CaesarCipher() | ||
original_message = "Hello, World!" | ||
print("Original Message:", original_message) | ||
encrypted_message = cipher.encrypt(original_message) | ||
print("Encrypted Message:", encrypted_message) | ||
decrypted_message = cipher.decrypt(encrypted_message) | ||
print("Decrypted Message:", decrypted_message) | ||
print("\nBrute Force Decryption:") | ||
possible_decryptions = cipher.brute_force_decrypt(encrypted_message) | ||
for shift, decryption in possible_decryptions: | ||
print(f"Shift {shift}: {decryption}") |
This file contains 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,61 @@ | ||
def generate_key_table(key): | ||
key = ''.join(sorted(set(key), key=lambda x: key.index(x))) | ||
key += ''.join(chr(i) for i in range(65, 91) if chr(i) not in key and chr(i) != 'J') | ||
return [list(key[i:i+5]) for i in range(0, 25, 5)] | ||
|
||
def preprocess_text(text): | ||
text = text.upper().replace('J', 'I') | ||
processed_text = "" | ||
i = 0 | ||
while i < len(text): | ||
processed_text += text[i] | ||
if i + 1 < len(text) and text[i] == text[i + 1]: | ||
processed_text += 'X' | ||
elif i + 1 < len(text): | ||
processed_text += text[i + 1] | ||
i += 1 | ||
i += 1 | ||
if len(processed_text) % 2 != 0: | ||
processed_text += 'X' | ||
return processed_text | ||
|
||
def find_position(char, key_table): | ||
for i, row in enumerate(key_table): | ||
if char in row: | ||
return i, row.index(char) | ||
return None | ||
|
||
def playfair_encrypt(plaintext, key): | ||
key_table = generate_key_table(key) | ||
plaintext = preprocess_text(plaintext) | ||
ciphertext = "" | ||
for i in range(0, len(plaintext), 2): | ||
row1, col1 = find_position(plaintext[i], key_table) | ||
row2, col2 = find_position(plaintext[i + 1], key_table) | ||
if row1 == row2: | ||
ciphertext += key_table[row1][(col1 + 1) % 5] | ||
ciphertext += key_table[row2][(col2 + 1) % 5] | ||
elif col1 == col2: | ||
ciphertext += key_table[(row1 + 1) % 5][col1] | ||
ciphertext += key_table[(row2 + 1) % 5][col2] | ||
else: | ||
ciphertext += key_table[row1][col2] | ||
ciphertext += key_table[row2][col1] | ||
return ciphertext | ||
|
||
def playfair_decrypt(ciphertext, key): | ||
key_table = generate_key_table(key) | ||
plaintext = "" | ||
for i in range(0, len(ciphertext), 2): | ||
row1, col1 = find_position(ciphertext[i], key_table) | ||
row2, col2 = find_position(ciphertext[i + 1], key_table) | ||
if row1 == row2: | ||
plaintext += key_table[row1][(col1 - 1) % 5] | ||
plaintext += key_table[row2][(col2 - 1) % 5] | ||
elif col1 == col2: | ||
plaintext += key_table[(row1 - 1) % 5][col1] | ||
plaintext += key_table[(row2 - 1) % 5][col2] | ||
else: | ||
plaintext += key_table[row1][col2] | ||
plaintext += key_table[row2][col1] | ||
return plaintext |
Oops, something went wrong.