Go: XORKeyStream():运行时错误:无效的内存地址或空指针解引用

huangapple go评论99阅读模式
英文:

Go: XORKeyStream(): runtime error: invalid memory address or nil pointer dereference

问题

当我尝试运行以下代码时(我试图使用CFB模式的AES加密字符串的字节数组表示),我在sEnc.XORKeyStream(msgB, msgB)这一行上得到一个无效的内存地址或空指针解引用错误。文档说XORKeyStream的源和目标可以是同一个字节数组(也尝试过使用单独的目标,但没有成功),但我无法弄清楚是什么导致了这个错误。我正在使用最新版本的Go App Engine SDK在OS X Lion上。

由于在这一点上我仍然在尝试简单地加密一些东西,所以我只是生成一个随机密钥,暂时不打算存储它。

func generateKey(w http.ResponseWriter, r *http.Request) {
    key := make([]byte, 32)
    n, err := rand.Read(key)
    err = err
    if(err != nil) {
        fmt.Fprint(w, err)
        return
    }
    if(n != 32) {
        fmt.Fprint(w, "Not enough bytes read.")
        return
    }
    c, err := aes.NewCipher(key)
    if(err != nil) {
        fmt.Fprint(w, err)
    }
    
    iv := make([]byte, 32)
    n, err = rand.Read(iv)
    err = err
    if(err != nil) {
        fmt.Fprint(w, err)
        return
    }
    if(n != 32) {
        fmt.Fprint(w, "Not enough bytes read.")
        return
    }
    
    sEnc := cipher.NewCFBEncrypter(c, iv)
    
    msg := "text to be encrypted"
    msgR := strings.NewReader(msg)
    msgB := make([]byte, msgR.Len())
    n, err = msgR.Read(msgB)
    if(err != nil) {
        fmt.Fprint(w, err)
        return
    }
    if(n == 0) {
        fmt.Fprint(w, "No bytes read.")
        return
    }
    fmt.Fprint(w, msgB)
    fmt.Fprint(w, "<br>")
    sEnc.XORKeyStream(msgB, msgB)
    fmt.Fprint(w, msgB)
}

错误信息:

> 2011/08/15 15:15:58 http: panic serving : runtime error: invalid
> memory address or nil pointer dereference runtime.panic
> /private/tmp/appengine/google_appengine/goroot/src/pkg/runtime/proc.c:1041
> runtime.panicstring
> /private/tmp/appengine/google_appengine/goroot/src/pkg/runtime/runtime.c:116
> runtime.sigpanic
> /private/tmp/appengine/google_appengine/goroot/src/pkg/runtime/darwin/thread.c:470
> server.generateKey server/server.go:66 http.HandlerFunc·ServeHTTP
> /private/tmp/appengine/google_appengine/goroot/src/pkg/http/server.go:589
> http.*ServeMux·ServeHTTP
> /private/tmp/appengine/google_appengine/goroot/src/pkg/http/server.go:790
> appengine_internal.handleFilteredHTTP
> /private/tmp/appengine/google_appengine/goroot/src/pkg/appengine_sdk/go_appengine_sdk/appengine_internal/api_dev.go:58
> http.HandlerFunc·ServeHTTP
> /private/tmp/appengine/google_appengine/goroot/src/pkg/http/server.go:589
> http.*conn·serve
> /private/tmp/appengine/google_appengine/goroot/src/pkg/http/server.go:555
> runtime.goexit
> /private/tmp/appengine/google_appengine/goroot/src/pkg/runtime/proc.c:178
> INFO 2011-08-15 15:15:58,310 dev_appserver.py:4248] "GET
> /genkey/aes256 HTTP/1.1" 500 -

我已经将其余的代码发布到paste bin,但只是包含/初始化函数,没有什么特别有趣的内容。

英文:

When I try to run the following code (I'm trying to encrypt the byte array representation of a string, using AES in CFB mode) I get an invalid memory address or nil pointer dereference error, on the line sEnc.XORKeyStream(msgB, msgB). The documentation says the source and destination for XORKeyStream can be the same byte array (also tried using a separate destination, but no dice), but I can't figure out what's causing the error. I'm using the latest version of the Go App Engine SDK on OS X Lion.

Since at this point I'm still trying to get the code to simply encrypt something, I'm just generating a random key and not bothering to store it yet.

func generateKey(w http.ResponseWriter, r *http.Request) {
key := make([]byte, 32)
n, err := rand.Read(key)
err = err
if(err != nil) {
fmt.Fprint(w, err)
return
}
if(n != 32) {
fmt.Fprint(w, &quot;Not enough bytes read.&quot;)
return
}
c, err := aes.NewCipher(key)
if(err != nil) {
fmt.Fprint(w, err)
}
iv := make([]byte, 32)
n, err = rand.Read(iv)
err = err
if(err != nil) {
fmt.Fprint(w, err)
return
}
if(n != 32) {
fmt.Fprint(w, &quot;Not enough bytes read.&quot;)
return
}
sEnc := cipher.NewCFBEncrypter(c, iv)
msg := &quot;text to be encrypted&quot;
msgR := strings.NewReader(msg)
msgB := make([]byte, msgR.Len())
n, err = msgR.Read(msgB)
if(err != nil) {
fmt.Fprint(w, err)
return
}
if(n == 0) {
fmt.Fprint(w, &quot;No bytes read.&quot;)
return
}
fmt.Fprint(w, msgB)
fmt.Fprint(w, &quot;&lt;br&gt;&quot;)
sEnc.XORKeyStream(msgB, msgB)
fmt.Fprint(w, msgB)
}

The error messages:

> 2011/08/15 15:15:58 http: panic serving : runtime error: invalid
> memory address or nil pointer dereference runtime.panic
> /private/tmp/appengine/google_appengine/goroot/src/pkg/runtime/proc.c:1041
> runtime.panicstring
> /private/tmp/appengine/google_appengine/goroot/src/pkg/runtime/runtime.c:116
> runtime.sigpanic
> /private/tmp/appengine/google_appengine/goroot/src/pkg/runtime/darwin/thread.c:470
> server.generateKey server/server.go:66 http.HandlerFunc·ServeHTTP
> /private/tmp/appengine/google_appengine/goroot/src/pkg/http/server.go:589
> http.*ServeMux·ServeHTTP
> /private/tmp/appengine/google_appengine/goroot/src/pkg/http/server.go:790
> appengine_internal.handleFilteredHTTP
> /private/tmp/appengine/google_appengine/goroot/src/pkg/appengine_sdk/go_appengine_sdk/appengine_internal/api_dev.go:58
> http.HandlerFunc·ServeHTTP
> /private/tmp/appengine/google_appengine/goroot/src/pkg/http/server.go:589
> http.*conn·serve
> /private/tmp/appengine/google_appengine/goroot/src/pkg/http/server.go:555
> runtime.goexit
> /private/tmp/appengine/google_appengine/goroot/src/pkg/runtime/proc.c:178
> INFO 2011-08-15 15:15:58,310 dev_appserver.py:4248] "GET
> /genkey/aes256 HTTP/1.1" 500 -

I've posted the rest of the code to paste bin, but it's just the includes/init function, nothing all that interesting.

答案1

得分: 3

sEnc是由于错误而为nil。对于cipher.NewCFBEncrypter,"iv的长度必须与块的长度相同"。AES块大小是128位或16字节吗?例如,

iv := make([]byte, c.BlockSize())
英文:

sEnc is nil as a result of an error. For cipher.NewCFBEncrypter, "the iv must be the same length as the Block's block." The AES block size is 128 bits or 16 bytes? For example,

iv := make([]byte, c.BlockSize())

huangapple
  • 本文由 发表于 2011年8月15日 23:21:15
  • 转载请务必保留本文链接:https://go.coder-hub.com/7066801.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定