Skip to content

Commit fd21822

Browse files
authored
Merge pull request #1731 from Harshit28j/text_encryption_hj
Add text encryption script
2 parents 547c343 + f59af82 commit fd21822

File tree

5 files changed

+252
-0
lines changed

5 files changed

+252
-0
lines changed

Image_text_hider/img_text_hider.py

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
import argparse
2+
from stegano import lsb
3+
import os
4+
5+
def hide_text_in_image(image_path, text, output_path):
6+
secret_image = lsb.hide(image_path, text)
7+
secret_image.save(output_path)
8+
9+
def reveal_text_from_image(image_path):
10+
try:
11+
secret_text = lsb.reveal(image_path)
12+
return (secret_text,1) #here 1 and 0 are used to enhance script quality
13+
except UnicodeDecodeError:
14+
return ("Unable to reveal text. Decoding error occurred.",0)
15+
except IndexError:
16+
return("Failed to find message in this file format, Try using different file format like png",0)
17+
except Exception as e:
18+
return(f"Contact the owner! {e}",0)
19+
20+
def main():
21+
print("\n[Welcome to Image text hider this script can hide text inside image]\n")
22+
print("To Hide the text inside image\nUSAGE: python img_text_hider.py hide img_name_with_path.jpg 'This is my secret msg' output_file_name.jpg\n")
23+
print("To reveal the hidden text inside image\nUSAGE: python img_text_hider.py reveal hidden_img_name.jpg\n")
24+
parser = argparse.ArgumentParser(description="Image Text Hider")
25+
26+
subparsers = parser.add_subparsers(dest="command", help="Available commands")
27+
28+
# Hide command
29+
hide_parser = subparsers.add_parser("hide", help="Hide text behind an image")
30+
hide_parser.add_argument("image", help="Path to the image file")
31+
hide_parser.add_argument("text", help="Text to hide")
32+
hide_parser.add_argument("output", help="Output path for the image with hidden text")
33+
34+
# Reveal command
35+
reveal_parser = subparsers.add_parser("reveal", help="Reveal text from an image")
36+
reveal_parser.add_argument("image", help="Path to the image file")
37+
38+
args = parser.parse_args()
39+
40+
if args.command == "hide":
41+
if os.path.exists(args.image):
42+
hide_text_in_image(args.image, args.text, args.output)
43+
print("Text hidden in the image successfully. Output image saved at", args.output)
44+
else:
45+
print("Image path you specified does not exist, Make sure to check image path and file name with extention")
46+
elif args.command == "reveal":
47+
if os.path.exists(args.image):
48+
49+
revealed_text,check = reveal_text_from_image(args.image)
50+
51+
if check==1: #if works out well
52+
print(f"Revealed text: [{revealed_text}]")
53+
else: # else display with error so that user can troubleshot the problem easily
54+
print(f'Error!,{revealed_text}')
55+
56+
else:
57+
print("Image path you specified does not exist, Make sure to check image path and file name with extention")
58+
59+
if __name__ == "__main__":
60+
main()

Image_text_hider/readme.md

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
# Image Text Hider
2+
3+
This script allows you to hide text inside an image using steganography. It utilizes the LSB (Least Significant Bit) technique to embed the text into the image without visibly altering the image.
4+
5+
## Requirements
6+
7+
- Python 3.6 or above
8+
- Install the required packages by running `pip install -r requirements.txt`.
9+
10+
## Usage
11+
12+
To hide text inside an image:
13+
python img_text_hider.py hide <image_path> <text> <output_path>
14+
15+
- `<image_path>`: Path to the image file.
16+
- `<text>`: Text to hide inside the image.
17+
- `<output_path>`: Output path for the image with hidden text.
18+
19+
To reveal the hidden text from an image:
20+
python img_text_hider.py reveal <image_path>
21+
22+
- `<image_path>`: Path to the image file with hidden text.
23+
24+
## Example
25+
26+
Hide text inside an image:
27+
python img_text_hider.py hide my_image.jpg "This is my secret message" output_image.jpg
28+
29+
Reveal the hidden text from an image:
30+
python img_text_hider.py reveal output_image.jpg

Image_text_hider/requirements.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
argparse
2+
stegano

text_encryption/encryption_method.py

Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
import string
2+
import sys
3+
4+
# Caesar cipher encryption
5+
def caesar_cipher_encrypt(text, shift):
6+
encrypted_text = ""
7+
for char in text:
8+
if char.isalpha():
9+
alphabet = string.ascii_uppercase if char.isupper() else string.ascii_lowercase
10+
encrypted_char = alphabet[(alphabet.index(char) + shift) % len(alphabet)]
11+
encrypted_text += encrypted_char
12+
else:
13+
encrypted_text += char
14+
return encrypted_text
15+
16+
# Affine cipher encryption
17+
def affine_cipher_encrypt(text, a, b):
18+
encrypted_text = ""
19+
for char in text:
20+
if char.isalpha():
21+
alphabet = string.ascii_uppercase if char.isupper() else string.ascii_lowercase
22+
encrypted_char = alphabet[(a * alphabet.index(char) + b) % len(alphabet)]
23+
encrypted_text += encrypted_char
24+
else:
25+
encrypted_text += char
26+
return encrypted_text
27+
28+
# Substitution cipher encryption
29+
def substitution_cipher_encrypt(text, key):
30+
encrypted_text = ""
31+
for char in text:
32+
if char.isalpha():
33+
alphabet = string.ascii_uppercase if char.isupper() else string.ascii_lowercase
34+
substitution_alphabet = str.maketrans(alphabet, key.upper()) if char.isupper() else str.maketrans(alphabet, key.lower())
35+
encrypted_char = char.translate(substitution_alphabet)
36+
encrypted_text += encrypted_char
37+
else:
38+
encrypted_text += char
39+
return encrypted_text
40+
41+
# Transposition cipher encryption
42+
def transposition_cipher_encrypt(text, key):
43+
ciphertext = [''] * int(key)
44+
for column in range(int(key)):
45+
currentIndex = column
46+
while currentIndex < len(text):
47+
ciphertext[column] += text[currentIndex]
48+
currentIndex += int(key)
49+
50+
return ''.join(ciphertext)
51+
52+
def main():
53+
text = input("Enter the text to encrypt: ")
54+
55+
# Ask user for the encryption method
56+
print("Choose an encryption method:")
57+
print("1. Caesar cipher")
58+
print("2. Affine cipher")
59+
print("3. Substitution cipher")
60+
print("4. Transposition cipher")
61+
62+
try:
63+
choice = int(input("Enter your choice (1-4): "))
64+
except ValueError: #to handle wrong values
65+
print("Invlaid selection")
66+
sys.exit(0)
67+
68+
if choice == 1:
69+
70+
try:
71+
shift = int(input("Enter the shift value for Caesar cipher: "))
72+
encrypted_text = caesar_cipher_encrypt(text, shift)
73+
print("Caesar cipher (encryption):", encrypted_text)
74+
except ValueError:
75+
print("Invalid Input")
76+
77+
elif choice == 2:
78+
79+
try:
80+
a = int(input("Enter the value for 'a' in Affine cipher: "))
81+
b = int(input("Enter the value for 'b' in Affine cipher: "))
82+
encrypted_text = affine_cipher_encrypt(text, a, b)
83+
print("Affine cipher (encryption):", encrypted_text)
84+
85+
except ValueError:
86+
print("Invalid Input")
87+
88+
elif choice == 3:
89+
90+
key = input("Enter the substitution key: ")
91+
if len(key)==26:
92+
encrypted_text = substitution_cipher_encrypt(text, key)
93+
print("Substitution cipher (encryption):", encrypted_text)
94+
else:
95+
print("Key must have the same length as the number of characters in the alphabet (26).")
96+
97+
elif choice == 4:
98+
99+
try:
100+
transpose_key = input("Enter the transposition key (make sure its less than length of stirng): ")
101+
if int(transpose_key)>len(text):
102+
print("Key must be less than length of string")
103+
else:
104+
encrypted_text = transposition_cipher_encrypt(text, transpose_key)
105+
print("Transposition cipher (encryption):", encrypted_text)
106+
107+
except ValueError:
108+
print("Invalid Input")
109+
110+
else:
111+
print("Invalid choice. Please choose a number between 1 and 4.")
112+
113+
if __name__ == "__main__":
114+
main()

text_encryption/readme.md

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
# Encryption Script
2+
3+
This script allows you to encrypt text using various encryption methods.
4+
5+
## Installation
6+
7+
1. Make sure you have Python installed on your system.
8+
2. Clone this repository or download the script file.
9+
10+
## Usage
11+
12+
1. Open a terminal or command prompt.
13+
2. Navigate to the directory where the script is located.
14+
3. Run the following command: python script.py
15+
16+
Follow the prompts to enter the text and choose an encryption method.
17+
Encryption Methods
18+
19+
Caesar Cipher
20+
The Caesar cipher is a substitution cipher where each letter in the plaintext is shifted a certain number of positions down the alphabet.
21+
To choose the Caesar cipher, enter 1 when prompted.
22+
23+
Affine Cipher
24+
The Affine cipher is a substitution cipher that combines the Caesar cipher with multiplication and addition.
25+
To choose the Affine cipher, enter 2 when prompted.
26+
27+
Substitution Cipher
28+
The Substitution cipher is a method of encryption where each letter in the plaintext is replaced by another letter according to a fixed key.
29+
To choose the Substitution cipher, enter 3 when prompted. Note that the key must have the same length as the number of characters in the alphabet (26).
30+
31+
Transposition Cipher
32+
The Transposition cipher is a method of encryption that rearranges the letters of the plaintext to form the ciphertext.
33+
To choose the Transposition cipher, enter 4 when prompted. You will also be asked to enter a transposition key, which should be less than the length of the text.
34+
Note: If you enter an invalid choice or provide incorrect input, appropriate error messages will be displayed.
35+
36+
Example
37+
Here is an example of running the script:
38+
Enter the text to encrypt: Hello, World!
39+
Choose an encryption method:
40+
1. Caesar cipher
41+
2. Affine cipher
42+
3. Substitution cipher
43+
4. Transposition cipher
44+
Enter your choice (1-4): 3
45+
Enter the substitution key: QWERTYUIOPASDFGHJKLZXCVBNM
46+
Substitution cipher (encryption): ITSSG, KTSSG!

0 commit comments

Comments
 (0)