Compare commits
No commits in common. "master" and "0.0.1" have entirely different histories.
@ -8,8 +8,6 @@ go 实现 AES 加解密文件
|
|||||||
``` cmd
|
``` cmd
|
||||||
PS C:\Users\61778\Desktop> .\goED.exe --help
|
PS C:\Users\61778\Desktop> .\goED.exe --help
|
||||||
Usage of C:\Users\61778\Desktop\goED.exe:
|
Usage of C:\Users\61778\Desktop\goED.exe:
|
||||||
-key string
|
|
||||||
加密秘钥-限制大小写字母数字16位
|
|
||||||
-mode string
|
-mode string
|
||||||
加密或者解密 en de (default "de")
|
加密或者解密 en de (default "de")
|
||||||
-outPath string
|
-outPath string
|
||||||
|
85
main.go
85
main.go
@ -4,11 +4,33 @@ import (
|
|||||||
"flag"
|
"flag"
|
||||||
"fmt"
|
"fmt"
|
||||||
"goED/util"
|
"goED/util"
|
||||||
|
"io/ioutil"
|
||||||
|
"os"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
func readFileToStr(path string) string {
|
||||||
|
content, err := ioutil.ReadFile(path)
|
||||||
|
if err != nil {
|
||||||
|
fmt.Println("read file failed, err:", err)
|
||||||
|
return ""
|
||||||
|
}
|
||||||
|
return string(content)
|
||||||
|
}
|
||||||
|
|
||||||
|
func genFile(str, path string) {
|
||||||
|
file, err := os.OpenFile(path, os.O_CREATE|os.O_TRUNC|os.O_WRONLY, 0666)
|
||||||
|
if err != nil {
|
||||||
|
fmt.Println("open file failed, err:", err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
defer file.Close()
|
||||||
|
file.Write([]byte(str)) //写入字节切片数据
|
||||||
|
//file.WriteString("hello go") //直接写入字符串数据
|
||||||
|
}
|
||||||
|
|
||||||
func test() {
|
func test() {
|
||||||
//content, err := ioutil.ReadFile("C:\\Users\\61778\\Desktop\\HelloWorld.class")
|
//content, err := ioutil.ReadFile("C:\\Users\\61778\\Desktop\\HelloWorld.class")
|
||||||
strContent := util.ReadFileToStr("C:\\METSIM\\example\\gascleaning_1.5_30.sfw")
|
strContent := readFileToStr("C:\\METSIM\\example\\gascleaning_1.5_30.sfw")
|
||||||
//fmt.Println(strContent)
|
//fmt.Println(strContent)
|
||||||
Encrypt, _ := util.AesEcpt.AesBase64Encrypt(strContent)
|
Encrypt, _ := util.AesEcpt.AesBase64Encrypt(strContent)
|
||||||
//fmt.Printf("Encrypt: %v\n", Encrypt)
|
//fmt.Printf("Encrypt: %v\n", Encrypt)
|
||||||
@ -16,52 +38,65 @@ func test() {
|
|||||||
//fmt.Printf("Decrypt: %v\n", Decrypt)
|
//fmt.Printf("Decrypt: %v\n", Decrypt)
|
||||||
fmt.Printf("明文长度: %v\n解密后长度: %v\n", len(strContent), len(Decrypt))
|
fmt.Printf("明文长度: %v\n解密后长度: %v\n", len(strContent), len(Decrypt))
|
||||||
}
|
}
|
||||||
|
func testGenFile(path, enPath, dePath string) {
|
||||||
|
strContent := readFileToStr(path)
|
||||||
|
Encrypt, _ := util.AesEcpt.AesBase64Encrypt(strContent)
|
||||||
|
genFile(Encrypt, enPath)
|
||||||
|
//fmt.Printf("Encrypt: %v\n", Encrypt)
|
||||||
|
Decrypt, _ := util.AesEcpt.AesBase64Decrypt(Encrypt)
|
||||||
|
genFile(Decrypt, dePath)
|
||||||
|
//fmt.Printf("Decrypt: %v\n", Decrypt)
|
||||||
|
fmt.Printf("明文长度: %v\n解密后长度: %v\n", len(strContent), len(Decrypt))
|
||||||
|
}
|
||||||
|
|
||||||
|
func enFile(path string) {
|
||||||
|
enFileToOutPath(path, path)
|
||||||
|
}
|
||||||
|
func enFileToOutPath(path, outPath string) {
|
||||||
|
strContent := readFileToStr(path)
|
||||||
|
Encrypt, _ := util.AesEcpt.AesBase64Encrypt(strContent)
|
||||||
|
genFile(Encrypt, outPath)
|
||||||
|
}
|
||||||
|
func deFile(path string) {
|
||||||
|
deFileToOutPath(path, path)
|
||||||
|
}
|
||||||
|
func deFileToOutPath(path string, outPath string) {
|
||||||
|
strContent := readFileToStr(path)
|
||||||
|
Decrypt, _ := util.AesEcpt.AesBase64Decrypt(strContent)
|
||||||
|
genFile(Decrypt, outPath)
|
||||||
|
}
|
||||||
|
|
||||||
var mode = flag.String("mode", "de", "加密或者解密 en de")
|
var mode = flag.String("mode", "de", "加密或者解密 en de")
|
||||||
var path = flag.String("path", "", "文件路径")
|
var path = flag.String("path", "", "文件路径")
|
||||||
var outPath = flag.String("outPath", "", "指定输出文件路径")
|
var outPath = flag.String("outPath", "", "指定输出文件路径")
|
||||||
var key = flag.String("key", "", "加密秘钥-限制大小写字母数字16位 可以不指定")
|
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
flag.Parse()
|
flag.Parse()
|
||||||
//验证操作文件路径是否有效
|
//fmt.Println(*mode)
|
||||||
flag, message := util.PathExists(*path)
|
//fmt.Println(*path)
|
||||||
if !flag {
|
|
||||||
fmt.Printf(message)
|
|
||||||
} else {
|
|
||||||
// 输出路径设置
|
|
||||||
var out string = *path
|
var out string = *path
|
||||||
var modeName string
|
var modeName string
|
||||||
if len(*outPath) != 0 {
|
if len(*outPath) != 0 {
|
||||||
out = *outPath
|
out = *outPath
|
||||||
}
|
}
|
||||||
//验证秘钥是否有效
|
if len(*path) == 0 {
|
||||||
if len(*key) == 16 {
|
fmt.Printf("path 不能为空")
|
||||||
util.InitBykey(*key)
|
} else if *mode != "en" && *mode != "de" {
|
||||||
} else if (len(*key) > 0) && (len(*key) < 16) {
|
|
||||||
fmt.Printf("秘钥限制大小写字母数字16位")
|
|
||||||
}
|
|
||||||
//验证mode 输入是否正确
|
|
||||||
if *mode != "en" && *mode != "de" {
|
|
||||||
fmt.Printf("mode 只能是 en 或者 de")
|
fmt.Printf("mode 只能是 en 或者 de")
|
||||||
//执行加密
|
|
||||||
} else if *mode == "en" {
|
} else if *mode == "en" {
|
||||||
modeName = "加密"
|
modeName = "加密"
|
||||||
util.EnFileToOutPath(*path, out)
|
enFileToOutPath(*path, out)
|
||||||
//执行解密
|
|
||||||
} else if *mode == "de" {
|
} else if *mode == "de" {
|
||||||
modeName = "解密"
|
modeName = "解密"
|
||||||
util.DeFileToOutPath(*path, out)
|
deFileToOutPath(*path, out)
|
||||||
}
|
}
|
||||||
fmt.Println(modeName + "成功,被" + modeName + "文件是在: " + *path + " " + modeName + "后文件生产在:" + out)
|
fmt.Println(modeName + "成功,被" + modeName + "文件是在: " + *path + " " + modeName + "后文件生产在:" + out)
|
||||||
}
|
|
||||||
|
|
||||||
//test()
|
//test()
|
||||||
// TestGenFile("C:\\METSIM\\example\\gascleaning_1.5_30.sfw",
|
// testGenFile("C:\\METSIM\\example\\gascleaning_1.5_30.sfw",
|
||||||
// "C:\\Users\\61778\\Desktop\\testGoED\\gascleaning_1.5_30.sfw.EN",
|
// "C:\\Users\\61778\\Desktop\\testGoED\\gascleaning_1.5_30.sfw.EN",
|
||||||
// "C:\\Users\\61778\\Desktop\\testGoED\\gascleaning_1.5_30.sfw")
|
// "C:\\Users\\61778\\Desktop\\testGoED\\gascleaning_1.5_30.sfw")
|
||||||
// EnFile("C:\\Users\\61778\\Desktop\\HelloWorld.class")
|
// enFile("C:\\Users\\61778\\Desktop\\HelloWorld.class")
|
||||||
// DeFile("C:\\Users\\61778\\Desktop\\HelloWorld.class")
|
// deFile("C:\\Users\\61778\\Desktop\\HelloWorld.class")
|
||||||
|
|
||||||
// cmd := exec.Command("cmd", "/C", "C:\\METSIM\\METSIM.exe",
|
// cmd := exec.Command("cmd", "/C", "C:\\METSIM\\METSIM.exe",
|
||||||
// "MOD=C:\\Users\\61778\\Desktop\\testGoED\\gascleaning_1.5_30.sfw", "SIL=1")
|
// "MOD=C:\\Users\\61778\\Desktop\\testGoED\\gascleaning_1.5_30.sfw", "SIL=1")
|
||||||
|
@ -26,16 +26,6 @@ func init() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func InitBykey(key string) {
|
|
||||||
AesEcpt.key = []byte(key)
|
|
||||||
AesEcpt.iv = []byte("aebksHkG4jAEk2Ag")
|
|
||||||
var err error
|
|
||||||
AesEcpt.block, err = aes.NewCipher(AesEcpt.key)
|
|
||||||
if err != nil {
|
|
||||||
panic(err)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// 加密
|
// 加密
|
||||||
func (a *AesEncrypter) AesBase64Encrypt(in string) (string, error) {
|
func (a *AesEncrypter) AesBase64Encrypt(in string) (string, error) {
|
||||||
origData := []byte(in)
|
origData := []byte(in)
|
||||||
|
@ -1,71 +0,0 @@
|
|||||||
package util
|
|
||||||
|
|
||||||
import (
|
|
||||||
"fmt"
|
|
||||||
"io/ioutil"
|
|
||||||
"os"
|
|
||||||
)
|
|
||||||
|
|
||||||
//PathExists 判断一个文件或文件夹是否存在
|
|
||||||
//输入文件路径,根据返回的bool值来判断文件或文件夹是否存在
|
|
||||||
func PathExists(path string) (bool, string) {
|
|
||||||
if len(path) == 0 {
|
|
||||||
return false, "path 不能为空"
|
|
||||||
}
|
|
||||||
_, err := os.Stat(path)
|
|
||||||
if err == nil {
|
|
||||||
return true, "存在"
|
|
||||||
}
|
|
||||||
if os.IsNotExist(err) {
|
|
||||||
return false, "path路径文件不存在"
|
|
||||||
}
|
|
||||||
return false, "path路径文件不存在"
|
|
||||||
}
|
|
||||||
|
|
||||||
func ReadFileToStr(path string) string {
|
|
||||||
content, err := ioutil.ReadFile(path)
|
|
||||||
if err != nil {
|
|
||||||
fmt.Println("read file failed, err:", err)
|
|
||||||
return ""
|
|
||||||
}
|
|
||||||
return string(content)
|
|
||||||
}
|
|
||||||
|
|
||||||
func GenFile(str, path string) {
|
|
||||||
file, err := os.OpenFile(path, os.O_CREATE|os.O_TRUNC|os.O_WRONLY, 0666)
|
|
||||||
if err != nil {
|
|
||||||
fmt.Println("open file failed, err:", err)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
defer file.Close()
|
|
||||||
file.Write([]byte(str)) //写入字节切片数据
|
|
||||||
//file.WriteString("hello go") //直接写入字符串数据
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestGenFile(path, enPath, dePath string) {
|
|
||||||
strContent := ReadFileToStr(path)
|
|
||||||
Encrypt, _ := AesEcpt.AesBase64Encrypt(strContent)
|
|
||||||
GenFile(Encrypt, enPath)
|
|
||||||
//fmt.Printf("Encrypt: %v\n", Encrypt)
|
|
||||||
Decrypt, _ := AesEcpt.AesBase64Decrypt(Encrypt)
|
|
||||||
GenFile(Decrypt, dePath)
|
|
||||||
//fmt.Printf("Decrypt: %v\n", Decrypt)
|
|
||||||
fmt.Printf("明文长度: %v\n解密后长度: %v\n", len(strContent), len(Decrypt))
|
|
||||||
}
|
|
||||||
|
|
||||||
func EnFile(path string) {
|
|
||||||
EnFileToOutPath(path, path)
|
|
||||||
}
|
|
||||||
func EnFileToOutPath(path, outPath string) {
|
|
||||||
strContent := ReadFileToStr(path)
|
|
||||||
Encrypt, _ := AesEcpt.AesBase64Encrypt(strContent)
|
|
||||||
GenFile(Encrypt, outPath)
|
|
||||||
}
|
|
||||||
func DeFile(path string) {
|
|
||||||
DeFileToOutPath(path, path)
|
|
||||||
}
|
|
||||||
func DeFileToOutPath(path string, outPath string) {
|
|
||||||
strContent := ReadFileToStr(path)
|
|
||||||
Decrypt, _ := AesEcpt.AesBase64Decrypt(strContent)
|
|
||||||
GenFile(Decrypt, outPath)
|
|
||||||
}
|
|
Loading…
x
Reference in New Issue
Block a user