2017-09-28 2 views
1

Dies ist, was ich in Node.js haben:Golang Decrypt AES 256 CBC base64 von NodeJS

var crypto = require('crypto') 

function encryptstring(str) { 
    var cipher = crypto.createCipheriv('aes-256-cbc', 'NFd6N3v1nbL47FK0xpZjxZ7NY4fYpNYd', 'TestingIV1234567'), 
     encrypted = cipher.update(str, 'utf-8', 'base64'); 
    encrypted += cipher.final('base64'); 
    return encrypted; 
} 

console.log(encryptstring("Testing 111111111111111111111111111111111111111111")) 

Das zurückgibt: w2f0vBP2hRfgVqssqOluk68Qxkc9LXFESc0ZGzPBq3p6f/x/LbwBbg1XOoRr7I/DAtESJGdweKG6nL9m8RfewA==

Dies ist, was ich in Go haben:

package main 

import (
    "crypto/aes" 
    "crypto/cipher" 
    "encoding/base64" 
    "fmt" 
) 

// decrypt from base64 to decrypted string 
func decrypt(key []byte, iv []byte, cryptoText string) string { 
    ciphertext, _ := base64.URLEncoding.DecodeString(cryptoText) 
    block, err := aes.NewCipher(key) 
    if err != nil { 
     panic(err) 
    } 
    if len(ciphertext) < aes.BlockSize { 
     panic("ciphertext too short") 
    } 

    ciphertext = ciphertext[aes.BlockSize:] 
    stream := cipher.NewCFBDecrypter(block, iv) 
    stream.XORKeyStream(ciphertext, ciphertext) 

    return fmt.Sprintf("%s", ciphertext) 
} 

func main() { 
    encKey := "NFd6N3v1nbL47FK0xpZjxZ7NY4fYpNYd" 
    iv := "TestingIV1234567" 
    stringtodecrypt := "w2f0vBP2hRfgVqssqOluk68Qxkc9LXFESc0ZGzPBq3p6f/x/LbwBbg1XOoRr7I/DAtESJGdweKG6nL9m8RfewA==" 

    stringtodecrypt = decrypt([]byte(encKey), []byte(iv), stringtodecrypt) 

    fmt.Println(string(stringtodecrypt)) 
} 

Das endet mit _▒▒▒6▒▒d,O▒ob"▒

Eine Menge der Go-Code wurde genommen n von https://gist.github.com/manishtpatel/8222606

habe ich versucht, dies auch: How do I decrypt an AES256 bit cipher in golang that was encrypted in nodejs? (mit einigen Modifikationen wie hex.DecodeString in diesem Fall nicht notwendig war), aber es würde einen Fehler werfen sagt panic: crypto/cipher: input not full blocks

Das ist mein Code war, als ich versuchte, dass:

func main() { 
    encKey := "NFd6N3v1nbL47FK0xpZjxZ7NY4fYpNYd" 
    iv := "TestingIV1234567" 
    stringtodecrypt := "w2f0vBP2hRfgVqssqOluk68Qxkc9LXFESc0ZGzPBq3p6f/x/LbwBbg1XOoRr7I/DAtESJGdweKG6nL9m8RfewA==" 

    block, err := aes.NewCipher([]byte(encKey)) 
    if err != nil { 
     panic(err) 
    } 

    mode := cipher.NewCBCDecrypter(block, []byte(iv)) 

    mode.CryptBlocks([]byte(stringtodecrypt), []byte(stringtodecrypt)) 

    fmt.Println(string(stringtodecrypt)) 
} 

ich habe viel herum gesucht, und ich kann das nicht scheinen, um herauszufinden.

Was mache ich falsch?

+0

Das erste, was ich sehe, ist, dass Sie versuchen, die base64 als zu dekodieren 'URLEncoding', kann aber nicht sein, da es'/'Zeichen enthält. Hast du 'StdEncoding' versucht? – JimB

+0

Danke für den Vorschlag, ich habe das versucht und es zurückgegeben: 'G▒ 䳾 6▒▒d, O▒ob" ▒▒G▒ {▒▒A▒zj * ơ▒▒▒ [▒▒˺' –

Antwort

2

Ihr zweiter Versuch war näher, aber du hast die base64 String zuerst nicht entschlüsseln.

Wenn Sie von der CBCDecrypter example in der Chiffre-Paket gehen, würden Sie so etwas wie dieses:

encKey := "NFd6N3v1nbL47FK0xpZjxZ7NY4fYpNYd" 
iv := "TestingIV1234567" 
ciphertext, err := base64.StdEncoding.DecodeString("w2f0vBP2hRfgVqssqOluk68Qxkc9LXFESc0ZGzPBq3p6f/x/LbwBbg1XOoRr7I/DAtESJGdweKG6nL9m8RfewA==") 
if err != nil { 
    panic(err) 
} 

block, err := aes.NewCipher([]byte(encKey)) 
if err != nil { 
    panic(err) 
} 

if len(ciphertext)%aes.BlockSize != 0 { 
    panic("ciphertext is not a multiple of the block size") 
} 

mode := cipher.NewCBCDecrypter(block, []byte(iv)) 
mode.CryptBlocks(ciphertext, ciphertext) 

fmt.Printf("%s\n", ciphertext) 

https://play.golang.org/p/16wV2UJ5Iw

Verwandte Themen