angular - 在 golang 中对 AES 解密的 panic 返回

标签 angular authentication go encryption aes

我在一个 Angular 应用程序上实现了 AES 加密,该应用程序将加密的字符串发送到用 golang 编写的 REST api,然后解密它以验证它是否是有效 key 。

加密和解密分别在 Angular 应用程序和 golang 上进行,但是当我们解密从 Angular 应用程序发送的字符串时,rest API 返回 Panic

以下是我在应用程序上加密组件文件的代码

import * as CryptoJS from 'crypto-js';

var key = "NPZ8fvABP5pKwU3"; // passphrase used to encrypt 
let encrypted_text = CryptoJS.AES.encrypt('Hello World', 'NPZ8fvABP5pKwU3');

当我用下面的代码解密它时,它在 Angular 应用程序上返回“Hello World”

var bytes  = CryptoJS.AES.decrypt(encrypted_text.toString(), 'NPZ8fvABP5pKwU3');
var plaintext = bytes.toString(CryptoJS.enc.Utf8);
console.log(plaintext);

它无法在 main.go 文件中使用以下代码在 rest api 中返回相同的文本

package main

import (
    "crypto/aes"
    "crypto/cipher"
    "crypto/md5"
    "crypto/rand"
    "encoding/hex"
    "fmt"
    "io"
)

func decrypt(data []byte, passphrase string) []byte {
    key := []byte(createHash(passphrase))
    block, err := aes.NewCipher(key)
    if err != nil {
        panic(err.Error())
    }
    gcm, err := cipher.NewGCM(block)
    if err != nil {
        panic(err.Error())
    }
    nonceSize := gcm.NonceSize()
    nonce, ciphertext := data[:nonceSize], data[nonceSize:]
    plaintext, err := gcm.Open(nil, nonce, ciphertext, nil)
    if err != nil {
        panic(err.Error())
    }
    return plaintext
}

func main() {
    key2 := "NPZ8fvABP5pKwU3"
    key3 := []byte("encrypted string from angular app")

    plaintext := decrypt(key3, key2)
    fmt.Printf(string(plaintext))
}

func createHash(key string) string {
 hasher := md5.New()
 hasher.Write([]byte(key))
 return hex.EncodeToString(hasher.Sum(nil))
}

https://play.golang.org/p/iGYyg0RB-Zi

返回错误

panic: cipher: message authentication failed

最佳答案

您不需要使用createHash 函数,只需使用 key 即可。问题是您的 key 长 15 个字节,而 aes.NewCipher 需要 16、24 或 32 个字节。只需更改您的 key 并使用此代码:

package main

import (
    "crypto/aes"
    "crypto/cipher"
    "crypto/md5"

    "encoding/hex"
    "fmt"
)

func decrypt(data []byte, passphrase string) []byte {
    key := []byte(passphrase)
    block, err := aes.NewCipher(key)
    if err != nil {
        panic(err.Error())
    }
    gcm, err := cipher.NewGCM(block)
    if err != nil {
        panic(err.Error())
    }
    nonceSize := gcm.NonceSize()
    nonce, ciphertext := data[:nonceSize], data[nonceSize:]
    plaintext, err := gcm.Open(nil, nonce, ciphertext, nil)
    if err != nil {
        panic(err.Error())
    }
    return plaintext
}

func main() {
    key2 := "NPZ8fvABP5pKwU3"
    key3 := []byte("encrypted string from angular app")

    plaintext := decrypt(key3, key2)
    fmt.Printf(string(plaintext))
}

关于angular - 在 golang 中对 AES 解密的 panic 返回,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51077945/

相关文章:

Angular 错误 : 'app-header' is not a known element:

angular - 升级到 Angular 9 后,无法在类型文件中找到变量

Angular:在 VSCode 中运行 ngcc 会导致性能问题

javascript - 添加新模块 AngularJS FountainJS (Yeoman)

c# - .NET 4.5 UserPrincipal.FindByIdentity (System.DirectoryServices.AccountManagement) 中的错误

python - 一个自写的装饰器(比如@login_required)实际上在做什么?

go - SharedInformerFactory WithOptions - 无法根据标签进行过滤

authentication - 通过 OpenID、Oauth 或 Facebook Connect 进行身份验证后重定向回页面

go - 如何停止从 UDP 读取的 goroutine?

go - slice 作为 map 中的键