示例#1
0
func main() {
	// build random rotors and reflector
	randSrc := rand.NewSource(kSeed)
	rotors, err := genRandRotors(randSrc, kPower, kTurnPoss[:], kPoss[:], kRings[:], kNRotors)
	if err != nil {
		fmt.Println(err)
		return
	}
	reflector, err := genRandReflector(randSrc, kPower)
	if err != nil {
		fmt.Println(err)
		return
	}
	randEnigma, err := enigma.NewEnigma(rotors, reflector)
	if err != nil {
		fmt.Println(err)
		return
	}

	var out int
	//values := []int{0, 1, 2}
	//values := []int{3, 1, 8}
	//values := []int{11, 1, 10}
	values := []int{2, 9, 0}
	for _, v := range values {
		if out, err = randEnigma.Transform(v); err != nil {
			fmt.Println(err)
			return
		}
		fmt.Println(v, out)
	}
}
示例#2
0
func main() {
	// path to file should be
	if len(os.Args) < 3 {
		fmt.Println(ErrNoFile)
		return
	}
	inFilename := os.Args[1]
	outFilename := os.Args[2]

	// build random rotors and reflector
	randSrc := rand.NewSource(kSeed)
	rotors, err := genRandRotors(randSrc, kPower, kTurnPoss[:], kPoss[:], kRings[:], kNRotors)
	if err != nil {
		fmt.Println(err)
		return
	}
	reflector, err := genRandReflector(randSrc, kPower)
	if err != nil {
		fmt.Println(err)
		return
	}
	randEnigma, err := enigma.NewEnigma(rotors, reflector)
	if err != nil {
		fmt.Println(err)
		return
	}

	// transform input file
	inFile, err := ioutil.ReadFile(inFilename)
	if err != nil {
		fmt.Println(ErrInvalidFile)
		return
	}
	var out int
	outFile := make([]byte, len(inFile))
	for i, in := range inFile {
		if out, err = randEnigma.Transform(int(in)); err != nil {
			fmt.Println(err)
			return
		}
		outFile[i] = byte(out)
	}
	if err = ioutil.WriteFile(outFilename, outFile, 0644); err != nil {
		fmt.Println(err)
		return
	}
	return
}