how to transform encrypt function from golang to nodejs

huangapple go评论91阅读模式
英文:

how to transform encrypt function from golang to nodejs

问题

我使用golang编写了一个文件加密函数,但我不知道如何使用nodejs实现它。

以下是你要翻译的内容:

我使用golang编写了一个文件加密函数,但我不知道如何使用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())
	}

	// 字符串的字节数组
	key := []byte(aeskey)

	// 创建AES密码器
	block, err := aes.NewCipher(key)
	if err != nil {
		panic(err)
	}

	// IV需要是唯一的,但不需要保密。因此,通常将其包含在密文的开头。
	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)

	// 创建一个新文件来保存加密数据。
	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 := []byte(aesKey)

	// 创建AES密码器
	block, err := aes.NewCipher(key)
	if err != nil {
		panic(err)
	}

	// 在测试解密之前,
	// 如果文本太短,则不正确
	if len(ciphertext) < aes.BlockSize {
		panic("Text is too short")
	}

	// 获取16字节的IV
	iv := ciphertext[:aes.BlockSize]

	// 从密文中删除IV
	ciphertext = ciphertext[aes.BlockSize:]

	// 返回解密流
	stream := cipher.NewCFBDecrypter(block, iv)
	// 从密文中解密字节
	stream.XORKeyStream(ciphertext, ciphertext)
	// 创建一个新文件来保存解密数据。
	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")
}

实际上,我对以下部分有些困惑:

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

我使用node编写了一个解密函数,但它不能正常工作:

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")

谢谢你的帮助!

英文:

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

package main
import (
&quot;bytes&quot;
&quot;crypto/aes&quot;
&quot;crypto/cipher&quot;
&quot;crypto/rand&quot;
&quot;io&quot;
&quot;io/ioutil&quot;
&quot;os&quot;
)
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&#39;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 + &quot;.aes&quot;)
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) &lt; aes.BlockSize {
panic(&quot;Text is too short&quot;)
}
// 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 + &quot;.ts&quot;)
if err != nil {
panic(err.Error())
}
_, err = io.Copy(f, bytes.NewReader(ciphertext))
if err != nil {
panic(err.Error())
}
}
func main() {
key := &quot;0123456789123456&quot;
encrypt(key, &quot;1.ts&quot;)
decrypt(key, &quot;1.ts.aes&quot;)
}

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(&#39;fs&#39;);
var crypto = require(&#39;crypto&#39;);
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(&#39;aes-128-cbc&#39;, aseKey, new Buffer(iv));
buffer = cipher.update(new Buffer(bodyBytes));
fs.writeFile(inputFile + &quot;.node.ts&quot;, buffer)
}
decrypt(&quot;0123456789123456&quot;, &quot;1.ts.aes&quot;)

thanks for you help

答案1

得分: 1

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)

}

英文:
var fs = require(&#39;fs&#39;);
var crypto = require(&#39;crypto&#39;);
function decrypt(aseKey, inputFile){
var fileBody = fs.readFileSync(inputFile)
var  decipher =  crypto.createDecipheriv(&quot;aes-128-cfb&quot;,new Buffer(aseKey) , fileBody.slice(0,16))
var recv = decipher.update(fileBody.slice(16))
fs.writeFileSync(inputFile + &quot;.n.ts&quot;, recv)
}

huangapple
  • 本文由 发表于 2017年5月27日 17:08:32
  • 转载请务必保留本文链接:https://go.coder-hub.com/44215014.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定