你可以使用标志(flags)来选择要调用的函数。

huangapple go评论87阅读模式
英文:

How could i use flags to select which func i want to call

问题

你好,这是我第一次尝试编写一个实际的Go命令行程序,所以请原谅它的外观。我也从互联网上找了一些代码。实际上,我想做的是在选择加密或解密时仍然能够选择源文件和目标文件。提前感谢任何帮助。我找不到任何详细解释这个问题的东西,或者至少没有我能理解的东西。

package main

import (
	"crypto/aes"
	"crypto/cipher"
	"crypto/rand"
	"errors"
	"io"
	"io/ioutil"
	"log"
	"os"
)

func decrypt(key, ciphertext []byte) (plaintext []byte, err error) {
	var block cipher.Block

	if block, err = aes.NewCipher(key); err != nil {
		return
	}

	if len(ciphertext) < aes.BlockSize {
		err = errors.New("ciphertext too short")
		return
	}

	iv := ciphertext[:aes.BlockSize]
	ciphertext = ciphertext[aes.BlockSize:]

	cfb := cipher.NewCFBDecrypter(block, iv)
	cfb.XORKeyStream(ciphertext, ciphertext)

	plaintext = ciphertext

	return
}

func encrypt(key, text []byte) (ciphertext []byte, err error) {
	var block cipher.Block

	if block, err = aes.NewCipher(key); err != nil {
		return nil, err
	}

	ciphertext = make([]byte, aes.BlockSize+len(string(text)))

	iv := ciphertext[:aes.BlockSize]
	if _, err = io.ReadFull(rand.Reader, iv); err != nil {
		return
	}

	cfb := cipher.NewCFBEncrypter(block, iv)
	cfb.XORKeyStream(ciphertext[aes.BlockSize:], text)

	return
}

func encryptFileData(srcFile, destFile string) {
	if len(os.Args) > 1 {
		srcFile = os.Args[1]
	}
	if len(os.Args) > 2 {
		destFile = os.Args[2]
	}
	var cipherText, plainText []byte
	var err error

	key := []byte("abcdefg123456789")

	plainText, _ = ioutil.ReadFile(srcFile)
	if cipherText, err = encrypt(key, plainText); err != nil {
		log.Fatal(err)
	}
	ioutil.WriteFile(destFile, cipherText, 0755)

	return
}

func decryptFileData(srcFile, destFile string) {
	if len(os.Args) > 1 {
		srcFile = os.Args[1]
	}
	if len(os.Args) > 2 {
		destFile = os.Args[2]
	}
	var cipherText, plainText []byte
	var err error

	key := []byte("abcdefg123456789")

	cipherText, _ = ioutil.ReadFile(srcFile)
	if plainText, err = decrypt(key, cipherText); err != nil {
		log.Fatal(err)
	}
	ioutil.WriteFile(destFile, plainText, 0755)

	return
}

func main() {
	encryptFileData(os.Args[1], os.Args[2])
	decryptFileData(os.Args[1], os.Args[2])
}

以上是你提供的Go代码的翻译。

英文:

Hello this is my first actually stab at writing an actual Go command line program so please forgive the appearance I also pulled some of this code off the Internet. What I am actually trying to do is have the ability to choose when i want to encrypt or decrypt while still being able to choose the src file and dest file. Thanks in advance for any help. I couldn't find anything solid explaining this or at least nothing i could make out.

package main
import (
&quot;crypto/aes&quot;
&quot;crypto/cipher&quot;
&quot;crypto/rand&quot;
&quot;errors&quot;
&quot;io&quot;
&quot;io/ioutil&quot;
&quot;log&quot;
&quot;os&quot;
)
func decrypt(key, ciphertext []byte) (plaintext []byte, err error) {
var block cipher.Block
if block, err = aes.NewCipher(key); err != nil {
return
}
if len(ciphertext) &lt; aes.BlockSize {
err = errors.New(&quot;ciphertext too short&quot;)
return
}
iv := ciphertext[:aes.BlockSize]
ciphertext = ciphertext[aes.BlockSize:]
cfb := cipher.NewCFBDecrypter(block, iv)
cfb.XORKeyStream(ciphertext, ciphertext)
plaintext = ciphertext
return
}
func encrypt(key, text []byte) (ciphertext []byte, err error) {
var block cipher.Block
if block, err = aes.NewCipher(key); err != nil {
return nil, err
}
ciphertext = make([]byte, aes.BlockSize+len(string(text)))
iv := ciphertext[:aes.BlockSize]
if _, err = io.ReadFull(rand.Reader, iv); err != nil {
return
}
cfb := cipher.NewCFBEncrypter(block, iv)
cfb.XORKeyStream(ciphertext[aes.BlockSize:], text)
return
}
func encryptFileData(srcFile, destFile string) {
if len(os.Args) &gt; 1 {
srcFile = os.Args[1]
}
if len(os.Args) &gt; 2 {
destFile = os.Args[2]
}
var cipherText, plainText []byte
var err error
key := []byte(&quot;abcdefg123456789&quot;)
plainText, _ = ioutil.ReadFile(srcFile)
if cipherText, err = encrypt(key, plainText); err != nil {
log.Fatal(err)
}
ioutil.WriteFile(destFile, cipherText, 0755)
return
}
func decryptFileData(srcFile, destFile string) {
if len(os.Args) &gt; 1 {
srcFile = os.Args[1]
}
if len(os.Args) &gt; 2 {
destFile = os.Args[2]
}
var cipherText, plainText []byte
var err error
key := []byte(&quot;abcdefg123456789&quot;)
cipherText, _ = ioutil.ReadFile(srcFile)
if plainText, err = decrypt(key, cipherText); err != nil {
log.Fatal(err)
}
ioutil.WriteFile(destFile, plainText, 0755)
return
}
func main() {
encryptFileData(os.Args[1], os.Args[2])
decryptFileData(os.Args[1], os.Args[2])
}

答案1

得分: 4

使用flag包。例如:

func main() {
    encrypt := flag.Bool("encrypt", false, "加密文件")
    decrypt := flag.Bool("decrypt", false, "解密文件")
    flag.Parse()

    srcFile, destFile := flag.Arg(0), flag.Arg(1)
    if *encrypt {
        encryptFileData(srcFile, destFile)
    }

    if *decrypt {
        decryptFileData(srcFile, destFile)
    }
}
英文:

Use the flag package. For example:

func main() {
encrypt := flag.Bool(&quot;encrypt&quot;, false, &quot;encrypt file&quot;)
decrypt := flag.Bool(&quot;decrypt&quot;, false, &quot;decrypt file&quot;)
flag.Parse()
srcFile, destFile := flag.Arg(0), flag.Arg(1)
if *encrypt {
encryptFileData(srcFile, destFile)
}
if *decrypt {
decryptFileData(srcFile, destFile)
}
}

huangapple
  • 本文由 发表于 2016年3月3日 10:58:36
  • 转载请务必保留本文链接:https://go.coder-hub.com/35762354.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定