gpt4 book ai didi

node.js - 如何将加密函数从 golang 转换为 nodejs

转载 作者:数据小太阳 更新时间:2023-10-29 03:13:32 25 4
gpt4 key购买 nike

我用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())
}

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

其实我只是有些迷茫

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

谢谢你的帮助

最佳答案

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

关于node.js - 如何将加密函数从 golang 转换为 nodejs,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44215014/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com