duanlie3187 2017-05-27 09:08
浏览 59

如何将加密功能从golang转换为node.js

I write a encrypt file function use golang, but I don't how to implement it use nodejs

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 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"
    encrypt(key, "1.ts")
    decrypt(key, "1.ts.aes")
}

In fact, I just have some confuse about

 ciphertext := make([]byte, aes.BlockSize+len(plaintext))
 iv := ciphertext[:aes.BlockSize]

I write a decrypt function use node ,but it don't work well:

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

function decrypt(aseKey, inputFile){
    var buffer = fs.readFileSync(inputFile)
    var arr = Array.prototype.slice.call(buffer, 0)
    var iv = arr.slice(0, 16)
    var bodyBytes = arr.slice(16)
    var cipher = crypto.createCipheriv('aes-128-cbc', aseKey, new Buffer(iv));
    buffer = cipher.update(new Buffer(bodyBytes));
    fs.writeFile(inputFile + ".node.ts", buffer)
}

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

thanks for you help

  • 写回答

1条回答 默认 最新

  • duankui3838 2017-05-31 09:56
    关注
    var fs = require('fs');
    var crypto = require('crypto');
    
    function decrypt(aseKey, inputFile){
        var fileBody = fs.readFileSync(inputFile)
        var  decipher =  crypto.createDecipheriv("aes-128-cfb",new Buffer(aseKey) , fileBody.slice(0,16))
        var recv = decipher.update(fileBody.slice(16))
    
        fs.writeFileSync(inputFile + ".n.ts", recv)
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测