Beispiel #1
0
func main() {
	shouldDecrypt := flag.Bool("d", false, "decrypt the input file")
	shouldEncrypt := flag.Bool("e", false, "encrypt the input file")
	genKeyFile := flag.String("genkey", "", "generate a new key")
	keyFile := flag.String("k", "", "key file")
	inFile := flag.String("in", "", "input file")
	outFile := flag.String("out", "", "output file")
	flag.Parse()

	var err error
	if *keyFile == "" && *genKeyFile == "" {
		fmt.Println("[!] no key specified and not generating a key, nothing to do.")
		os.Exit(1)
	} else if *keyFile != "" {
		key, err = ioutil.ReadFile(*keyFile)
		if err != nil {
			fmt.Println("[!]", err.Error())
			os.Exit(1)
		}
	} else if *genKeyFile != "" {
		genkey(*genKeyFile)
	}

	if len(*inFile) == 0 {
		fmt.Println("[!] no input file specified (specify one with -in)")
		os.Exit(1)
	} else if len(*outFile) == 0 {
		fmt.Println("[!] no output file specified (specify one with -out)")
	}

	if (!*shouldDecrypt) && (!*shouldEncrypt) {
		fmt.Println("[!] no mode specified: specify encryption with -e and")
		fmt.Println("    decryption with -d.")
		os.Exit(1)
	} else if *shouldDecrypt && *shouldEncrypt {
		fmt.Println("[!] only one mode should be specified; either encrypt")
		fmt.Println("    or decrypt.")
		os.Exit(1)
	}

	if *shouldDecrypt {
		err = badcrypto.DecryptFile(*inFile, *outFile, key)
	} else {
		err = badcrypto.EncryptFile(*inFile, *outFile, key)
	}

	if err != nil {
		fmt.Printf("[!] %s\n", err.Error())
	} else {
		fmt.Println("[+] ok")
	}
}
Beispiel #2
0
func scan(start, stop int64, infile, savefile string) {
	for i := start; i < stop; i++ {
		key, err := genkey(i)
		if err != nil {
			fmt.Println("[!] failed to generate key:", err.Error())
		}
		tmp, err := tmpFile()
		if err != nil {
			fmt.Println("[!] failed to generate temp file:", err.Error())
			os.Exit(1)
		}
		err = badcrypto.DecryptFile(infile, tmp, key)
		if rmErr := os.Remove(tmp); rmErr != nil {
			fmt.Println("error removing", tmp, ":", err.Error())
		}
		if err != nil {
			continue
		}
		fmt.Println("[+] key was generated with timestamp", i)
		if savefile != "" {
			outfile := fmt.Sprintf("%s.%d", savefile, i)
			err = badcrypto.DecryptFile(infile, outfile, key)
			if err != nil {
				fmt.Println("[!] failed to save decrypted file:", err.Error())
			}
		}

		keyfile := fmt.Sprintf("key-%d.out", i)
		err = ioutil.WriteFile(keyfile, key, 0644)
		if err != nil {
			fmt.Println("[!] failed to write key file:", err.Error())
		}
		os.Exit(0)
	}

	fmt.Println("[!] no key found -- try expanding the search")

	os.Exit(1)
}