英文:
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 (
"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])
}
答案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("encrypt", false, "encrypt file")
decrypt := flag.Bool("decrypt", false, "decrypt file")
flag.Parse()
srcFile, destFile := flag.Arg(0), flag.Arg(1)
if *encrypt {
encryptFileData(srcFile, destFile)
}
if *decrypt {
decryptFileData(srcFile, destFile)
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论