にAES 256 CTRを使用して復号化ノードJSでの暗号化: -私はノードJSコードに続く使用して、いくつかのJSONテキストを暗号化してきたGolang
var algorithm = 'aes-256-ctr';
var crypto = require('crypto');
var password = "6A80FD8D38D579D1090F6CDB62C729311781E4BA31CD7D804BD7BF5AEC3BFC2D"
var typedRequest = {"abc":"cde"}
var cipher = crypto.createCipher(algorithm, password);
var hashRequest = cipher.update(JSON.stringify(typedRequest),
'utf8', 'hex');
hashRequest += cipher.final('hex');
そして今、私はGolangでこのencryptedTextを解読したかったです。しかし、私はこれを行う方法を見つけることができません.GolangのAES 256 CTRのほぼすべての復号化ロジックの例では、復号化中に常にIVが必要だが、Node JSではこれを使用していないことがわかった。私はGolangで何かを書かれているが、そのが正しく解読し、今のところ、エラーを与えていない: -
package main
import (
"crypto/aes"
"crypto/cipher"
"encoding/hex"
"fmt"
)
func main() {
encKey := "6A80FD8D38D579D1090F6CDB62C729311781E4BA31CD7D804BD7BF5AEC3BFC2D"
cipherText := "746c17cd10f8f86646f843ac2a"
encKeyDecoded, err := hex.DecodeString(encKey)
if err != nil {
panic(err)
}
cipherTextDecoded, err := hex.DecodeString(cipherText)
if err != nil {
panic(err)
}
iv := cipherTextDecoded[:aes.BlockSize]
block, err := aes.NewCipher([]byte(encKeyDecoded))
if err != nil {
panic(err)
}
cipherTextBytes := []byte(cipherTextDecoded)
plaintext := make([]byte, len(cipherTextBytes) - aes.BlockSize)
stream := cipher.NewCTR(block, iv)
stream.XORKeyStream(plaintext, cipherTextBytes[aes.BlockSize:])
fmt.Println(string(plaintext))
}
正しいGolangコードを得ることにすべてのヘルプは高く評価されています。おかげで今
=====================================
は、私が持っています回答から提案を受けた後、次のようにアップデート: -
これは私のノードのjsのコードである: -
var crypto = require('crypto'),
algorithm = 'aes-256-ctr',
password = '6A80FD8D38D579D1090F6CDB62CA34CA',
// do not use a global iv for production,
// generate a new one for each encryption
iv = '79b67e539e7fcadf'
var typedRequest = {"abc":"cde"}
var cipher = crypto.createCipheriv(algorithm, password, iv);
var hashRequest = cipher.update(JSON.stringify(typedRequest),
'utf8', 'hex');
hashRequest += iv.toString('hex') + cipher.final('hex');
は、これは私の移動コードです: -
package main
import (
"crypto/aes"
"crypto/cipher"
"encoding/hex"
"fmt"
)
func main() {
encKey := "6A80FD8D38D579D1090F6CDB62CA34CA"
cipherText := "af7d1eb42107549a7e3adbce1a79b67e539e7fcadf" // Got from above
encKeyDecoded, err := hex.DecodeString(encKey)
if err != nil {
panic(err)
}
cipherTextDecoded, err := hex.DecodeString(cipherText)
if err != nil {
panic(err)
}
block, err := aes.NewCipher([]byte(encKeyDecoded))
if err != nil {
panic(err)
}
iv := cipherTextDecoded[:aes.BlockSize]
cipherTextBytes := []byte(cipherTextDecoded)
plaintext := make([]byte, len(cipherTextBytes) - aes.BlockSize)
stream := cipher.NewCTR(block, iv)
stream.XORKeyStream(plaintext, cipherTextBytes[aes.BlockSize:])
fmt.Println(string(plaintext))
}
は今、私は完全に何かを取得しています差分tを復号した形で表す。
「IVが必要ですが、Node JSで同じものを使用していません」という意味は何ですか? – JimB
質問が更新されました。気づいてくれてありがとう。 – Chacha
CTRモードでは、初期カウンタが必要です。多くの場合、IVとして供給されます。ノードJSが内部的にカウンタを作成し、それがあるマナーで返されるか、同じマナー内の暗号化されたデータに追加される可能性があります.IVは暗号化されたデータのプレフィックスとして多くの時間を追加します。 Tjereは、暗号化関数が16進文字列をキーとして期待するかどうかといった他の質問ですか? – zaph