You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Enigma密码算法长文本编码异常排查求助

Enigma密码算法长文本编码异常排查求助

Hey there! Let's dig into your Enigma code issue. You mentioned that encoding the alphabet three times (78 chars) gives a different result from online tools, but 78 capital A's work fine. That clue tells me the problem is almost certainly in your rotor rotation logic—since repeating A's doesn't change the input index, the rotation triggers might line up by accident, but varied input exposes the timing error.

Looking at your rotor_rotations function, here's the key mistake:
You're rotating the rightmost rotor first, then checking if its new position matches the notch to trigger the middle rotor's rotation. But in a real Enigma machine, the notch triggers the next rotor's rotation before the rightmost rotor moves. Also, you're missing the critical "double step" behavior for the middle rotor, which is a unique quirk of the Enigma design.

Let's break down the correct rotor sequence:

  1. First, check if the middle rotor is in its notch position. If yes, we do a "double step": rotate both the left and middle rotors (this ensures the middle rotor rotates in sync with the left when it hits its notch).
  2. If the middle rotor isn't in the notch, check if the rightmost rotor is in its notch position. If yes, rotate the middle rotor.
  3. Finally, rotate the rightmost rotor.

Your current code does the reverse: it rotates the right rotor first, then checks the new position for the notch, which means you're triggering the middle rotor rotation one keypress too late. That's why longer, varied text drifts from the correct output—each missed rotation adds up over time.

Here's the fixed rotor_rotations function, plus a note on correct notch values (since your current notches don't match standard Enigma rotors, which might also contribute to discrepancies):

def rotate(string):
    return string[1:] + string[0]

def rotor_rotations(rotors, alfators, notches):
    # Handle double step for middle rotor first
    # Check if middle rotor is at its notch BEFORE any rotation
    if notches[1] == rotors[1][-1]:
        # Rotate left and middle rotor simultaneously
        rotors[0] = rotate(rotors[0])
        alfators[0] = rotate(alfators[0])
        rotors[1] = rotate(rotors[1])
        alfators[1] = rotate(alfators[1])
    else:
        # Check if right rotor is at its notch BEFORE rotating it
        if notches[2] == rotors[2][-1]:
            # Rotate middle rotor
            rotors[1] = rotate(rotors[1])
            alfators[1] = rotate(alfators[1])
    # Finally rotate the rightmost rotor
    rotors[2] = rotate(rotors[2])
    alfators[2] = rotate(alfators[2])
    return rotors, alfators

Also, update your notch values to match standard Enigma rotors (this ensures consistency with online tools):

# Standard notches: Rotor I (Q), Rotor II (E), Rotor III (V)
notches = ['Q', 'E', 'V']

Why did 78 A's work? When you input the same character repeatedly, the rotation triggers might have aligned by chance with your incorrect logic, but as soon as the input varies (like the full alphabet), the index calculations interact with the misaligned rotations, causing the output to drift.

备注:内容来源于stack exchange,提问作者Tristan Galle

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.13 19:19:37