emedvedev/enigma

Enigma machines that were serialized using gob don't work

Closed this issue · 3 comments

I need to persist an Enigma machine to bytes and restore it later. I use this function to save it

func saveEnigmaToFile(machine *enigma.Enigma, filename string) {
    buf := &bytes.Buffer{}
    enc := gob.NewEncoder(buf)

    enc.Encode(machine)

    file, err := os.Create(filename)
    defer file.Close()
    if err != nil {
        log.Fatal(err)
    }

    file.Write(buf.Bytes())
}

And restore it like this:

    var machine2 *enigma.Enigma
    machine2, err := loadEnigmaFromFile("machine99.bin")

But the restored machine's configuration is different from what was saved. Using degob, I see that it was serialized ok (added linefeeds for clarity).

{"Reflector":{"ID":"B",
"Sequence":[24,17,20,7,16,18,11,3,15,23,13,6,14,10,12,8,4,1,5,25,2,22,21,9,0,19]},
"Plugboard":[1,0,3,2,5,4,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25],
"Rotors":[
{"ID":"III",
"StraightSeq":[1,3,5,7,9,11,2,15,17,19,23,21,25,13,24,4,8,22,6,0,10,12,20,18,16,14],
"ReverseSeq":[19,0,6,1,15,2,18,3,16,4,20,5,21,13,25,7,24,8,23,9,22,11,17,10,14,12],
"Turnover":[21],"Offset":0,"Ring":0},
{"ID":"II",
"StraightSeq":[0,9,3,10,18,8,17,20,23,1,11,7,22,19,12,2,16,6,25,13,15,24,5,21,14,4],
"ReverseSeq":[0,9,15,2,25,22,17,11,5,1,3,10,14,19,24,20,16,6,4,13,7,23,12,8,21,18],
"Turnover":[4],"Offset":1,"Ring":0},
{"ID":"IV",
"StraightSeq":[4,18,14,21,15,25,9,0,24,16,20,8,17,7,23,11,13,5,19,6,10,3,2,12,22,1],
"ReverseSeq":[7,25,22,21,0,17,19,13,11,6,20,15,23,16,2,4,9,12,1,18,10,3,24,14,8,5],
"Turnover":[9],"Offset":2,"Ring":0}]}

But the Offset and Ring values are alwasy wrong for the second and thrid rotor, regardless of if the machine encrypted something or not.

Is there something special about the Enigma struct when it comes to serializing and deserializing it?

There shouldn't be anything special, really. What's the code of the loadEnigmaFromFile function?

Shame on me. The code is so trivial I did not bother posting it. Or was it my subconcious preventing that I make a fool of myself?

Turns out the filename sent to loadEnigmaFromFile was ignored and a test file was deserialized instead. Sorry for the noise!