矩形列换位加密法:无数组实现字符串列转换加密求助
Got it, let's break down how to solve this without relying on arrays. You're already able to pull the first column, so we just need to generalize that logic to handle any column, regardless of the input string's length.
First, Let's Align on the Core Logic
For your example:
- Input message:
sell all stocks now→ first strip spaces to get the plaintext:sellallstocksnow(length 16) - Key:
213→ this means we split the plaintext into 3 columns, and read them in the order: column 2 → column 1 → column 3 (the key digits map directly to column numbers, starting at 1)
Calculate Critical Values First
Before writing loops, compute these two values for any input:
column_count: Equal to the length of your key (3 in your case)row_count: The number of rows needed, calculated by rounding up the length of the plaintext divided bycolumn_count. Use this formula to avoid floating-point operations:
For your 16-character plaintext:row_count = (len(plaintext) + column_count - 1) // column_count(16 + 3 - 1) // 3 = 18 // 3 = 6rows total.
The Loop Logic (No Arrays Required)
Instead of storing columns in arrays, we'll directly build the ciphertext by iterating over each column in the key's order, pulling characters one by one and appending them to the result.
Here's how to handle each target column (e.g., column 2 first, then 1, then 3):
- Start at the position corresponding to the target column (adjust for 0-based vs 1-based indexing—most languages use 0-based, so subtract 1 from the key's digit)
- Skip
column_countcharacters each time to get the next character in the same column - Stop once you exceed the length of the plaintext
Example Code (Python, No Arrays)
This code follows the logic above and works for any key/input length:
def column_transpose_encrypt(plaintext, key): # Preprocess: remove spaces, convert to uppercase clean_text = plaintext.replace(" ", "").upper() col_count = len(key) text_length = len(clean_text) ciphertext = "" # Iterate over each column in the key's order for col_char in key: # Convert key digit to 0-based column index current_col = int(col_char) - 1 # Pull every nth character starting at current_col for i in range(current_col, text_length, col_count): ciphertext += clean_text[i] return ciphertext # Test with your example input_msg = "sell all stocks now" secret_key = "213" print(column_transpose_encrypt(input_msg, secret_key))
When you run this, it'll generate the ciphertext by directly appending characters from each column—no arrays used anywhere.
Quick Breakdown of Your Example
- For column 2 (0-based index 1): We pull positions 1, 4, 7, 10, 13, 15 → characters
E, A, S, C, N, W→ appendsEASCNS - For column 1 (0-based index 0): Pull positions 0, 3, 6, 9, 12 →
S, L, L, O, S→ appendsSLLOS - For column 3 (0-based index 2): Pull positions 2, 5, 8, 11, 14 →
L, L, T, K, O→ appendsLLTKO - Final ciphertext:
EASCNSLLOSLLTKO(note: if your expected output has an extraW, double-check the input string—maybe a typo in spacing, but the core logic holds)
Key Takeaways
- The
range(current_col, text_length, col_count)loop is the magic here—it skips exactly the right number of characters to stay in the same column - You don't need to store columns at all; just build the ciphertext incrementally
- The row count formula works for any input length, so this logic scales even if your plaintext is shorter or longer than 16 characters
内容的提问来源于stack exchange,提问作者Carla Mather

