node.js - 如何用nodejs 解密 通过golang加密的文件 ?
PHP中文网
PHP中文网 2017-05-31 10:39:35
0
3
973

我用golang实现了文件加密解密,但是不知道如何使用nodejs来实现golang的揭秘。
golang代码:

package main

import (
    "bytes"
    "crypto/aes"
    "crypto/cipher"
    "crypto/rand"
    "io"
    "io/ioutil"
    "os"
)

func encrypt(aeskey string, filename string) {
    plaintext, err := ioutil.ReadFile(filename)
    if err != nil {
        panic(err.Error())
    }

    // Byte array of the string
    key := []byte(aeskey)

    // Create the AES cipher
    block, err := aes.NewCipher(key)
    if err != nil {
        panic(err)
    }

    // The IV needs to be unique, but not secure. Therefore it's common to
    // include it at the beginning of the ciphertext.
    ciphertext := make([]byte, aes.BlockSize+len(plaintext))
    iv := ciphertext[:aes.BlockSize]
    if _, err := io.ReadFull(rand.Reader, iv); err != nil {
        panic(err)
    }

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

    // create a new file for saving the encrypted data.
    f, err := os.Create(filename + ".aes")
    if err != nil {
        panic(err.Error())
    }
    _, err = io.Copy(f, bytes.NewReader(ciphertext))
    if err != nil {
        panic(err.Error())
    }
}
func main() {
    key := "0123456789123456"
    encrypt(key, "1.ts")
}

通过以上golang代码加密的文件,如果用nodejs解析出来?

不知道转nodejs 主要集中 golang的几个byte处理。这是自己写的,但是是错误的。。

var fs = require('fs');
var crypto = require('crypto');

function decrypt(aseKey, inputFile){

    var fileBody = fs.readFileSync(inputFile)
    var  cipher =  crypto.createDecipheriv("aes128", Buffer.from(aseKey), fileBody.slice(0,16))
    var result = cipher.update(fileBody.slice(16))
    fs.writeFileSync(inputFile+".n.ts", result)
}

decrypt("0123456789123456", "1.ts.aes")

附golang解密这个文件的函数

func decrypt(aesKey string, inputFile string) {

    ciphertext, err := ioutil.ReadFile(inputFile)
    if err != nil {
        panic(err.Error())
    }

    // Key
    key := []byte(aesKey)

    // Create the AES cipher
    block, err := aes.NewCipher(key)
    if err != nil {
        panic(err)
    }

    // Before even testing the decryption,
    // if the text is too small, then it is incorrect
    if len(ciphertext) < aes.BlockSize {
        panic("Text is too short")
    }

    // Get the 16 byte IV
    iv := ciphertext[:aes.BlockSize]

    // Remove the IV from the ciphertext
    ciphertext = ciphertext[aes.BlockSize:]

    // Return a decrypted stream
    stream := cipher.NewCFBDecrypter(block, iv)
    // Decrypt bytes from ciphertext
    stream.XORKeyStream(ciphertext, ciphertext)
    // create a new file for saving the encrypted data.
    f, err := os.Create(inputFile + ".ts")
    if err != nil {
        panic(err.Error())
    }
    _, err = io.Copy(f, bytes.NewReader(ciphertext))
    if err != nil {
        panic(err.Error())
    }
}

func main() {
    key := "0123456789123456"
    decrypt(key, "1.ts.aes")
}
PHP中文网
PHP中文网

认证0级讲师

全部回复(3)
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责声明 Sitemap
PHP中文网:公益在线PHP培训,帮助PHP学习者快速成长!