You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
star/aes/aes.go

116 lines
2.3 KiB
Go

2 years ago
package aes
import (
"errors"
"fmt"
"io"
"os"
"b612.me/starcrypto"
"b612.me/staros"
)
func EncodeStr(str string, key []byte) string {
if len(key) < 16 {
key = starcrypto.Md5(key)
} else {
key = key[:len(key)/16*16]
if len(key) > 32 {
key = key[:32]
}
}
ensdata := starcrypto.AesEncryptCFBNoBlock([]byte(str), key)
return starcrypto.Base91EncodeToString(ensdata)
}
func DecodeStr(str string, key []byte) string {
if len(key) < 16 {
key = starcrypto.Md5(key)
} else {
key = key[:len(key)/16*16]
}
strtmp := starcrypto.Base91DecodeString(str)
str = string(strtmp)
return string(starcrypto.AesDecryptCFBNoBlock([]byte(str), key))
}
func EncodeFile(fpath, out string, key []byte) error {
if len(key) < 16 {
key = starcrypto.Md5(key)
} else {
key = key[:len(key)/16*16]
}
if !staros.Exists(fpath) {
return errors.New("SrcFile Not Exists")
}
fpsrc, err := os.Open(fpath)
if err != nil {
return err
}
defer fpsrc.Close()
fpdst, err := os.Create(out)
if err != nil {
return err
}
defer fpdst.Close()
bufsize := 1024 * 1024 //1MB
stat, _ := fpsrc.Stat()
buf := make([]byte, bufsize)
var sumAll int64
for {
n, err := fpsrc.Read(buf)
if err != nil && err != io.EOF {
return err
}
fmt.Printf("已完成:%.2f%%\r", float64(sumAll)/float64(stat.Size())*100)
sumAll += int64(n)
encodeBytes := starcrypto.AesEncryptCFBNoBlock(buf[:n], key)
fpdst.Write(encodeBytes)
if err == io.EOF {
fmt.Print("已完成100% \n")
break
}
}
return nil
}
func DecodeFile(fpath, out string, key []byte) error {
if len(key) < 16 {
key = starcrypto.Md5(key)
} else {
key = key[:len(key)/16*16]
}
if !staros.Exists(fpath) {
return errors.New("SrcFile Not Exists")
}
fpsrc, err := os.Open(fpath)
if err != nil {
return err
}
defer fpsrc.Close()
fpdst, err := os.Create(out)
if err != nil {
return err
}
defer fpdst.Close()
bufsize := 1024 * 1024 //1MB
stat, _ := fpsrc.Stat()
buf := make([]byte, bufsize)
var sumAll int64
for {
n, err := fpsrc.Read(buf)
if err != nil && err != io.EOF {
return err
}
fmt.Printf("已完成:%.2f%%\r", float64(sumAll)/float64(stat.Size())*100)
sumAll += int64(n)
encodeBytes := starcrypto.AesDecryptCFBNoBlock(buf[:n], key)
fpdst.Write(encodeBytes)
if err == io.EOF {
fmt.Print("已完成100% \n")
break
}
}
return nil
}