我正在尝试在 Go 中加密数据,并使用带有 PKCS7 填充的 AES CBC 模式以角度方式对其进行解密。但是当我尝试解密Angular中的数据时,它没有返回任何内容
转到代码:
package main
import (
"bytes"
"crypto/aes"
"crypto/cipher"
"crypto/rand"
"encoding/base64"
"fmt"
"io"
)
func main() {
secret := []byte("28cEVB4BUE7GKNwjuRhN3szK5E3!&q*y")
data := []byte("Test")
encResult, err := Encrypt(data, secret)
fmt.Println("encResult", encResult)
fmt.Println("enc err", err)
// encrypted := "U2FsdGVkX1+LU7rE47VtIDwGIOsJa05BzOmAzQfdbVk="
result, err := Dncrypt(encResult, secret)
fmt.Println("decrypted result", result)
fmt.Println("decryption err", err)
}
/*CBC encryption Follow the example code of the golang standard library
But there is no padding inside, so make up
*/
// Use PKCS7 to fill, IOS is also 7
func PKCS7Padding(ciphertext []byte, blockSize int) []byte {
padding := blockSize - len(ciphertext)%blockSize
padtext := bytes.Repeat([]byte{byte(padding)}, padding)
return append(ciphertext, padtext...)
}
func PKCS7UnPadding(origData []byte) []byte {
length := len(origData)
unpadding := int(origData[length-1])
return origData[:(length - unpadding)]
}
//aes encryption, filling the 16 bits of the key key, 24, 32 respectively corresponding to AES-128, AES-192, or AES-256.
func AesCBCEncrypt(rawData, key []byte) ([]byte, error) {
block, err := aes.NewCipher(key)
if err != nil {
panic(err)
}
//fill the original
blockSize := block.BlockSize()
rawData = PKCS7Padding(rawData, blockSize)
// Initial vector IV must be unique, but does not need to be kept secret
cipherText := make([]byte, blockSize+len(rawData))
//block size 16
iv := cipherText[:blockSize]
fmt.Println("iv", iv)
if _, err := io.ReadFull(rand.Reader, iv); err != nil {
panic(err)
}
//block size and initial vector size must be the same
mode := cipher.NewCBCEncrypter(block, iv)
mode.CryptBlocks(cipherText[blockSize:], rawData)
return cipherText, nil
}
我从加密JS解密方法中得到一个空响应。
对于加密JS,iv的值应该是多少?
泛舟湖上清波郎朗
墨色风雨
相关分类