This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Hello World |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var enigma = Enigma(rotor0: swissK[0], rotor1: swissK[1], rotor2: swissK[2], plugboard: plugboard, key: (.A, .A, .A)) | |
let message = "HELLOWORLD" | |
let message2 = "AAAAAAAAAA" | |
let ciphered = enigma.cipher(message) | |
let ciphered2 = enigma.cipher(message2) | |
var _enigma = Enigma(rotor0: swissK[0], rotor1: swissK[1], rotor2: swissK[2], plugboard: plugboard, key: (.A, .A, .A)) | |
let deciphered = _enigma.cipher(ciphered) | |
let deciphered2 = _enigma.cipher(ciphered2) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// rotor | |
let ETW_K: Cipher = { token in | |
switch token { | |
case .A: return .Q | |
case .B: return .W | |
case .C: return .E | |
case .D: return .R | |
case .E: return .T | |
case .F: return .Z | |
case .G: return .U |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public struct RotorBox { | |
... | |
public mutating func cipher(_ target: Token) -> Token { | |
defer { | |
// 関数を抜ける際に回転 | |
rotate() | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public struct RotorBox { | |
public struct Rotor { | |
let forward: Cipher | |
let backward: Cipher | |
var position: Token | |
init(_ forward: @escaping Cipher, position: Token) { | |
self.forward = forward |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public enum Token: Character { | |
case A = "A", B = "B", C = "C", D = "D", E = "E", F = "F" | |
case G = "G", H = "H", I = "I", J = "J", K = "K", L = "L" | |
case M = "M", N = "N", O = "O", P = "P", Q = "Q", R = "R" | |
case S = "S", T = "T", U = "U", V = "V", W = "W", X = "X" | |
case Y = "Y", Z = "Z" | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public typealias Cipher = (Token) -> (Token) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public struct Enigma { | |
private var rotorBox: RotorBox | |
private let plugboard: Cipher | |
public typealias Key = (Token, Token, Token) | |
public init(rotor0: @escaping Cipher, rotor1: @escaping Cipher, rotor2: @escaping Cipher, plugboard: @escaping Cipher, key: Key) { | |
self.rotorBox = RotorBox( | |
rotor0: RotorBox.Rotor(rotor0, position: key.0), | |
rotor1: RotorBox.Rotor(rotor1, position: key.1), |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Foundation | |
enum Token: Character { | |
case A = "A" | |
case B = "B" | |
case C = "C" | |
case D = "D" | |
case E = "E" | |
case F = "F" | |
case G = "G" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# pow(3, 8) = 6561 | |
options = ['', '+', '-'] | |
for i in options: | |
for j in options: | |
for k in options: | |
for l in options: | |
for m in options: | |
for n in options: | |
for o in options: | |
for p in options: |