ag_tools/cmd/rsa/decryption.go

127 lines
3.7 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package rsa
import (
"fmt"
"io"
"os"
"archgrid.xyz/ag/tools/types"
"archgrid.xyz/ag/toolsbox/encryption/rsa"
"archgrid.xyz/ag/toolsbox/serialize/base64"
"archgrid.xyz/ag/toolsbox/serialize/hex"
"github.com/spf13/cobra"
)
var (
rsaDecryptionFromFile bool
rsaDecryptionToFile bool
rsaDecryptionFromEncoding types.ResultEncoding = types.ResultInHex
rsaDecryptionInputFile string
rsaDecryptionOutputFile string
)
var rsaDecryptionCmd = &cobra.Command{
Use: "decrypt",
Short: `使用RSA算法对给定的内容进行解密`,
Long: `使用RSA算法对给定的内容进行解密生成RSA密钥。如果指定了输入文件则从输入文件中读取内容进行解密否则从命令行读取内容进行解密。如果指定了输出文件则将解密后的内容写入到输出文件中否则将解密后的内容输出到命令行。`,
Args: cobra.MaximumNArgs(1),
Run: rsaDecryptionExecute,
}
func rsaDecryptionExecute(cmd *cobra.Command, args []string) {
var (
content []byte
cert []byte
keys *rsa.KeyPair
err error
)
if len(privateKeyFile) > 0 {
file, err := os.Open(privateKeyFile)
if err != nil {
fmt.Printf("无法打开RSA私钥文件%s\n", err)
return
}
cert, err = io.ReadAll(file)
if err != nil {
fmt.Printf("无法读取RSA私钥文件%s\n", err)
return
}
} else {
fmt.Println("必须提供RSA私钥文件才能够解密。")
return
}
keys, err = rsa.NewFromPEM(cert)
if err != nil {
fmt.Printf("无法解析RSA私钥%s\n", err)
return
}
if rsaDecryptionFromFile {
if len(rsaDecryptionInputFile) == 0 {
fmt.Println("必须指定输入文件名。")
return
}
file, err := os.Open(rsaDecryptionInputFile)
if err != nil {
fmt.Printf("无法打开输入文件:%s\n", err)
return
}
content, err = io.ReadAll(file)
if err != nil {
fmt.Printf("无法读取输入文件:%s\n", err)
return
}
} else {
if len(args) == 0 {
fmt.Println("必须指定待解密的内容。")
return
}
switch rsaDecryptionFromEncoding {
case types.ResultInHex:
content, err = hex.FromHex(args[0])
if err != nil {
fmt.Printf("无法解析十六进制编码:%s\n", err)
return
}
case types.ResultInBase64:
content, err = base64.FromBase64(args[0])
if err != nil {
fmt.Printf("无法解析Base64编码%s\n", err)
return
}
}
}
decrypted, err := keys.Decrypt(content)
if err != nil {
fmt.Printf("无法解密:%s\n", err)
return
}
if rsaDecryptionToFile {
if len(rsaDecryptionOutputFile) == 0 {
fmt.Println("必须指定输出文件名。")
return
}
file, err := os.OpenFile(rsaDecryptionOutputFile, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0600)
if err != nil {
fmt.Printf("无法打开输出文件:%s\n", err)
return
}
defer file.Close()
_, err = file.Write(decrypted)
if err != nil {
fmt.Printf("无法写入输出文件:%s\n", err)
return
}
} else {
fmt.Printf("解密结果:%s\n", decrypted)
}
}
func init() {
rsaDecryptionCmd.Flags().BoolVarP(&rsaDecryptionFromFile, "from-file", "f", false, "从文件中读取待解密的内容")
rsaDecryptionCmd.Flags().BoolVarP(&rsaDecryptionToFile, "to-file", "t", false, "将解密后的内容写入到文件中")
rsaDecryptionCmd.Flags().VarP(&rsaDecryptionFromEncoding, "from-encoding", "e", "指定输出格式可选值为hex、base64默认值为hex。对文件输入格式不起作用。")
rsaDecryptionCmd.Flags().StringVarP(&rsaDecryptionInputFile, "input-file", "i", "", "指定输入文件名")
rsaDecryptionCmd.Flags().StringVarP(&rsaDecryptionOutputFile, "output-file", "o", "", "指定输出文件名")
rsaCmd.AddCommand(rsaDecryptionCmd)
}