File size: 4,501 Bytes
52d28e8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
import streamlit as st
import base64
from Crypto.Cipher import AES
from Crypto.Util.Padding import pad, unpad
from Crypto.Random import get_random_bytes
import hashlib

st.title("🔒 Text Encryptor/Decryptor")
st.markdown("Secure text encryption using multiple algorithms")

# Encryption functions
def caesar_encrypt(text, shift):
    result = ""
    for char in text:
        if char.isalpha():
            ascii_offset = 65 if char.isupper() else 97
            result += chr((ord(char) + shift - ascii_offset) % 26 + ascii_offset)
        else:
            result += char
    return result

def caesar_decrypt(text, shift):
    return caesar_encrypt(text, -shift)

def vigenere_encrypt(text, key):
    key = key.upper()
    key_len = len(key)
    encrypted = []
    for i, char in enumerate(text):
        if char.isalpha():
            offset = 65 if char.isupper() else 97
            key_char = key[i % key_len]
            shift = ord(key_char) - 65
            encrypted_char = chr((ord(char) + shift - offset) % 26 + offset)
            encrypted.append(encrypted_char)
        else:
            encrypted.append(char)
    return "".join(encrypted)

def vigenere_decrypt(text, key):
    key = key.upper()
    key_len = len(key)
    decrypted = []
    for i, char in enumerate(text):
        if char.isalpha():
            offset = 65 if char.isupper() else 97
            key_char = key[i % key_len]
            shift = ord(key_char) - 65
            decrypted_char = chr((ord(char) - shift - offset) % 26 + offset)
            decrypted.append(decrypted_char)
        else:
            decrypted.append(char)
    return "".join(decrypted)

def aes_encrypt(text, password):
    salt = get_random_bytes(16)
    key = hashlib.scrypt(password.encode(), salt=salt, n=16384, r=8, p=1, dklen=32)
    iv = get_random_bytes(16)
    cipher = AES.new(key, AES.MODE_CBC, iv)
    ciphertext = cipher.encrypt(pad(text.encode(), AES.block_size))
    return base64.b64encode(salt + iv + ciphertext).decode()

def aes_decrypt(encrypted, password):
    encrypted = base64.b64decode(encrypted)
    salt = encrypted[:16]
    iv = encrypted[16:32]
    ciphertext = encrypted[32:]
    key = hashlib.scrypt(password.encode(), salt=salt, n=16384, r=8, p=1, dklen=32)
    cipher = AES.new(key, AES.MODE_CBC, iv)
    decrypted = unpad(cipher.decrypt(ciphertext), AES.block_size)
    return decrypted.decode()

# Interface
operation = st.radio("Select Operation:", ["Encrypt", "Decrypt"])
method = st.selectbox("Select Encryption Method:", 
                     ["Caesar Cipher", "Vigenère Cipher", "AES-256"])

text = st.text_area("Enter Text:", height=200)
password = st.text_input("Enter Key/Password:", type="password")

if st.button(f"Perform {operation}"):
    if text and password:
        try:
            if method == "Caesar Cipher":
                if not password.isdigit():
                    raise ValueError("Caesar cipher requires numeric key")
                shift = int(password) % 26
                if operation == "Encrypt":
                    result = caesar_encrypt(text, shift)
                else:
                    result = caesar_decrypt(text, shift)
                    
            elif method == "Vigenère Cipher":
                if operation == "Encrypt":
                    result = vigenere_encrypt(text, password)
                else:
                    result = vigenere_decrypt(text, password)
                    
            elif method == "AES-256":
                if operation == "Encrypt":
                    result = aes_encrypt(text, password)
                else:
                    result = aes_decrypt(text, password)
                    
            st.success(f"Result:")
            st.code(result)
            
            st.download_button(
                label="Download Result",
                data=result,
                file_name="encrypted.txt" if operation == "Encrypt" else "decrypted.txt",
                mime="text/plain"
            )
            
        except Exception as e:
            st.error(f"Error: {str(e)}")
    else:
        st.warning("Please enter both text and key/password")

st.sidebar.markdown("## Instructions")
st.sidebar.markdown("""
1. Select encrypt/decrypt operation
2. Choose encryption method
3. Enter text and key/password
4. Click the action button
                    
**Key Requirements:**
- Caesar: Numeric key (1-25)
- Vigenère: Text key (letters only)
- AES-256: Strong password (any characters)
""")