File size: 1,860 Bytes
0d527a7
6121902
0d527a7
6121902
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f40a9d3
6121902
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f40a9d3
6121902
 
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
import gradio as gr
import numpy as np

def transpose_cipher(text, key):
    # Convert the key to a list of integers
    key_list = [int(k) for k in key.split()]
    # Calculate the number of columns
    num_cols = len(key_list)
    # Calculate the number of rows
    num_rows = -(-len(text) // num_cols)  # Ceiling division
    # Initialize the matrix with spaces
    matrix = [[' ' for _ in range(num_cols)] for _ in range(num_rows)]
    # Fill the matrix with the text
    index = 0
    for row in range(num_rows):
        for col in range(num_cols):
            if index < len(text):
                matrix[row][col] = text[index]
                index += 1
    # Transpose the matrix based on the key
    transposed_matrix = ['' for _ in range(num_rows)]
    for col in range(num_cols):
        for row in range(num_rows):
            transposed_matrix[key_list[col] - 1] += matrix[row][col]
    # Join the transposed matrix into a single string
    return ''.join(transposed_matrix)

# Create the Gradio interface
with gr.Blocks() as demo:
    # Input components with tooltips and instructions
    text_input = gr.Textbox(
        label="Text to Encrypt",
        placeholder="Enter your text here",
        info="Enter the text you want to encrypt."
    )
    key_input = gr.Textbox(
        label="Key",
        placeholder="Enter the key (space-separated integers)",
        info="Enter the key as a sequence of space-separated integers. The key should be a permutation of the column indices."
    )
    # Output component
    encrypted_output = gr.Textbox(label="Encrypted Text")
    # Button to trigger the encryption
    encrypt_button = gr.Button("Encrypt")
    # Define the event listener
    encrypt_button.click(fn=transpose_cipher, inputs=[text_input, key_input], outputs=encrypted_output)

# Launch the Gradio app
demo.launch(show_error=True)