Add padding processing
This commit is contained in:
parent
4eecf0b3e5
commit
c537f79ccc
1 changed files with 48 additions and 1 deletions
49
crypto.go
49
crypto.go
|
@ -43,7 +43,7 @@ func DecryptAndVerify(input []byte, key *rsa.PrivateKey, pub *rsa.PublicKey, sig
|
|||
body := make([]byte, len(encBody))
|
||||
dec := cipher2.NewCBCDecrypter(cipher, iv)
|
||||
dec.CryptBlocks(body, encBody)
|
||||
err = json.Unmarshal(body, v)
|
||||
err = json.Unmarshal(unpad(body), v)
|
||||
|
||||
if err != nil {
|
||||
return err
|
||||
|
@ -111,7 +111,54 @@ func EncryptAndSign(v interface{}, key *rsa.PrivateKey, pub *rsa.PublicKey, sign
|
|||
encBody := make([]byte, len(body))
|
||||
enc := cipher2.NewCBCEncrypter(cipher, iv)
|
||||
enc.CryptBlocks(encBody, body)
|
||||
|
||||
copy(pad(encBody), encBody[len(encBody):])
|
||||
writer.Write([]byte(hex.EncodeToString(encBody)))
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func pad(body []byte) []byte {
|
||||
rest := len(body) % 8
|
||||
if rest == 0 {
|
||||
padLen := getPad(body)
|
||||
if padLen != 0 {
|
||||
return []byte{8, 8, 8, 8, 8, 8, 8, 8}
|
||||
}
|
||||
|
||||
return []byte{}
|
||||
}
|
||||
|
||||
pad := make([]byte, rest)
|
||||
|
||||
for i := 0; i < rest; i++ {
|
||||
pad[i] = byte(rest)
|
||||
}
|
||||
|
||||
return pad
|
||||
}
|
||||
|
||||
func getPad(body []byte) int {
|
||||
lastIndex := len(body) - 1
|
||||
padLen := body[lastIndex]
|
||||
if 1 < padLen && padLen < 9 {
|
||||
isPadding := true
|
||||
|
||||
for i := byte(0); i < padLen; i++ {
|
||||
if body[-i] != padLen {
|
||||
isPadding = false
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
if isPadding {
|
||||
return int(padLen)
|
||||
}
|
||||
}
|
||||
|
||||
return 0
|
||||
}
|
||||
|
||||
func unpad(body []byte) []byte {
|
||||
return body[:len(body)-getPad(body)]
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue