Skip to content

Commit

Permalink
Refactor encryption functions and app structure for clarity and robus…
Browse files Browse the repository at this point in the history
…tness

The commit includes the following changes:
- Several variables in encryption/decryption functions are renamed for better readability and consistency.
- Data padding handling is refined to make the data processing smoother. The encryption function now includes only necessary padding, which simplifies overall data handling and reduces potential for errors.
- Application structure is refactored, moving files from 'app' to a new package 'factory', enhancing modularity and improving maintainability.
- To accomodate the above changes, the 'cmd/main.go' file has been updated, ensuring robust error handling.
- Libraries are added to the go.mod file to prepare for future updates. This is done for proactive dependency management, averting potential discrepancies in future works.

These changes are part of an ongoing effort to streamline the codebase and solidify error handling in the application.
  • Loading branch information
Septrum101 committed Sep 18, 2023
1 parent 3b2f85c commit d2b3a4c
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 24 deletions.
9 changes: 2 additions & 7 deletions app/factory.go → app/factory/factory.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package app
package factory

import (
"bytes"
Expand Down Expand Up @@ -106,12 +106,7 @@ func (f *Factory) CheckLoginAuth() error {
}
switch resp.StatusCode() {
case 200:
cipherByte := resp.Body()
if len(cipherByte)%16 != 0 {
cipherByte = utils.PKCS5Padding(cipherByte, 16)
}
_, err := utils.ECBDecrypt(cipherByte, f.Key)
if err != nil {
if _, err := utils.ECBDecrypt(resp.Body(), f.Key); err != nil {
return err
}
return nil
Expand Down
2 changes: 1 addition & 1 deletion app/model.go → app/factory/model.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package app
package factory

var (
AesKeyPool = []byte{
Expand Down
6 changes: 4 additions & 2 deletions cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"fmt"
"strings"

"github.com/thank243/zteOnu/app"
"github.com/thank243/zteOnu/app/factory"
"github.com/thank243/zteOnu/version"
)

Expand All @@ -19,7 +19,7 @@ func main() {
port := flag.Int("port", 8080, "ONU http port")
flag.Parse()

fac := app.New(*user, *passwd, *ip, *port)
fac := factory.New(*user, *passwd, *ip, *port)

fmt.Print("step [0] reset factory: ")
if err := fac.Reset(); err != nil {
Expand All @@ -32,6 +32,7 @@ func main() {
fmt.Print("step [1] request factory mode: ")
if err := fac.ReqFactoryMode(); err != nil {
fmt.Println(err)
return
} else {
fmt.Println("ok")
}
Expand All @@ -40,6 +41,7 @@ func main() {
ver, err := fac.SendSq()
if err != nil {
fmt.Println(err)
return
} else {
fmt.Println("ok")
}
Expand Down
1 change: 0 additions & 1 deletion go.sum
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
github.com/go-resty/resty/v2 v2.7.0 h1:me+K9p3uhSmXtrBZ4k9jcEAfJmuC8IivWHwaLZwPrFY=
github.com/go-resty/resty/v2 v2.7.0/go.mod h1:9PWDzw47qPphMRFfhsyk0NnSgvluHcljSMVIq3w7q0I=
golang.org/x/net v0.0.0-20211029224645-99673261e6eb h1:pirldcYWx7rx7kE5r+9WsOXPXK0+WH5+uZ7uPmJ44uM=
golang.org/x/net v0.0.0-20211029224645-99673261e6eb/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y=
golang.org/x/net v0.15.0 h1:ugBLEUaxABaB5AJqW9enI0ACdci2RUd4eP51NTBvuJ8=
golang.org/x/net v0.15.0/go.mod h1:idbUs1IY1+zTqbi8yxTbhexhEEk5ur9LInksu6HrEpk=
Expand Down
31 changes: 18 additions & 13 deletions utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,36 +11,41 @@ func ECBEncrypt(origData, key []byte) ([]byte, error) {
return nil, err
}

origData = PKCS5Padding(origData, block.BlockSize())
crypted := make([]byte, len(origData))
origData = padding(origData, block.BlockSize())
encrypted := make([]byte, len(origData))
// 对每个block进行加密
for i := 0; i < len(origData); i += block.BlockSize() {
block.Encrypt(crypted[i:i+block.BlockSize()], origData[i:i+block.BlockSize()])
block.Encrypt(encrypted[i:i+block.BlockSize()], origData[i:i+block.BlockSize()])
}
return crypted, nil
return encrypted, nil
}

func ECBDecrypt(crypted, key []byte) ([]byte, error) {
func ECBDecrypt(encrypted, key []byte) ([]byte, error) {
block, err := aes.NewCipher(key)
if err != nil {
return nil, err
}

origData := make([]byte, len(crypted))
// padding bytes
if len(encrypted)%16 != 0 {
encrypted = padding(encrypted, block.BlockSize())
}

origData := make([]byte, len(encrypted))
// 对每个block进行解密
for i := 0; i < len(crypted); i += block.BlockSize() {
block.Decrypt(origData[i:i+block.BlockSize()], crypted[i:i+block.BlockSize()])
for i := 0; i < len(encrypted); i += block.BlockSize() {
block.Decrypt(origData[i:i+block.BlockSize()], encrypted[i:i+block.BlockSize()])
}
origData = PKCS5UnPadding(origData)
origData = unPadding(origData)
return origData, nil
}

func PKCS5Padding(origData []byte, blockSize int) []byte {
func padding(origData []byte, blockSize int) []byte {
padding := blockSize - len(origData)%blockSize
padtext := bytes.Repeat([]byte{0}, padding)
return append(origData, padtext...)
padText := bytes.Repeat([]byte{0}, padding)
return append(origData, padText...)
}

func PKCS5UnPadding(origData []byte) []byte {
func unPadding(origData []byte) []byte {
return bytes.TrimRight(origData, "\x00")
}

0 comments on commit d2b3a4c

Please sign in to comment.