在高俚语中加密 AES/CBC,在角度加密中解密JS

我正在尝试在 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的值应该是多少?


红糖糍粑
浏览 88回答 2
2回答

泛舟湖上清波郎朗

Go 代码生成一个随机 IV,并在 CBC 模式下使用 AES-256 执行加密,并带有 PKCS7 填充。IV 与密文 连接,结果是 Base64 编码。密钥是通过 Utf8 编码从给定字符串生成的。IV|ciphertext请注意,Go 代码if _, err := io.ReadFull(rand.Reader, iv); err != nil {&nbsp; &nbsp; panic(err)}使用随机值初始化 IV。因此,IV的输出应该在之后。由于它是在发布代码中打印的,因此显示零IV,但与使用的IV不对应。顺便说一句,对于解密,显式输出不是必需的,因为如前所述,代码连接IV和密文。在加密JS代码中,使用零IV,可能是因为Go代码中的输出不正确。相反,IV和密文必须分开。此外,密钥作为字符串传递。这是不正确的,因为 CryptoJS 将字符串解释为密码并执行基于密码的密钥派生。相反,密钥必须作为 .由于密钥是 Go 代码中 Utf8 编码的,因此必须应用 Utf8 编码器(另请参阅其他答案)。WordArrayCBC 和 PKCS7 填充是默认值,可以指定,但不需要指定。以下加密 JS 代码中的密文是使用 Go 代码生成的:// Separate IV and ciphertextvar data&nbsp; &nbsp;= CryptoJS.enc.Base64.parse("WL7oDghTeEbjZ6QPeb/TGuDGijktQ4PZS7+wd0Ayu8Y="); // from Go codevar iv = CryptoJS.lib.WordArray.create(data.words.slice(0, 4));&nbsp; &nbsp; // first 4 words = 16 bytesvar ct = CryptoJS.lib.WordArray.create(data.words.slice(4));&nbsp; &nbsp; &nbsp; &nbsp;// rest// Decryptvar decrypted = CryptoJS.AES.decrypt(&nbsp; &nbsp; {ciphertext: ct},&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // alternatively as Base64 encoded string&nbsp; &nbsp; CryptoJS.enc.Utf8.parse("28cEVB4BUE7GKNwjuRhN3szK5E3!&q*y"),&nbsp; &nbsp;// Utf8 encode key string&nbsp; &nbsp; {iv:iv});console.log("Decrypted: ", decrypted.toString(CryptoJS.enc.Utf8)); // Decrypted:&nbsp; Test<script src="https://cdnjs.cloudflare.com/ajax/libs/crypto-js/4.0.0/crypto-js.min.js"></script>并将密文正确解密为 。Test

墨色风雨

试试这个,确保你使用相同的密钥进行加密和解密//The get method is use for decrypt the value.get(keys, value){&nbsp; &nbsp; var key = CryptoJS.enc.Utf8.parse(keys);&nbsp; &nbsp; var iv = CryptoJS.enc.Utf8.parse(keys);&nbsp; &nbsp; var decrypted = CryptoJS.AES.decrypt(value, key, {&nbsp; &nbsp; &nbsp; &nbsp; keySize: 128 / 8,&nbsp; &nbsp; &nbsp; &nbsp; iv: iv,&nbsp; &nbsp; &nbsp; &nbsp; mode: CryptoJS.mode.CBC,&nbsp; &nbsp; &nbsp; &nbsp; padding: CryptoJS.pad.Pkcs7&nbsp; &nbsp; });&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; return decrypted.toString(CryptoJS.enc.Utf8);}
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Go