NodeJS 错误:06065064:数字信封例程:EVP_DecryptFinal_ex:bad decrypt当尝试使用相同的密钥和VI解密时

wnavrhmk  于 2023-06-29  发布在  Node.js
关注(0)|答案(1)|浏览(176)

这是我第一个关于堆栈溢出的问题。我试图建立一个小工具,我可以用来加密和解密配置文件。我为所有文件生成一个密钥,然后将该密钥传递给加密函数。然后当我去解密时,我从文件中阅读密钥,并将其传递给解密函数。与initVector相同。我可以加密就好,但当我去解密我得到“坏解密”错误。下面是代码。请原谅草率的代码,我是新来的,我只是想让它工作,我会重构和清理它一旦它。谢谢你。

const crypto = require ("crypto")
const fs = require ("fs")
var prompt = require('prompt');
const Crypt = require('./crypt.js')

var eschema = {
    properties: {
      fpath: {
        description: 'Enter path to files to encrypt: ',
        type: 'string',
        default: "./config/unsafe"
      },
      epath: {
        description: 'Enter path to store encrypted files: ',
        type: 'string',
        default: "./encrypted"
      },
      kpath: {
        description: 'Enter path to store key and vector files: ',
        type: 'string',
        default: "./"
      }
    }
  };

  var dschema = {
    properties: {
      fpath: {
        description: 'Enter path to files to decrypt: ',
        type: 'string',
        default: "./encrypted"
      },
      dpath: {
        description: 'Enter path to store decrypted files: ',
        type: 'string',
        default: "./"
      },
      vpath: {
        description: 'Enter path to key and vector files: ',
        type: 'string',
        default: "./"
      }
    }
  };
  console.log(process.argv[2])

if(process.argv[2] == '-e'){
    prompt.start()
    prompt.get(eschema, function (err, result){
      if(err){
        throw err;
      }
        iv = crypto.randomBytes(16)
        k = crypto.randomBytes(32)
        let vector = iv.toString('hex').slice(0, 16);
        let key = k.toString('hex').slice(0,32)
        Crypt.encrypt(key, vector, result.fpath, result.epath)
        fs.mkdir(result.epath, (err) => {
            if (err) {
                throw err;
            }
            fs.writeFile(result.kpath +`key`, key, err => {
                if (err) {
                    console.error(err)
                }
                
            })
            fs.writeFile(result.kpath +`vector`, vector, err => {
                if (err) {
                    console.error(err)
                }
                
        
            })
        })

    })
    

}else if(process.argv[2] == '-d'){
    prompt.start()
    prompt.get(dschema, function (err, result){
      if(err){
        throw err;
      }
        let key = fs.readFileSync(result.vpath + "key" )
        let vector =  fs.readFileSync(result.vpath + "vector")
        
        Crypt.decrypt(key, vector, result.fpath)

    })
    

}else{
    console.log("Please pass 'encrypt' or 'decrypt'")
}
const crypto = require ("crypto")
const fs = require ("fs")

class Crypt {
 

    encrypt(key, vector, path, epath){
        fs.readdir(path, (err, files) => {
            if (err) {
            throw err
        }
        console.log(key)
        console.log(vector)
            files.forEach(file => {
                console.log("Encrypting: " + file)
                let cipher = crypto.createCipheriv("aes-256-cbc", key, vector);
                let rfile = path + "/" + file
                let data = fs.readFileSync(rfile)
                let encryptedData = cipher.update(data, "utf-8", "hex");
                encryptedData += cipher.final("hex");
                let eobj = {
                    vi: vector,
                    content: encryptedData
                }
                fs.writeFile(epath + '/' + file + '.enc', encryptedData, err => {
                    if (err) {
                        console.error(err)
                    }
                })
            })
        })
    }

    decrypt(key, vector, path, dpath){
        fs.readdir(path, (err, files) => {
            if (err){
            throw err
        }
            console.log("Decrypting:")
            console.log(files)
            files.forEach(file => {
                let rfile = path + "/" + file
                let data = fs.readFileSync(rfile)
                console.log('Key: ' + key)
                console.log('Vector: ' + vector)
                let decipher = crypto.createDecipheriv("aes-256-cbc", key, vector);
                let decryptedData = decipher.update(data, "hex", "utf-8")
                decryptedData += decipher.final("utf8");
                console.log("Decrypted message: " + decryptedData);
            })
        })
    }
}
module.exports = new Crypt
pgx2nnw8

pgx2nnw81#

解决这个问题的方法是,我没有将向量作为二进制传递,而是作为字符串传递,这导致了这个问题。

相关问题