douyu5679 2018-06-28 08:16
浏览 369

在golang中对AES解密的紧急返回

I have implemented AES encryption on an angular app which sends encrypted string to a REST api written in golang which then decrypts it to verify if its a valid key or not.

Encryption and decryption is working on angular app and golang separately but rest API returns Panic when we decrypt string sent from angular app

Following is my code on app to encrypt in component file

import * as CryptoJS from 'crypto-js';

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

When I decrypt it with following code it returns "Hello World" on angular app

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

It fails to return same text in rest api with following code in main.go file

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

returned error

panic: cipher: message authentication failed
  • 写回答

1条回答 默认 最新

  • duanpao6163 2018-06-28 12:09
    关注

    You don't need to use createHash function, just use the key. The problem is that your key is 15 bytes long while aes.NewCipher expects either 16, 24 or 32 bytes. Just change you key and use this code:

    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))
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料
  • ¥15 使用R语言marginaleffects包进行边际效应图绘制