2017-12-08 1 views
-1

에 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에서 뭔가를 작성한하지만 제대로 해독하고 지금은 오류를 포기하지 않을 : - 올바른 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)) 
} 

을 감사합니다. 이제 감사

=====================================

, 내가 가진 답변에서 제안을 복용 후 다음 업데이트 : -

이 내 노드 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는 암호 해독 된 형태이다.

+0

"IV가 필요하지만 노드 JS에서 동일한 것을 사용하지 않았습니다"는 의미는 무엇입니까? – JimB

+0

질문이 업데이트되었습니다. 주의 해 주셔서 감사합니다. – Chacha

+0

CTR 모드의 경우 초기 카운터가 필요하며 여러 번 IV로 제공됩니다. 노드 JS가 내부적으로 카운터를 생성하고 일부 매너에서 반환되거나 동일한 매너의 암호화 된 데이터에 추가 될 수 있습니다. IV는 암호화 된 데이터에 접두어로 추가되는 경우가 많습니다. Tjere는 암호화 함수가 16 진수 문자열을 키로 기대하는지와 같은 다른 질문이 있습니까? – zaph

답변

3

올바르지 않은 NodeJS 코드입니다. CTR 모드에서 IV가 필요한 경우 crypto.createCipher은 CTR 모드에 대해 정의되지 않은 동작입니다.

의 NodeJS 문서에 따라 crypto.createCipheriv을 사용해야합니다. Golang 코드는이 IV를 일반 텍스트의 시작 부분에서 검색하려고하므로 NodeJS 코드에이 코드를 배치해야합니다.

IV는 각 암호화 작업마다 고유해야하며 CSPRNG를 사용하여이 작업을 수행하는 것이 좋습니다.

+0

알고리즘을 aes-256-ctr과 동일하게 유지할 수 있습니까? 그리고 iv의 길이는 어떻게 될까요? – Chacha

+0

@Chacha 예, IV 길이는 항상 블록 크기와 같으므로 AES의 경우 128 비트입니다. –

+0

질문을 업데이트했습니다. 가능하다면 도움을 청할 수 있습니까? – Chacha

관련 문제